Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClickListener on a LinearLayout

Tags:

android

People also ask

What is the use of OnClickListener?

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.

How do you make a clickable layout?

To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.

What is OnClickListener in Android Studio?

OnClickListener and wires the listener to the button using setOnClickListener(View. OnClickListener) . As a result, the system executes the code you write in onClick(View) after the user presses the button. The system executes the code in onClick on the main thread.

What is new view OnClickListener ()?

The onClickListener is constructed as: new View.OnClickListener() { public void onClick(View v) { } } That onClick method is used by the listener to handle the event (in this case, a button press). So, you would put the code you would like executed in that method.


You have to set LinearLayout attribute android:clickable="true" in the xml layout file or setClickable(true) in the java activity code.


i noticed that all the advices above don't helpt as long as any of the child elements inside the LinearLayout has the attribute android:textIsSelectable="true".


I found that that setClickable(true) would still cause clicks to go to children of the linearlayout. I found that to have the LinearLayout capture the touch instead of it's children I had to override the dispatchTouchEvent method so I created a subclass of LinearLayout for just this purpose. Seems like an ugly solution though.


you should set the LinearLayout's focusable to true and set all the children view's focusable to false, don't use the android:clickable="true", but you can't see the effect of the click of the linelayout. BTW, the best way is to implement the onTouchEvent api.


android:clickable="true" works perfectly under one condition. Youhave to put the childs inside the LilnearLayout to android:clickable="false".