To get started with Android we must know how to play with buttons, text fields(TextEdit), labels, etc. and you must know, how to fix common problems in Android Studio.
In this example, we will learn how to create a button and its handler.
- Open Android Studio and create a new Project.
- Android Studio panel will open. In the main panel, you will see java file MainActivity.java this is the java file of the activity which you created at the time of creation of Project.
- Now you need to open up a few files you will need.
a. XML layout file of your activity. i.e. activity_main.xml (placed in app -> res(resources folder) -> layout -> activity_main.xml)activity_main.xml file location Note: You may see rendering problems. This is due to missing SDK(Software Development Kit) for the target Android version. SDK version tells the IDE, how to render layouts. To fix the rendering problems update SDK from Tools -> Android ->SDK Manager.
Open SDK Manager Android Studio Android Studio SDK Manager will show installed and available SDK versions.
SDK Panel Android Studio Update for the required target SDK. By checking the required SDK and click OK. It will download the checked SDK versions. (download may be slow as because of large file size also depends on your internet connection, so check only the required SDK).
Now change SDK version from the icon from the layout in activity_main.xml.Change SDK version Android Studio b. strings file of your project i.e. strings.xml (placed in app->res->values->strings.xml) in which all the variables of your interface are placed.
strings.xml in Android Studio - In activity_main.xml open Design view from the bottom of the panel. Drag and drop Button from the Palette.
Place NewButton in Android Activity - Open strings.xml create a new string for button name.
1<string name="button_name">Click Me</string>
create a new string in strings - Open text of activity_main.xml from the bottom of activity_main.xml panel.
Open text view of activity_main Change the android:text value in inverted commas with @strings(reference to strings.xml file)/button_name(variable name in strings.xml).
1android:text="@string/button_name"change text of button in activity_main Change android: id value in inverted commas with the new id with “@+id/click_me_button” (@+id add up a new id for the Palette items) to handle button in Android Studio.
Change id of the button in activity_main you can also change it in design view by double-clicking the button and changing the value of id.
Change button id Android - Add button click property in activity_main.xml in <Button /> tag.
1android:onClick="doClickMeButton"
add button onClick in Android Studio Activity - In your java file i.e. MainActivity.java implement the method for click_me_button and pass instance reference of the View.
1234public void doClickMeButton(View v){}
Implement button onclick method An error will show up you need to import the View class. Pressing Alt+Enter will show up suggested options, or it will automatically import the View class. You can manually import the view class by placing the import code outside the class.
1import android.view.View; - Create a Toast in implemented method body.
1Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
Import toast class to make it work.
1import android.widget.Toast;Toast: Toast is to show the user a little message on any action. For more detail click here.
1234public void doClickMeButton(View v){Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();}handle button in android studio - Run app using AVD(android virtual device), many other alternates are also available like genymotion, blue stacks, etc. I prefer genymotion based on its performance.
Run button app button app running Download the project zip file here.
Alternate download here.