The logic is simple. setOnClickListener
belongs to step 2.
- You create the button
- You create an instance of
OnClickListener
* like it’s done in that example and override theonClick
-method. - You assign that
OnClickListener
to that button usingbtn.setOnClickListener(myOnClickListener);
in your fragments/activitiesonCreate
-method. - When the user clicks the button, the
onClick
function of the assignedOnClickListener
is called.
*If you import android.view.View;
you use View.OnClickListener
. If you import android.view.View.*;
or import android.view.View.OnClickListener;
you use OnClickListener
as far as I get it.
Another way is to let you activity/fragment inherit from OnClickListener
. This way you assign your fragment/activity as the listener for your button and implement onClick
as a member-function.
Related Posts:
- Adding an onclicklistener to listview (android)
- ‘App not Installed’ Error on Android
- Draw a transparent circle onto a filled android canvas
- Can’t start Eclipse – Java was started but returned exit code=13
- google console error `OR-IEH-01`
- Install an apk file from command prompt?
- Unity remote 5 not working
- Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm
- recyclerview No adapter attached; skipping layout
- ‘adb’ is not recognized as an internal or external command, operable program or batch file
- recyclerview No adapter attached; skipping layout
- Enable VT-x in your BIOS security settings (refer to documentation for your computer)
- NetworkType.UNMETERED vs NetworkType.METERED – PeriodicWork
- Find all Bluetooth devices (headsets, phones etc) nearby, without forcing the devices in discoverable mode
- “Default Activity Not Found” on Android Studio upgrade
- How to install APK from PC?
- ERROR Android emulator gets killed
- Android device does not show up in adb list
- how to overcome Android Studio cannot resolve symbol for android classes
- ADB Android Device Unauthorized
- Error “File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it”
- What is the meaning of Log.i()
- How to recompile with -Xlint:deprecation
- Execution failed app:processDebugResources Android Studio
- How can I use adb over WiFi?
- How do I get the dialer to open with phone number displayed?
- RecyclerView vs. ListView
- How to convert int to Integer
- Is it possible to convert apk file to IPA file?
- Facebook login NullPointerException
- “Failed to install the following Android SDK packages as some licences have not been accepted” error
- What does FrameLayout do?
- How to use ScrollView in Android?
- How to make a countdown timer in Android?
- What is com.google.android.gms.persistent and why is it always using the CPU?
- Parcelable encountered IOException writing serializable object getactivity()
- Google Sceneform – Is it deprecated? Any replacement?
- onActivityResult() not called
- “com.example is restricted” when uploading APK to Play Store
- Android Studio: Unable to obtain result of ‘adb version’
- Android simple alert dialog
- How do we use runOnUiThread in Android?
- Could not find com.android.tools.build:gradle:3.0.1
- Error getGoogleAppId failed with status: 10 Android KitKat – GCM and Google Drive
- How do you change the launcher logo of an app in Android Studio?
- “Bitmap too large to be uploaded into a texture”
- How to add .gif Animation in unity Scene ? Does Unity support Animated GIFS?
- How do you close/hide the Android soft keyboard programmatically?
- Android : When do we use getIntent()?
- Rotating a view in Android
- Android error “unable to find explicit activity class”
- Can not resolve method ‘findViewById(int)’
- Cannot resolve symbol ‘context’
- How to get current time and date in Android
- Gradle DSL method not found: ‘compile()’
- How to implement onBackPressed() in Fragments?
- Does anyone know the what’s causing “Package name is not a valid package name” when adding a new layout?
- how to use getSharedPreferences in android
- Set transparent background of an imageview on Android
- How to completely uninstall Android Studio from windows(v10)?
- Play sound on button click android
- How to make the corners of a button round?
- ADB error: cannot connect to daemon
- DELETE_FAILED_INTERNAL_ERROR Error while Installing APK
- Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
- Failed to find Build Tools revision 23.0.1
- ClassLoader referenced unknown path: /data/app/
- Unknown URL content://downloads/my_downloads
- How to inflate one view with a layout
- android edittext onchange listener
- Delete child from firebase [duplicate]
- Android Studio – Company Domain Name
- Installing ADB on macOS [duplicate]
- How to authorize and accept ADB RSA key with broken touch screen on Android
- Determining translationX/Y values for ObjectAnimator; how to move a view to an exact screen position?
- React Native: JAVA_HOME is not set and no ‘java’ command could be found in your PATH
- Android getText from EditText field
- Error: Could not automatically detect an ADB binary. Some emulator functionality will not work until a custom path to ADB is added in the extended…
- How to make layout with rounded corners..?
- Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)
- How to solve failed to find build tools revision 26.0.2?
- Why is the Android emulator screen blank?
- Android Fragment no view found for ID?
- How do I “select Android SDK” in Android Studio?
- How to disable action bar permanently
- Exception ‘open failed: EACCES (Permission denied)’ on Android
- Manifest Merger failed with multiple errors in Android Studio
- hardcoded string “row three”, should use @string resource
- E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab7519c0
- How to handle .tar.md5 files
- notifyDataSetChange not working from custom adapter
- Why am I getting Permission Denied when trying to push a Sqlite file to my rooted Android device?
- What is Activity.finish() method doing exactly?
- Scrollview can host only one direct child
- How to determine the version of Gradle?
- Android studio- “SDK tools directory is missing”
- sendUserActionEvent() is null
- SDK Location not found in android studio
- Android app unable to start activity componentinfo