In an android app, I have a GridView holding TextView created by an adapter. In those TextView I add an icon which has three states (pressed, selected and default.)
I removed the default selector of the GridView with android:listSelector="#00000000" and I would like the selected state of the icon to display instead. But although the pressed state works (ie when the TextView is pressed, the pressed version of the icons is shown) the seleted doesn't.
I've tried those tricks (found at different places on the web) but it didn't work either:
setting
android:descendantFocusability="afterDescendants"
or
android:drawSelectorOnTop="true"
or (in the TextViews)
android:duplicateParentState="true" />
And if I set the TextView to be focusable, it gets focus independently of the GridView (ie, clicking on it doesn't call the GridView onClick method...)
The icon is defined in a xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/ic_flag_bg_pressed"/>
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/ic_flag_bg_pressed"/>
<item android:state_focused="true"
android:drawable="#drawable/ic_flag_bg_selected"/>
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/ic_flag_bg_default"/>
</selector>
Is there a way to tell the GridView to pass the focused state to its children ?
Got stuck on this as well. An alternate approach that can look okay is to use a border style selector and draw it on top.
Related
As the title saying , I need a view to hide or show a recyclerview.
Like Spinner.
And I can only show/hide the recyclerview by clicking it.
(It should be a line like Spinner.)
The following image would be more comprehensive.
Is there any way to reach this?
You need to create a custom toggle button with different drawables based on what state the button is in. You have to use a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
See the documentation here: Custom Background
Then you set the visibility on the other view in the onClick event for the button to View.GONE/View.VISIBLE
recylerView.setVisibility(View.GONE);
recylerView.setVisibility(View.VISIBLE);
I'm using Buttons in children of ExpandableListView.
I've added custom drawable to backgrounds of the buttons so that it can show another images per status.
It shows/update backgrounds of the buttons normally but sometimes it doesn't when buttons are pressed in a specific child.
Here is the drawable I use for button background.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_plus_highlight" />
<item android:state_focused="true" android:drawable="#drawable/button_plus_highlight" />
<item android:drawable="#drawable/button_plus_normal"/>
</selector>
Add this parameter on ListView in XML
android:descendantFocusability="blocksDescendants"
I hope this helps. :)
I have a LinearLayout containing 2 TextView, one larger and black, one smaller and light grey. The LinearLayout's background is white by default then on touch quite a strong blue, and when it's touched (IE Blue), the grey text is lost in the colour, so I want to change it to white.
I've seen (using buttons) I can give textColor a resource file like this to change the text's colour
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
Could I do something similar for my Layout/TextView? I thought about using an OnTouchListener, but the colour didn't change at the same time as the background did, and I've not found an onTouchEnd event yet to reset the colour back to grey.
You can applicate android:duplicateParentState="true" to the two TextView into your LinearLayout.
Then, the click event will be propagate to the two child views.
How about adding a onTouchListener to the parent ==> Returning false from this call back ==> Also fetch all children of the parent in same call back ==> Cast them to TextViews and set text color ==> Call Parent.invalidate().
When creating button drawables, I typically follow the following format to implement an "onClick" change of background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/RESOURCE_FOR_CLICKED" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/RESOURCE_FOR_CLICKED" />
<item android:drawable="#drawable/RESOURCE_NORMAL" />
</selector>
This works fine for me when I am creating traditional buttons as I want them to return to their original state once onClick is finished, and I don't have to implement any code.
However, this does not work for RadioButtons because I actually want their background drawable to be different in the non-pressed state once they have selected.
Are there XML attributes for states involving radio buttons that I should be aware of to implement this sort of thing? If not, do I have to manage the changing of backgrounds in code?
android:state_checked will let you specify a drawable for when a radio item is selected.
I have the following UI presented when my ListView is empty:
Now, I want that when the user will press this "New Reminder" layout, it'll change to a "highlighted" state (with the blue focus background in ICS and the yellow color in GB)
The layout is clickable and the onClick method is called, but there is no indication for the user while he press his finger down.
I tried setting focusable to true, but it didn't do the trick.
What can I do to give any view the default "pressed" effect?
Thank you!
With your layout, you can set background with drawable like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:drawable/star_big_on" />
<item android:state_pressed="true" android:drawable="#android:drawable/star_big_on" />
<item android:drawable="#android:drawable/star_big_off" />
</selector>
Draw-able like below
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/left_radio_selected"/>
<item android:state_enabled="false" android:drawable="#drawable/left_radio_inactive"/>
<item android:state_enabled="true" android:state_selected="true" android:drawable="#drawable/left_radio_selected"/>
<item android:drawable="#drawable/left_radio_active"/>
and set your linerlayout.setSelected(true);
I don't know if I understand you correctly. If you want to create pressed state for this layout, you have to prepare state drawable xml (more info). Up there you can set what should your layout looks like while in pressed state.
I'm not sure but your question seems a little vague. From my understanding what you want might be suggested in this post Force a ListView item to stay "pressed" after being clicked?