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!"); } }); }
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,
- 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)
More Posts:
- What is the Difference Between Microsoft SharePoint vs OneDrive - SharePoint
- ls Command to See Hidden Files - Linux
- Microsoft Teams Zoom In and Zoom Out Keyboard Shortcut - Teams
- Java JDBC Delete a Record in Database Table using PreparedStatement - Java
- Docker Run Command Examples - Part 1 - Docker
- Customizing Notepad++ New Document Line Encoding: CR/LF/CR LF - NotepadPlusPlus
- Get HTTP Request Response Headers Safari Browser - MacOS
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2) - Android-Studio