Android - multiple OnClickListener?

I have 4 images. We must be able to click on these images. I would like to know if I need to create 4 OnClickListener , or is there another way to do this correctly?

 public class NavigateActivity extends Activity implements OnClickListener { // images private ImageView phone; private ImageView bookings; private ImageView settings; private ImageView pictures; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.navigate); phone = (ImageView) findViewById(R.navigate.callcenter); phone.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (v == phone) { AlertDialog alertDialog = new AlertDialog.Builder(NavigateActivity.this).create(); alertDialog.setTitle("Attention"); alertDialog.setMessage("Etes-vous sur de vouloir appeler le Call center"); alertDialog.setButton("Oui", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:1232456789")); startActivity(callIntent); } }); alertDialog.setButton2("Non", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialog.show(); } } }); } @Override public void onClick(View v) { // TODO Auto-generated method stub } } 
+8
android
source share
5 answers

You can just do it,

 phone.setOnClickListener(this); bookings.setOnClickListener(this); settings.setOnClickListener(this); pictures.setOnClickListener(this); 

And in the onClick () method,

  @Override public void onClick(View v) { if(v == phone){ // your stuff } else if(v == bookings){ // your stuff } else if(v == settings){ // your stuff } ese if(v == pictures){ // your stuff } } 
+11
source share

You can reuse your listener:

 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { /*...*/ } }); phone.setOnClickListener(listener); bookings.setOnClickListener(listener); /*...*/ 
+5
source share

You can use / make your listener like this: -

 img1.setOnClickListener(imgClk); img2.setOnClickListener(imgClk); img3.setOnClickListener(imgClk); img4.setOnClickListener(imgClk); 

And then you need to create OnClickListener after onCreate / out side onCreate ()

  public OnClickListener imgClk = new OnClickListener() { @Override public void onClick(View v) { switch(v.getId()){ case R.id.img1: //write your code here break; case R.id.img2: //write your code here break; case R.id.img3: //write your code here break; case R.id.img4: //write your code here break; } }; 

Hope this helps you.

+5
source share

For any view to listen to our actions, you must attach a listener to this view. Therefore, you need to connect four listeners. Attaching an OnclickListener and recording an implementation are two different things.

+1
source share

I would suggest using android: onClick to get more readable code.

Example:

 <Button android:id="@+id/buttonId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="buttonText" android:onClick="onClick"/> 

Then in your activity class add the onClick method.

 public void onClick(View v) { switch(v.getId()){ case R.id.myButton: //Your logic goes here... break; default: break; } } 
0
source share

All Articles