In my previous example I had shown how you can create your own custom method that can be called when a button is clicked in your Android Activity while working in Android Studio - https://code2care.org/q/android-studio-button-on-click-example
Now let's see how you can perform the same action by using the onClickListener,
- Create a button in your activity XML file,
- Make sure you add an android:id to it,
- Now in your Java Activity class onCreate method, create the object of the button: Button myButton = findViewById(R.id.button);
- On the myButton object call the onClickListener method with an anonymous implementation.
Example:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button myButton = findViewById(R.id.button2); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("Button Clicked!"); } }); }

Android Studio onClickListener Example
Output on button Clicked:
2021-04-23 23:29:30.182 6655-6655/com.app I/System.out: Button Clicked!
2021-04-23 23:29:30.658 6655-6655/com.app I/System.out: Button Clicked!
Have Questions? Post them here!
More Posts related to Android-Studio,
- Locate MainActivity Java or Kotlin file in Android Studio
- groovy.lang.MissingPropertyException No such property Error
- How to fix java.net.NoRouteToHostException in Android Studio
- Your Android SDK is missing, out of date or corrupted SDK Problem
- [Android Studio] MainActivity does not exist
- [Android Studio] Could not automatically detect an ADB binary
- Android Studio: Cannot perform refactoring operation
- How to Configure Eclipse keymap in IntelliJ IDE
- Android Studio setup was canceled - How to resume
- [Android Studio] Hardcoded string Button, should use @string resource
- The selected device is incompatible : Android Studio
- Android Activity Main xml stuck loading
- Android Studio 1.3 beta now Available for Developers
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip
- Android Studio 4.2 Canary 1 now available
- [Android Studio] Button on click example
- Eclipse like Auto Import Shortcut in Intelij IDE Android Studio
- How to enable line numbers in IntelliJ Android Studio for all files
- Delete Android Studio Projects
- How to update Android Studio
- [Android Studio] Error: Missing system image on device
- macOS Big Sur java.lang.UnsatisfiedLinkError CoreFoundation - Android Studio
- Android [SDK Manager] The system cannot find the path specified
- Failed to install Android.apk on device 'emulator-5554': timeout
- [Solved] Error launching studio
More Posts:
- How to integrate Salesforce CRM Sales and Service with Microsoft Teams - Teams
- Deploy SharePoint wsp solution package using PowerShell - SharePoint
- How to install maven in macOS using Terminal Command - MacOS
- How to destroy PHP session() - PHP
- Install Oh My Zsh on Ubuntu Docker complete steps - Ubuntu
- How to Skip or Ignore JUnit test cases in Java - Java
- macOS Ventura 13: The default interactive shell is now zsh - zsh
- Turn Off Auto-Brightness on iPhone running iOS 16 - iOS