I'm trying to get my ListView to have three background color states.
normal
pressed (when the user's finger is touching)
context menu active (when the context menu appears for a specific item)
note: I don't have a need for a selected state when the context menu is NOT active.
Basically, the part I'm having a hard time with is #3. When a user long-presses a listview item, I want the context menu to show, but I also want the item they pressed to highlight a different color.
In code, I have this
// note: the names are just to tell you what view type we're dealing with.
// android_Widgit_ListView: the ListView
// android_Views_View: the Cell
if (android_Widgit_ListView != null)
{
android_Views_View.Background = ContextCompat.GetDrawable(context, Resource.Drawable.listview_selector);
android_Widgit_ListView.SetSelector(Resource.Drawable.listview_selector);
android_Widgit_ListView.CacheColorHint = Color.Transparent.ToAndroid();
}
And I have a selector (note the different colors just to see what's up)
<!-- listview_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/PrimaryMediumBlue"/>
<item android:state_activated="true" android:drawable="#color/PrimaryYellow"/>
<item android:state_selected="true" android:drawable="#color/SecondaryWarmGrey"/>
<item android:state_checked="true" android:drawable="#color/SecondaryGreen"/>
<item android:state_pressed="true" android:drawable="#color/SecondaryLightGrey"/>
<item android:drawable="#color/white"/>
</selector>
The problem is that when I long-press the item to bring up the context menu, the list view item returns to white.
How do I get that state to stay a different color?
Its a bit of a workaround but...
You could bind a color changing function to a property in the context menu.
ex: a bool called "active" that you set "OnAppering" and "OnDisapering".
Implement INotifyPropertyChanged and fire an event when the active property is changed.
and then listen for the event in the listview and change the color on that.
-
i am not sure this is the best way...
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:colorLongPressedHighlight">#color/ListViewHighlighted</item>
</style>
<color name="ListViewHighlighted">#A8A8A8</color>
add this on Resourses>Values>Styles.xml
Related
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
in my listview, when user long press on a item, i draw a custom actionbar and provide user option to delete multiple items at a time.
by default if i perform long press action, i will get selection color as blue and it gets disappear.
To overcome i tried adding a selector like this.
listviewselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/BLUE" />
</selector>
If i set this selector, when user performs long press i can see red color, but after wards if user performs selection, no color is getting retained on item. By default it looks white.
I tried setting background color based on condition like below
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(REDCOLOR);
}
else
{
convertView.setBackgroundColor(WHITE);
}
In this case, if user performs long press and then if user select multiple items i could see red color and by default all the items color will be white. But if user touches any item the default color will be nothing i.e no color appears on selection.
How to get default white color, on tap blue color and upon multiple selection red color?
i tried like this
New selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/WHITE" />
</selector>
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.RED));
}
else
{
convertView.setBackgroundColor(R.drawable.listviewselector);
}
In this i get by default all items blue color. why?
i tried like this, it worked.
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.BLUE));
}
else
{
convertView.setBackgroundColor(android.R.drawable.list_selector_background);
}
If I have a color list called color_list_1 like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FF0000"/>
<item android:color="#00FF00"/>
</selector>
I would like to be able to create another color list called color_list_2:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0000FF"/>
<item android:color="#color/color_list_1"/>
</selector>
What this allows me to do is have a control, Foo, which uses color_list_1 for a color. Now I may want to create a subclass called AmazingFoo which I want to keep the same look as Foo except I also want to add a pressed color to it.
I have tried this but it seems when using color_list_2 as the color, the states are ignored inside color_list_1. This means the pressed state works (because it is in color_list_2 directly) but when it falls back to color_list_1 the focused state is always false so the default color is returned.
I know it isn't a problem with the control because using color_list_1 works perfectly. It is only when I cascade it inside another colorlist that all of the states return false.
I have a toggle button with the following drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/ic_heart_pressed"/>
<item android:state_pressed="true" android:drawable="#drawable/ic_heart"/>
<item android:state_checked="true" android:drawable="#drawable/ic_heart_pressed"/>
<item android:drawable="#drawable/ic_heart"/>
</selector>
This is working fine. However, since this is a favorite button when I present the screen second time and it contains some items that are favorite to begin with, I want to change the default from heart to heart_pressed
I tried doing it programmatically like this in getView of my ArrayAdapter, however, with this I lose the "toggle" capability of the button.
if (item.isFav())
holder.hButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_heart_pressed));
else
holder.hButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_heart));
Question
Is there a way to change the default of a ToggleButton programmatically based on a condition in the code?
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...