Skip to main content

How to create a rounded button in Kotlin Android

How to create a rounded button in Kotlin Android.

Here's a step-by-step tutorial on how to create a rounded button in Kotlin for Android:

Step 1: Create a new project in Android Studio.

  • Open Android Studio and click on "Start a new Android Studio project".
  • Fill in the project details and click "Next".
  • Choose the target devices and click "Next".
  • Select "Empty Activity" and click "Next".
  • Enter the activity details and click "Finish".

Step 2: Open the layout file for the activity.

  • In the Project pane, navigate to "res" > "layout" and open the XML file for the activity layout.

Step 3: Add a button to the layout.

  • Inside the XML file, add the following code to create a button:
<Button
android:id="@+id/roundedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:background="@drawable/rounded_button"
/>

Step 4: Create a drawable resource file for the rounded button.

  • Right-click on the "res" folder and select "New" > "Drawable resource file".
  • Enter a file name for the drawable (e.g., rounded_button.xml) and click "OK".
  • Inside the XML file, add the following code to create a rounded shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF4081" />
<corners android:radius="20dp" />
</shape>

Step 5: Set the rounded button in the activity.

  • Open the Kotlin file for the activity (e.g., MainActivity.kt).
  • Inside the onCreate method, add the following code to reference the button and set its background:
val roundedButton: Button = findViewById(R.id.roundedButton)
roundedButton.setBackgroundResource(R.drawable.rounded_button)

Step 6: Run the app.

  • Connect an Android device or emulator to your computer.
  • Click on the "Run" button in Android Studio.
  • Select your device/emulator and click "OK".

That's it! You have successfully created a rounded button in Kotlin for Android.