In Android, a button respresent something that can be clicked by the user to perform some action.
Firstly, we need to create a project in Android Studio. To do follow these steps:
Click on File, then New and then New Project and give name whatever you like:
- Click on File, then New and then New Project and give name whatever you like
- Then, select Kotlin language Support and click next button.
- Select minimum SDK, whatever you need.
- Select Empty activity and then click finish.
After doing this you will see some directories on the left hand side after your project/gradle is finished loading. It should look like this:
Second step is to design our layout page. Go to app > res > layout and paste the folllowing code:
Modify activity_main.xml file
After setting up the design our final step is to code our progress bar. Open app/src/main/java/yourPackageName/MainActivity.kt
Create Button in MainActivity.kt file
First of all, we define the button and set its atributes.
val button = Button(this) // setting layout_width and layout_height using layout parameters button.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
then, add the button in the layout using addView()
val layout = findViewById(R.id.layout) as LinearLayout // add Button to LinearLayout layout.addView(button)
Run as Emulator:
- Button in Kotlin
- Dynamic TextView in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic Spinner in Kotlin
- Dynamic Switch in Kotlin
- Dynamic ScrollView in Kotlin
- Dynamic RadioGroup in Kotlin
- Dynamic TextClock in Kotlin
- Dynamic CheckedTextView in Kotlin
- Dynamic EditText in Kotlin
- Dynamic DatePicker in Kotlin
- Dynamic Chronometer in Kotlin
- Dynamic AutoCompleteTextView in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic ImageSwitcher in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.