set Listener for view id by view type - Android android.view

Android examples for android.view:View

Description

set Listener for view id by view type

Demo Code

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Main {

  public static void setListener(Activity activity, int id) {
    View vw = activity.findViewById(id);
    if (vw instanceof Button) {
      ((Button) activity.findViewById(id)).setOnClickListener((OnClickListener) activity);
    }/*from  w  w  w . ja v a  2 s  . c om*/
    if (vw instanceof ImageButton) {
      ((ImageButton) activity.findViewById(id)).setOnClickListener((OnClickListener) activity);
    }
    if (vw instanceof SeekBar) {
      ((SeekBar) activity.findViewById(id)).setOnSeekBarChangeListener((OnSeekBarChangeListener) activity);
    }
  }

}

Related Tutorials