Pass event to parent view?

I have a layout which contains five TextView. When user clicks any of the five TextView, it will do the same thing: open another activity. So, the OnClickerListener behavior to the five TextView is the same. Is it possible that I only add OnClickListerner to one view, e.g. to layout, so I don't have to add OnClickListerner to each of the five TextView? Put it another way, if none of the children components receives CLICK event, will the CLICK event passed through to their parent, the layout?

Thanks.


Asked by: Adelaide505 | Posted: 20-01-2022






Answer 1

The above answer from Mayra is not correct. If the lowest level control (i.e. textview in your case) does not have an OnClickListener() associated with it, then the click event "bubble up" to the parent. It will continue this way until a view is found that implements an OnClickListener; otherwise the click is ignored (I assume). I have taken advantage of this behavior many times.

Also, refer to this post for more details: Stackoverflow Question 8135032

I know this is a very old post, but wanted to correct it for anyone else that stumbles upon this question.

Answered by: Grace640 | Posted: 21-02-2022



Answer 2

I don't believe there is another place to automatically receive click inputs from all views in a layout.

You could extend TextView with your custom OnClick behavior.

Or, just pass the same OnClickListener instance to all of your text boxes.

Answered by: Wilson840 | Posted: 21-02-2022



Answer 3

It may not be the case but I recently spent a couple of hours trying to make a TextView propagate its click events to its parent. I wasn't being successful because apparently if you set the MovementMethod tv.setMovementMethod(LinkMovementMethod.getInstance()); it will catch the "bubbling-up" and never reach its parent.

Answered by: Daisy352 | Posted: 21-02-2022



Similar questions

android - Find the parent Dialog of a Button

I have an alert dialog which contains a button. The button is defined in some XML and the layout is set using Dialog.setContentView(). The button has a listener class and I want to know how, if at all, I can access the dialog from the onClick(View v) method. The reason for this is simply that I want to be able to dismiss the dialog - so if there's an easier/better way to do this then that would be useful t...


android - How to stop parent delete if child present

I want to use constraints in SQLite. Scenario: Stop parent delete if child is present. How can I achieve that?


android - Is there a fill parent for Text Size?

I am trying to make a sign (the ones that drivers use at the airport to find someone "Mr Smith") and I wanted the sign to have the largest Font size possible, (its for a tablet), I could write a function to change the size depending on the length of the Text but is there a way of doing natively/better? Thanks


android - The specified child already has a parent and click event

That is my code... for(int i=0;i<sitesList.getPdf().size();i++) { Bitmap bmp; URL url=null; InputStream is; ImageView iv=null; tr=new TableRow(this); tr.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); for (int j=0;j<2;j++) { iv=new ImageView(this);...


java - get parent object from button click

i got a custom class which inflates its layout from a xml. within that xml i got a button. then in my activity i instanziate that custom class and add it to: a linear layout (the view of the custom class) a typed array (the hole object) now i want that if the button is pressed the object gets removed from both, the typed array and the layout. now my problem is that first i got two pl...


How to get parent id on Android?

I've tried to get parent id of RadioButton (id of RadioGroup) by RadioGroup rg = (RadioGroup) arg0.getParent(); but it failed. Following construction RadioGroup rg = (RadioGroup) findViewById(R.id.radiogr); works well. How can I get parent id at runtime? fails with these logs: 12-26 17:11:05.205: E/AndroidRuntime(912): FATAL EXCEPTI...


android - "specified child already has a parent" error

I have a page that will display a weeks worth of scheduled games for a selected sport. The selected sport and week is stored within a XML file that is opened at the the start of the page. When I click on the next week link, a new week date value is stored to this xml file and then the page is reloaded.Any games for this next week should now be displayed. These games are displayed in a tablelayout where I programmatically c...


android - getting the child tag based on parent tag from xml

guys i have the following xml <Point scount ="1"> <Image name="app.png" /> </Point> I need to parse the xml like this First i need to goto that <Point> and then in that i need to pick the <image> tag. how to do that, now i am fetching the value from image tag like this. NodeList image = doc.getElementsByTag...


android - Can I use the parent of the root layout?

Any View has a parent. If I check the main layout of an activity, I see it has a parent, too. It is a FrameLayout, that also has a parent - itself. Others activities' layouts are not its children, it has only one child - the main layout I have started from. But if it is a FrameLayout, it could take other children, couldn't it? Can I use it somehow? Sometimes its use would make the tree of layouts one storey lower....


java - Why doesn't my view fill the parent?

I have two layouts, this is the parent layout: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/layoutTop" android:layout_widt...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top