sivaraj
sivaraj

Reputation: 1867

onclick listener in android

I used two image button for Next and Back and i used onclick event for those button i want to which image button fire on onclick and run particular function for next or back in onclick event how will i get which image button fire or onclick event at runtime

Upvotes: 8

Views: 46006

Answers (4)

Mohammad Reza
Mohammad Reza

Reputation: 95

For set onClickListener to a text: Go to activity_main.xml and add:

<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="12dp"
        android:textSize="18sp"
        tools:text="My Github"
        android:textColor="@color/black" />

In MainActivity.java file add:

public class MainActivity extends AppCompatActivity{

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
AppCompatTextView textView = findViewById(R.id.textview);
textView.setOnClickListener(view-> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/fekri86114"))));
           
}
}

Upvotes: 0

user16123931
user16123931

Reputation:

This can be done simple by declaring a unique ID to both the ImageButton in XML as follows

<ImageButton 
     android:id="@+id/backButton"
     .../>
<ImageButton
     android:id="@+id/nextButton"
     .../>

Then in JAVA if you use

Binding

which is by using ViewModel for your page then you can use the following code in onCreate() method and you must place your code inbetween View root = binding.getRoot(); and return root;

View root = binding.getRoot();

ImageButton backBtn = binding.backButton;
ImageButton nextBtn = binding.nextButton;

//Now Comes the OnClickEvent using Lambda a easiest way

backBtn.setOnClickListener(a -> 
{
    //Your Handler Code for Back Button
});

nextBtn.setOnClickListener(b -> 
{
    //Your Handler Code for Next Button
});

return root;

Then for

Normal

ImageButton backBtn = findViewById(R.id.backButton);
ImageButton nextBtn = findViewById(R.id.nextButton);

//onClickListeners
backBtn.setOnClickListener(a -> 
{
    //Your Handler Code for Back Button
});    
nextBtn.setOnClickListener(b -> 
{
    //Your Handler Code for Next Button
});

This Lambda Expression can be done in two ways which are by

Method 1

view.setOnCLickListener(method1 -> 
{
    //Your Handler Code
});

Method 2

If you have already defined a function and you are going set this method then

view.setOnClickListener(method2 -> functionName(); );

Upvotes: 0

Konstantin Burov
Konstantin Burov

Reputation: 69228

Use View.getId() to distinguish between different views that fire onClick events.

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.download:
    //code..
    break;
    case R.id.play:
    //code..
    break;
    case R.id.pause:
        //code..
    break;
    default:
        //code..
    break;
    }
}

Upvotes: 12

Cheryl Simon
Cheryl Simon

Reputation: 46844

You can use anonymous inner classes to write an onClick function for each button.

Button button1 = getMyButton();
button1.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // button 1 was clicked!
   }
  });
Button button2 = getMyButton();
button2.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // button 2 was clicked!
   }
  });

As Konstantin mentioned, you can also use the passed in View and switch on the id. However, I find that a bit messier if you end up with lots of clickable things.

Upvotes: 12

Related Questions