GridView item selector checked state not appearing - android

I have a GridView with a bunch of items that are being populated using a custom adapter. The grid view is set to CHOICE_MODE_MULTIPLE_MODAL in java, and I'm able to select things using the contextual action bar (all of this works fine).
I want the grid items to highlight when pressed and have a different highlight when selected (exactly the behavior you'll see in the Gallery app in ICS).
I have a selector which is being specified in the grid view XML like so: listSelector="#drawable/grid_item_selector". I have also specified android:drawSelectorOnTop="true". Here is the selector XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/grid_item_selected" android:state_activated="true"/>
<item android:drawable="#drawable/grid_item_selected" android:state_checked="true"/>
<item android:drawable="#drawable/grid_item_selected" android:state_selected="true"/>
<item android:drawable="#drawable/grid_item_pressed" android:state_pressed="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
The pressed state works perfectly. However, the checked/selected states never appear.
Even if I set an item to be checked in my java code, the checked state never appears.
I can't set the selector as the background of the grid items themselves because I need the selected state drawable to be the foreground, not the background.

The selector only shows on the thing that you're touching. Once you take your finger off it no longer displays.
So, what I've ended up doing is having a View at the bottom of the layout (so it appears on top) with a background set to a selector.
The selector only has anything for state_selected.
The OS handles the rest.
Not exactly the nicest, but it works...

Related

Android CAB listview state_selected and state_pressed

I have a problem: I have a ListView with a MULTI_CHOICE listener that opens up the ContextualActionBar. For each item in this list I've setted a backround on the root layout. This is the XML:
<item android:drawable="#color/light_blue_alpha"
android:state_activated="true"/>
<item android:drawable="#color/light_blue"
android:state_pressed="true"/>
<item android:drawable="#drawable/card_background"/>
In this case the default background is a card layout (basically white). When the item is pressed the background is a full light blue. When activated (through CAB choice) the background is a ligh blue with 0.5 alpha.
This is what is happening:
I start the CAB. If I select a new item during the CAB, the item changes its background to light_blue (while pressed). When I release the finger it changes to the default background card_background. A few milliseconds later it gets activated and then changes to light_blue_alpha
So basically I'm getting this kind of flickering blue-white-blue. What I want to achieve is to "remove" the white background transaction. I see for example that Gmail app does exactly what I want to do.
How can achieve that? Thank you!!
EDIT:
I tried moving all to a root listSelector. This is the selector:
<item android:state_pressed="true" android:drawable="#color/light_blue"/>
<item android:state_activated="true" android:drawable="#color/light_blue_alpha"/>
<item android:drawable="#android:color/transparent"/>
I also set drawOnTop to true, and the pressed background is correctly applied. But the state_activated doesn't! When I start the CAB, the item returns to its default background. Why doesn't it take the state_activated background??
take one variable in adapter named as selected_position, assign selected position value in this variable.
call notifydatasetchanged method for adapter
in get view method put condition like
if(selected_position == position)
{
change back ground color of list item
}
else
{
set default background color
}
Hope this helps

Hide exapandable list view icon with zero child size

I am developing an app which uses custom expandable list view. My icon changes when i click the list view. I don't want to add group indicator icon when the child size is zero. Please help
Thanks in advance
This is of great help.
Code Snippets:
For state_empty, you can set a different image which is not
confusing, or, simply use transparent color to display nothing...
Add this item in your stateful drawable along with others....
<item android:state_empty="true" android:drawable="#android:color/transparent"/>
So, your indicator can be like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true" android:drawable="#android:color/transparent"/>
<item android:state_expanded="true" android:drawable="#drawable/my_icon_max" />
<item android:drawable="#drawable/my_icon_min" />
</selector>
Explanation:
android:state_empty: - the element does not have any child
android:state_expanded: - state when it is expanded
android:drawable: - normal state, that is the cell has children but its not expanded
set the indicator like this:
getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.indicator));

how to can i get the view which changes color of only selected item of list

I am developing one app and in that i have one listview. In listview i want to change color of selected items only. It means if i clicked on 1st item it should change color and then again i click on 2nd item then color of 1st item will become normal and it will change color of 2nd item. Here i am using custom listview. and here position is selected item and CommonUtilities.getListPosition() is globally defined method for storing position. I am able to change color on select but when i click on 2nd item color doesn't change to its previous color.
if (position == CommonUtilities.getListPosition()) {
v.setBackgroundColor(Color.CYAN);
}else{
v.setBackgroundColor(Color.WHITE);
}
Apply the listSelector in the xml in the ListView like this:
android:listSelector="#drawable/list_selector"
And create list_selector.xml file in drawable folder with following content:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/row_not_selected" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#color/row_selected" android:state_pressed="true" android:state_selected="true"/>
</selector>
and create two colors in the colors.xml with names row_not_selected, row_selected.
Thats it.Thanks.
Please let me know if anything else required.
ListView item background via custom selector.
This link will help you. Use a selector. Specify different color when pressed and in normal mode. I hope this is exactly what your looking for.
when you click a row in listview u can change the color of the listview on click. Then on release change back to normal state. Why do you want to change the 1st row color when you click 2nd row?. Each row in listview remains in normal state until it is clicked. On release goes back to normal state. The above link helps you achieve the same.

GridView children don't display focus

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.

Android Custom Image Button Does Not Stay Selected

I am currently using an ImageButton, and I want to have the effect like radio button once you select it stays selected, until someone picks another image button. I have setup custom selector like below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/buttonimagesel" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/buttonimagesel" /> <!-- focused -->
<item android:drawable="#drawable/buttonimage" /> <!-- default -->
</selector>
But this just shows the selected image for as long as the key is pressed down. The effect i want is for it to stay selected like a radio button until the request is processed after which the whole activity including the button is redrawn. So I want one click to put the button in a selected state and unclick does not change this. Also I do not want the other buttons to be selectable after this happens, and I don't certainly don't want them to change images or anything like that.
Thanks
If you need to use an ImageButton, you can add an android:state_selected="true" item and use setSelected() in your onClick() logic. You would have to take care of deselecting all the other buttons when selecting a new one. This question might be useful: Android ImageButton with a selected state?
However you could also just use RadioButtons and customize their look (with android:background and android:button - these and all CompoundButtons have a checked state that work in a toggling way).

Categories

Resources