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

7

5

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.

Joppa answered 6/8, 2014 at 7:55 Comment(7)
post the code of the method and xmlJohnie
Could you explain a little more in detail? Are those views always the same ones or do you add them dynamically? Do you know you can also add OnClickListeners to each view?Selfsealing
@Korcholis Views always are the same.I know about setOclickListener for the views, but how can I discover which one has clicked?I hace edited my question for youJoppa
You can generate toast and check it in onClick().Wanids
In that case, use the way others suggest. I find interesting the one from @user2959120, as he also shows you how to assign the class itself as listenerSelfsealing
I could not find out how can I assign a class OnCLickListener.If I set onClick method for the fragment or activity class the problem is solved.Joppa
Yes yes, when I wrote class I meant Activity or Fragment.Selfsealing
W
5

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.

Waterside answered 6/8, 2014 at 8:31 Comment(0)
S
3

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;
          }
}
Severe answered 6/8, 2014 at 8:6 Comment(2)
Where did you override the onClick method?I suppose it should be in activity or fragment class, but it does not work.Joppa
@Joppa in same activity or fragment class. Take care to do that outside of onCreate method.Severe
C
0
There is View v parameter in onClickListner(View v) method : 


@Override
protected void onCreate(Bundle savedInstanceState) {
    LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
    layout.setOnclickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.linearLayout){
        //your logic
    } 
}
Cunnilingus answered 6/8, 2014 at 8:0 Comment(4)
Maybe I didnt explain good enough.I want to run action A if a view has clicked and action B if the layout has clicked in free space, I mean no view has clicked for action B.Joppa
your solution seems to solve the problem, but I cant find onCLick method for activity or fragment class.How can I override this method for the activity or fragment?Joppa
@Dilavar this will set the onClick only for the layout, not layout and the views separately.Joppa
then set view.onClickListener(this); like btn.setOnclickListner(this)Cunnilingus
K
0

Remember that the method OnClick have one parameter that indicates which View is clicked!

public void onClick(View v) {
      switch(v.getId()) {
        case R.id.btn1:
          // first button
          break;
        case R.id.btn2:
          // second button
          break;
      }
  }
Kelton answered 6/8, 2014 at 8:3 Comment(0)
F
0

Assign the id to each of your views by android:id="@+id/v1" then in

onClick(View v)

check for the view like

if(v == findViewById(R.id.v1))
{
 // v1 specific action
}
else if(v == findViewById(R.id.v2))
    {
     // v2 specific action
    }
Fullbodied answered 6/8, 2014 at 8:7 Comment(6)
you should use switch statement for this..it could be more easier to implementXavier
@Prag'sシ You can use both switch as well as if else ladder. and switch is better only if you have many cases.Fullbodied
yes but you are working with multiple ID if..elseif..else could be more complex to implement..Xavier
@Prag'sシ How you can say it is more complex?Fullbodied
just in simple line you have to find ID every time with if(v == findViewById(R.id.v1)) and in switch(v.getItemId()) can do the work..Xavier
@Prag'sシ Here in switch also you have to find the id in each case by calling as R.id.desiredView.Fullbodied
P
0

Ok, it's easy.
You have to add an OnClickListener to each view (button, textView and so on), and to the whole layout as well.
In this case, the onClickListeners that will be launched if you click are the view's onClickListener and the layout's onClickListener.
So no need for a whole bunch of classes.
One will be enough if you try this (do actions depending on the id and don't forget the layout ;) ):

public void onClick(View v) {
  switch(v.getId()) {
    case R.id.layout:
      layout actions here
      break;
    case R.id.textview:
      textview actions here
      break;

this onClickListener is for all of your views ;)

Play answered 6/8, 2014 at 8:26 Comment(0)
W
0

for anyone interested you can also log the currently clicked view:

 getActivity()
      .getWindow()
      .getDecorView()
      .findViewById(android.R.id.content)
      .setOnTouchListener((v, event) -> {

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    Log.v("[onClick]", v.getClass().getCanonicalName());
                }

                return false;
            });
Why answered 19/3, 2018 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.