How to delegate click event from parent (ViewGroup) to child (View) android? - android

You can, of course, assign onclick to all child views through a loop, but is there such an opportunity to hang the click event on the parent element, and then receive the event on the desired child view, as, for example, in html/js through the event target?

You can setup an onClickListener() for the parent and then inside that you can delegate the click using child.performClick(). if you cant to know more, you can reference https://developer.android.com/reference/android/view/View.html#performClick()
The code would look something like this:
parent.setOnClickListener {
child.performClick()
}

Related

onFocusChange including children?

Assume that I have a simple full-screen ConstraintLayout, with a few EditTexts and TextViews in the middle.
Is it possible to know whether one of those views is in focus? What I'm trying to achieve is basically a blanket onFocusChangeListener for all children of a layout.
I've tried assigning a listener to the ConstraintLayout, but it only fires if I tap on the layout itself, outside of those EditTexts.
I could simply assign a listener for each individual element in the UI, but what if I have hundreds?
(I could simply loop through all of them, but please assume I can't)
You could attach a global listener to your ViewTreeObserver with addOnGlobalFocusChangeListener. Something like:
view.getViewTreeObserver().addOnGlobalFocusChangeListener(
new OnGlobalFocusChangeListener() {
#Override
public void onGlobalFocusChanged(View prevFocus, View newFocus) {
// Do stuff here
}
}
);

Detecting which view has been pressed inside list item onClick

I am trying to create an item in a ListView that has multiple options; view and edit. I would like to create it in exactly the same way as android's contact system - see below:
I have added the red boxes to illustrate the behaviour I want. If you press within the left red-box, you call the contact. If you press within the right red-box, you send a text message to the contact. I have already created a similar layout in XML, but I am having trouble implementing this functionality in code.
I have tried to create custom android:onClick function calls for the separate layouts within the item, but calling an onClick method only allows you to pass in the View as a parameter, but not the position. Needing the position to use listview.getItemAtPosition function, I tried to use listview.getPositionForView to return the position but found this was extremely unstable and was very easy to return incorrect positioning due to recycling of views.
What is the best way of implementing a list populated by items with multiple buttons/layouts on each item?
One way to accomplish this while still using a single android:onClick function would be to utilize the setTag() method for each of the clickable views to store an Integer that is the position for the item. For example in your adapter's getView():
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
...
ImageView textMessageView = (ImageView)convertView.findViewById(R.id.whatever);
textMessageView.setTag(new Integer(position));
...
}
Then in your onClick() method you could do something like the following:
#Override
public void onClick(View clickedView) {
int position = clickedView.getTag() instanceof Integer ? ((Integer)clickedView.getTag()).intValue() : -1;
...
}
Don't know that it's ideal, but it allows you to use the XML android:onClick facility and doesn't involve creating separate onClick listeners for each list view item.
I also worked on a similar kind of project.
For doing it, i created my own layout having two linear layouts as linearLayoutLeft and linearLayoutRight.
I then added this layout to a listView.
And then i mentioned the onClickListener of the Listview, i used to getThe view being clicked at the position in the listView.
So for each row in listView, i had two linearLayouts and they have their own onClickListeners.

Dispatch touch event to child in custom viewgroup

Is there any way to dispatch touch event only to specific child in custom view group with stacked children.
For as long as you have reference on both, the child you want to receive the event and the parent, you can implement the parent to pass the touch to the child you wish to receive it.
can you post the code of the View?
So you can make something like:
#Override
public boolean onTouchEvent(Event e){
childToGetTheEvent.onTouchEvent(e);
}
Or is there any reason you cant do something like this?

messing with touch / click events

This doesn't seem like a typical view layout which I'm having trouble with.
I have two listViews.
Each listView has touchListener
(whose purpose is to synchronize scrolling by calling dispatchTouchEvent() to another listView)
ListView also has onItemClickListener to handle clicks on the row of listView.
Everything works as intended up to here.
I'd like to add another clickListener to subview-group of the listView's row to handle click event on the subview.
After attaching this clickListener, I see listView's scroll doesn't always work.
I suspect its because the clickListener of this child view is inspecting touch events (to see if its indeed a click) before the parent(listView)'s touchListener.
I can think of two workarounds to this problem.
attach touchListener instead of clickListener to child, and make it return false for all touch event except FINGER_UP event.
on FINGER_UP event, I execute the method which I initially had in onClickListener
public boolean onInterceptTouchEvent (MotionEvent ev)
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
(ok... I'm confused, I thought touch events goes to child views first and propagate to parents if children don't handle the touches..)
.. How do I implement the method 1?
.. Please help me to figure out #2 as well and to grasp the touch delivery mechanism.
EDIT -
This is where I add OnClickListener to my subview.
viewHolder.layout_author.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent profileIntent = new Intent(ImageListAdapter.this.activity, ProfileActivity.class);
profileIntent.putExtra("JsonUser", jsonAlbumImage.jsonUser);
ImageListAdapter.this.activity.startActivity(profileIntent);
}
});
I'd like to add another clickListener to subview-group of the listView's row to handle click event on the subview.
You simply need to write a custom adapter and assign the OnClickListener in getView(). You can look at the question: Android GridView Button Click Handler for example code.
Also awhile back I answered Android. Scrolling 2 listviews together did you use a similar approach to synchronize your ListViews? When I combine both of the answers, my app functions the way you want. I hope this helps.

ToggleButton - How Do I Manually Call Its onClick Listener?

I have created a custom View which is a descendent of ToggleButton. I want my custom view to have two states i.e. on and off just like ToggleButton but I want to MANUALLY change its state once my custom view has been clicked.
The way I have done this so far is to override the performClick() method but in my performClick() method I want to be able to call the assigned onClick listener, so my question is how do I call my custom view's onClick listener? (I bet there's an obvious answer!)
I realised I just override toggle().

Categories

Resources