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
- Android Studio setup was canceled - How to resume
- The selected device is incompatible : Android Studio
- Android Studio : Change FAB icon color : FloatingActionButton
- How to rename package name in Android Studio
- Android Emulator Screenshot saved location
- Android Activity Main xml stuck loading
- [Solution] Android Studio does not displays Toolbar in Layout Design
- [Event Log] Android Studio performance could be improved
- The package must have at least one . separator [Android Studio]
- Android Studio SDK Build-tools 23 rc2 not getting installed
- Android Emulator window was out of view and was recentered
- Emulator: Warning: restoring GLES1 context from snapshot. App may need reloading.
- How to Import External Jars to Android Studio Project
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2)
- Create assets folder in Android Studio
- Eclipse like Auto Import Shortcut in Intelij IDE Android Studio
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio
- Your Android SDK is missing, out of date or corrupted SDK Problem
- macOS Big Sur java.lang.UnsatisfiedLinkError CoreFoundation - Android Studio
- [Android Studio] How to locate code for activity_main.xml
- groovy.lang.MissingPropertyException No such property Error
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip
- Step-by-Step: How to install Flutter in Android Studio
- [Solved] Error launching studio
More Posts:
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database - Java
- New-SPLogFile PowerShell - create new SharePoint log file - SharePoint
- Android: Unknown error code during application install : - Android
- Java JDBC Transition Management using PreparedStatement Examples - Java
- fix macOS: The digital signature on the update is missing or invalid. Ventura - MacOS
- How to redirect SharePoint Site Collection to different URL - SharePoint
- How to lock cells in Microsoft Excel for Mac - Windows
- How to URL Decode a Query String in Python - Python