In this article we will learn how to create Button also how to create Button Click Listener in android studio. Buttons are the most used widgets in any android app. In other words the Click Listener interface also. In order to create Button we to add its widget in the XML file as following.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.pmphysiciansvisits.insert.fragments.PrivateClinicsVisitFragment">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Here"
android:textAllCaps="false"
android:layout_margin="20dp"
android:onClick="doSomething"/>
</FrameLayout>
Note that the attribute onClick, that specifies the action the will be done when the user click the button. This attribute take the name of the method in the linked java file. as following.
public void doSomething(View v) {
switch(v.getId()) {
case R.id.button:
// Button was clicked, do something.
break;
}
Using the same click event for one or more
Views in the XML
Moreover, using the above method we can handle the click listener for multiple views in an xml file. use following code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.pmphysiciansvisits.insert.fragments.PrivateClinicsVisitFragment">
<Button
android:id="@+id/buttonOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Button One"
android:textAllCaps="false"
android:layout_margin="20dp"/>
<Button
android:id="@+id/buttonTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Button Two"
android:textAllCaps="false"
android:layout_margin="20dp"/>
</LinearLayout>
Now, in the jave file we need to update the dosomething method as following.
public void doSomething(View v) {
switch(v.getId()) {
case R.id.buttonOne:
// Button was clicked, do something.
break;
case R.id.buttonTwo:
// Image was clicked, do something else.
break;
}
}
Defining external Listener
Another way to create onclick listener for buttons is to create an external listener as an object. Furthermore, we need to use this way when the code inside an inline listener is too big and your method / class becomes ugly and hard to read. Also, when we 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. For example, give help when long click on any element:
public class HelpLongClickListener implements View.OnLongClickListener
{
public HelpLongClickListener() {
}
@Override
public void onLongClick(View v) {
// show help toast or popup
}
}
Then you just need to have an attribute or variable in your Activity to use it:
HelpLongClickListener helpListener = new HelpLongClickListener(...);
button1.setOnClickListener(helpListener);
button2.setOnClickListener(helpListener);
label.setOnClickListener(helpListener);
button1.setOnClickListener(helpListener);
Inline onClickListener
We can use the inline onClickListener by the following way.
bottonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do stuff here...
}
});
Custom Click Listener to prevent multiple fast clicks
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 (for instance, 1000ms). When the button is clicked, a check will be ran to see if the trigger was executed in the past amount of time you defined, and if not it will trigger it.
public class SingleClickListener implements View.OnClickListener {
protected int defaultInterval;
private long lastTimeClicked = 0;
public SingleClickListener() {
this(1000);
}
public SingleClickListener(int minInterval) {
this.defaultInterval = minInterval;
}
@Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultInterval) {
return;
}
lastTimeClicked = SystemClock.elapsedRealtime();
performClick(v);
}
public abstract void performClick(View v);
}
And in the class, the SingleClickListener is associated to the Button at stake.
myButton = (Button) findViewById(R.id.my_button);
myButton.setOnClickListener(new SingleClickListener() {
@Override
public void performClick(View view) {
// do stuff
}
});