Android CAB listview state_selected and state_pressed - android

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

Related

Android. How to programatically change background color of button

I need to create Button programatically. And my button can be randomly colored. However, I need to set the background color of the button and also the background color when the button is in a state PRESSED.
That is, the background color of the button in its normal state should be different from the background color when the user pressed the button.
For example, when I need a different background for a button, depending on the state, I use a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_not_pressed" android:state_pressed="false" />
<item android:drawable="#drawable/shape_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/shape_not_pressed" />
</selector>
And in my xml for button:
android:background="#drawable/selector_button"
This is a working solution if you know the background you need in advance. However, in my case, I am dynamically getting the background for the button (pressed_bg and not_pressed_bg) and therefore this method does not work for me. Is it possible to implement this programmatically?
P.S. I need pressed effect
Please, help me.
You can programaticaly check and set the colors like this.
if(someButton.isPressed){
val pressedRandomColor = getPressedRandomColor()
someButton.setBackgroundColor(pressedRandomColor)
}else {
val randomColor = getRandomColor()
someButton.setBackgroundColor(randomColor)
}
The logic for getPressedRandomColor() and getRandomColor() you need to define by yourself, it should return Int value.
You can create a color code list and pass the color code randomly on a button click to set button background color.
yourButton.setBackgroundColor(Color.parseColor("#0060AB"));

ListView ContextMenu Active State Color

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

GridView item selector checked state not appearing

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...

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.

Button draws normal state after pressing

I'm new to Android and just starting the very basics. I implement my custom button skin using .9.png images for norma/focus/pressed states. It works fine, but I noticed that after a pressed the focussed button it visually "lost" focus and draws the normal state frame. I planned to use different state images to highloght what button is selected right now, but it seems that it would not work. I noticed also that the same happens with the default LAF button. Is it OK, or it's just emulator issue? What the good workaroud can be used?
Thanks
I think the following may help. I wanted to have one of the buttons in a list of button to be coloured differently, to highlight the fact you were already in that section.
My buttons android:background field was set to the following drawable (drawable/my_btn.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="true" android:drawable="#drawable/btn_focused" />
<item android:state_selected="true" android:drawable="#drawable/btn_selected" />
<item android:drawable="#color/transparent" />
</selector>
You'll noticed i've got an item with the android:state_selected="true" attribute set.
Then in code you can have
Button mybtn = (Button)findViewById(R.id.my_btn_1);
mybtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Button btn = (Button)findViewById(R.id.nav_secondary_1);
btn.setSelected(true);
}
});
I'm not sure if you can set the selected stat of a Button through a property in the xml. Not sure you would want to.
The order of the item's are also important as it can change the visibility of the other states. The current order will allow you to see the pressed and focused states. however, if you moved the selected item to the top you would find that your pressed and focused states would not be displayed.
I am not sure if you can combine the pressed, focused and selected states to allow for more customised graphics. I haven't tried it but the following would allow for more complicated state based graphical layouts.
<item android:state_selected="true" android:state_focused="true" android:drawable="#drawable/btn_selected_focused" />
Read up on Selectors here http://developer.android.com/guide/topics/resources/drawable-resource.html
This is the default behavior in touch mode, and you should not seek to tamper with it. This is how your users will expect for your app to behave. If you set the focus without touching the screen, such as when using the trackball that's available on most devices, it will indeed remain in focus, but in touch mode there's no visual representation for the state of having focus.

Categories

Resources