
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!
- JDK Location in Android Studio
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2)
- [Android Studio] MainActivity does not exist
- [Android Studio] Hardcoded string Button, should use @string resource
- Android Studio : Connection Error : Failed to download patch file
- [Android Studio] Could not automatically detect an ADB binary
- Show Android Studio Emulator in a Separate Window
- Android Emulator Screenshot saved location
- How to Import External Jars to Android Studio Project
- How to remove Floating Action Button Android Studio Blank Activity Template
- Error running 'app': No target device found.
- Use Netbeans keyboard shortcuts in Android Studio
- Instant Run requires Tools | Android | Enable ADB integration to be enabled
- [Solved] Error launching studio
- [Android Studio] Error: Missing system image on device
- Android Studio 1.3 beta now Available for Developers
- Android Studio : Change FAB icon color : FloatingActionButton
- Android Studio emulator/Device logCat logs not displayed
- Delete Android Studio Projects
- Android Emulator window was out of view and was recentered
- Android Studio NoClassDefFoundError: java.awt.Toolkit
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip
- Android Studio : Implement abstract methods shortcut
- Check Android Studio App is M1/M2 Chip based post installation
- Android Studio: Cannot perform refactoring operation
- Install Python on Alpine Linux - Docker - Docker
- Calculate Sum of List elements using Java Streams - Java
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ - Java
- Merge multiple zip files without unzipping (extracting) - HowTos
- 9 Ways to Loop Java Map (HashMap) with Code Examples - Java
- Cannot start Android Studio. No JDK found - Android-Studio
- How to turn off Stage Manager - macOS Ventura - MacOS
- How to enable line numbers in IntelliJ Android Studio for all files - Android-Studio