How to trigger selectors from the layout view?
Asked Answered
C

1

2

The problem

Suppose you have a view inside a layout view, and you wish that each touching event on the layout would affect the view's background selector.

for example, when you touch the layout , the selector of the view will choose the state_pressed state.

What I've tried

I've read about duplicateParentState and addStatesFromChildren , but I think it's the opposite of what I'm searching for.

I've also tried to use splitMotionEvents, but it didn't help.

The question

How do you do this?

Also, what should be done in case the layout is inside a listView (as an item within it) ?

Calm answered 23/3, 2014 at 8:36 Comment(6)
Did you tried using those attributes?Latakia
@Luksprog yes, but maybe not in the correct way.Calm
view.setDuplicateParentState(true) where virew is a child of a layout view (ViewGroup)Nicks
@Nicks have you tried it?Calm
Yes, i had vertical LinearLayout and added a Button to it, clicking on the LL "selects" the ButtonNicks
@Nicks can you please put the code & xml into an answer instead of comments? also, what should be done in case the layout is an item of a listView?Calm
B
1

Consider you have a spinner inside a linear layout. Make focusable property of view(here that is spinner) to false. And onClick of linear layout call performClick on view(spinner in this case).

EDIT:

In focus Change Listener of linear layout if it has focus, call requestFocus on view. You can follow same method to other states of view

Another method:

do not apply any selector to layout(linearlayout in this case) and add whatever selector you want to view(spinner in this case). For view add this parameter

android:duplicateParentState="true"
Bilious answered 23/3, 2014 at 8:58 Comment(11)
isn't it possible to just pass the selectors states to be applied on its child instead?Calm
This one actually worked. is it possible to have a selector that will be applied to the background for one state, and the foreground for another, as used on the play store (when you click on an item) ? Also, what should you do for listViews? I know listView already has "drawSelectorOnTop", but it will work only for the foreground, and not the background, plus it covers all of the item instead of its inner view.Calm
I did not really understand what do you want to do in listview. Sorry.. can you please explain bit more about your requirements for listviewBilious
If you are using custom row in a listview, same rule apply.. Because you are row will be inside some layout.Bilious
@androiddeveloper Just now found one blog about your another query(regarding foreground color) antoine-merle.com/blog/2013/07/17/…Bilious
This is actually a very good link. However, I think that the play store has 2 kinds of drawables, right? the one of the background of the item (the card), and the one that is on top (when you touch it)... Am I correct?Calm
My pleasure.. Thanks to you also.. It also motivates me to learn, when a person who has high reputation accepts my answer.. thanks a lotBilious
about the code, if you choose to use it, do know that it's incomplete - he forgot to do null checks and add attributes (i like writing such things in XML instead of code and also see them in the UI designer when possible). oh and thanks for the compliments.Calm
Yes.. even I like writing it in xml.. But I found that interesting.. so i wanted to share that with you..Bilious
yes, thanks. as i said, it worked. just had to change it a bit.Calm
It seems this solution doesn't work well on some devices. I don't get it. I've set the selector to be opaque color, and on SGS2 I can't see it at all, yet on other devices (nexus4 , SGS3) I can. How could it be?Calm

© 2022 - 2024 — McMap. All rights reserved.