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,
- Error running 'app': No target device found.
- How to save IntelliJ IDE Console logs to external log file
- Android Studio : Implement abstract methods shortcut
- Android Studio Error:(19, 0) Gradle DSL method not found: android()
- Android [SDK Manager] The system cannot find the path specified
- [Solution] Android Studio does not displays Toolbar in Layout Design
- [Android Studio] failed to find Build Tools revision 23.0.0 rc1
- [Android Studio] Hardcoded string Button, should use @string resource
- JDK Location in Android Studio
- Android Studio : Connection Error : Failed to download patch file
- Your Android SDK is missing, out of date or corrupted SDK Problem
- Cannot start Android Studio. No JDK found
- Android Studio : Build Tools 23.0.0 rc1 is deprecated
- Android Studio emulator/Device logCat logs not displayed
- Delete Android Studio Projects
- Android Studio : Change FAB icon color : FloatingActionButton
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2)
- Building library Gradle Project Info: Downloading services.gradle.org
- [Android Studio] Could not automatically detect an ADB binary
- The package must have at least one . separator [Android Studio]
- Create assets folder in Android Studio
- How to remove Floating Action Button Android Studio Blank Activity Template
- [Event Log] Android Studio performance could be improved
- The selected device is incompatible : Android Studio
- Disabling Spell Check in Android Studio
More Posts:
- How to check your installed version of Git - Git
- How to install Packages in Sublime Text Editor - Sublime-Text
- Transfer files between Android and Mac OS X using usb cable - Mac-OS-X
- Locate MainActivity Java or Kotlin file in Android Studio - Android-Studio
- HTML, HTML5. CSS, CSS3 Color Codes 2020 - Html
- How to run a Maven jar project from eclipse without tomcat - Eclipse
- Docker Desktop needs privileged access macOS - MacOS
- 10 Beginners Commands for macOS Terminal Usage - MacOS