Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to know which View is clicked in a layout OnClickListener?

I have a layout which contains some views. I want to set some actions when a user clicks anywhere on the layout, so I have set an OnClickListener for the layout which works as it should. But how can I know which view was clicked? I want to assign different actions depending on the view which was clicked; or maybe no view was clicked and it was only the layout itself. So the problem is if I set OnClickListener for one of the views, both OnCLickListeners related to the layout and the view will get activated, but I want only the view action. Thanks in advance.

like image 583
Babak-Na Avatar asked Aug 06 '14 07:08

Babak-Na


People also ask

How do I know if my TextView is clicked?

Inside the onClick(View v) implementation add: v. getId(); To determine whice TextView pressed.

When a button is clicked which listener is used?

If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.

What is view OnClickListener in Android Studio?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.


2 Answers

Other answers are quite abstract and some are incorrect. You can't Override onClick method for an Activity as it doesn't have one (unless of course you implement an OnClickListener).

Furthermore, if you set the listener for the layout the View argument received by the onClick method will always be the layout.


You can either create your own custom layout that intercepts the clicks and check if you clicked on a view, or you can set listeners for all the views.

A somewhat cleaner solution is using a single listener and checking if it's a view that was clicked.

private final String TAG = "MainActivity";
List<View> views = new ArrayList<View>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    TextView tv1 = (TextView) findViewById(R.id.tv1);
    TextView tv2 = (TextView) findViewById(R.id.tv2);
    TextView tv3 = (TextView) findViewById(R.id.tv3);

    views.add(tv1);
    views.add(tv2);
    views.add(tv3);

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isView = false;
            for (View view : views) {
                if (v.equals(view)) {
                    isView = true;
                    break;
                }
            }

            if (isView) {
                Log.e(TAG, "Click on view");
            } else {
                Log.e(TAG, "Click on layout");
            }
        }
    };

    tv1.setOnClickListener(clickListener);
    tv2.setOnClickListener(clickListener);
    tv3.setOnClickListener(clickListener);
    rLayout.setOnClickListener(clickListener);
}

If you want different actions for different views, just create a listener for each of them.

like image 165
Simas Avatar answered Nov 14 '22 23:11

Simas


For example Layout has two views View1 and View2. I assume that you have already inflated them. So you have to set OnClickListiner for each of them

Layout.setOnClickListener(this);
View1.setOnClickListener(this);
View2.setOnClickListener(this);

Then you have to override method:

public void onClick(View v) 
    {
        switch(v.getId())
        {
            case R.id.id_for_layout:
                // do what you want when user click layout
                break;

            case R.id.id_for_first_view:
                // do what you want when user click first view
                break;

            case R.id.id_for_second_view:
                // do what you want when user click second view
                break;
          }
}
like image 38
Mladen Rakonjac Avatar answered Nov 15 '22 01:11

Mladen Rakonjac