If you are new to Android Programming and Android Studio IDE and want to know how to perform a button on click action you are at the right place,
In this example I have created a simple project "My Application" that has a hello world text at the center of the Activity, I have replaced it with a button, the activity_main.xml file looks like this, note that I have added a new attribute android:onClick="buttonClicked" which will be the method in MainActivity.java that will perform the action when this button is clicked!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="133dp"
android:layout_height="45dp"
android:onClick="buttonClicked"
android:text="Button"
tools:layout_editor_absoluteX="139dp"
tools:layout_editor_absoluteY="279dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
*
* This method will be called when the button
* with id @+id/button is clicked
*
* @param view
*/
public void buttonClicked(View view) {
Toast.makeText(this,"Hello, you just clicked the button!",
Toast.LENGTH_SHORT).show();
}
As you can see I have defined the method public void buttonClicked(View view) that will display a toast message when the button is clicked.
β‘οΈThe other way to implement Button on Click action in using onClickListener - check out examples - https://code2care.org/q/android-studio-button-on-click-listener-example
Have Questions? Post them here!
- Step-by-Step: How to install Flutter in Android Studio
- [Android Studio] How to locate code for activity_main.xml
- [Event Log] Android Studio performance could be improved
- Android Studio emulator/Device logCat logs not displayed
- Show Android Studio Emulator in a Separate Window
- Android Studio SDK Build-tools 23 rc2 not getting installed
- Android Emulator Screenshot saved location
- [Android Studio] Error: Missing system image on device
- Android Studio NoClassDefFoundError: java.awt.Toolkit
- Android [SDK Manager] The system cannot find the path specified
- [Android Studio] Could not automatically detect an ADB binary
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip
- [Android Studio] MainActivity does not exist
- How to enable line numbers in IntelliJ Android Studio for all files
- Android Studio - Installed build tools revision 31.0.0 is corrupted. Remove and install again using the SDK manager.
- Android Studio setup was canceled - How to resume
- Your Android SDK is missing, out of date or corrupted SDK Problem
- Locate MainActivity Java or Kotlin file in Android Studio
- How to Determine Android Studio Gradle Version
- Delete Android Studio Projects
- [Android Studio] Button on click example
- Create assets folder in Android Studio
- Disabling Spell Check in Android Studio
- How to save IntelliJ IDE Console logs to external log file
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2)
- Command to know the installed Debian version? - Linux
- Make Android View Scrollable both Horizontally and Vertically - Android
- How to Know Version of Windows Terminal - Windows
- Installing vue.js in Laravel 8 - PHP
- Python: Determine Variable Type Example - Python
- How to format or prettify XML in Notepad++ - NotepadPlusPlus
- How to Subscribe to Google Nest Aware on iPhone - Google
- Get count of SharePoint List or Document Library Items using PowerShell - SharePoint