Tutorial by Examples

Say we have a button (we can create it programmatically, or bind it from a view using findViewbyId(), etc...) Button btnOK = (...) Now, create an anonymous class and set it inline. btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ...
When we create a button in layout, we can use the android:onClick attribute to reference a method in code to handle clicks. Button <Button android:width="120dp" android:height="wrap_content" android:text="Click me" android:onClick="handleCl...
When we create any View in layout, we can use the android:onClick attribute to reference a method in the associated activity or fragment to handle the click events. XML Layout <Button android:id="@+id/button" ... // onClick should reference the method in your activity or fra...
To catch a long click and use it you need to provide appropriate listener to button: View.OnLongClickListener listener = new View.OnLongClickListener() { public boolean onLongClick(View v) { Button clickedButton = (Button) v; String buttonText = clickedButton.getText().toStri...
When should I use it When the code inside an inline listener is too big and your method / class becomes ugly and hard to read You want to perform same action in various elements (view) of your app To achieve this you need to create a class implementing one of the listeners in the View API. ...
In order to prevent a button from firing multiple times within a short period of time (let's say 2 clicks within 1 second, which may cause serious problems if the flow is not controlled), one can implement a custom SingleClickListener. This ClickListener sets a specific time interval as threshold (...
There are many possible ways of customizing the look of a Button. This example presents several options: Option 0: Use ThemeOverlay (currently the easiest/quickest way) Create a new style in your styles file: styles.xml <resources> <style name=“mybutton” parent=”ThemeOverlay.AppC...

Page 1 of 1