Inflated listview item don't appear as pressed - android

Sorry if this is too trivial or if it had been previously posted. Right now, I'm with this issue. I have a ListView component where I'm creating a set of items, each one being inflated layouts. When I click in one item, the behavior onClick is full functional and the listeners are doing their job, but the ListView don't show the "pressed status" in the buttons. The below image shows what I'm seeking for:
Does anyone have a hint to give me? I already checked and tryed a couple of things:
1. android:state_focused="true"
2. android:state_pressed="true"
3. setClickable(true)
Thanks in advance!

You have to manually apply styles to the states of the list item. Because, you are creating your own list item view instead of android list item view. so, you have to provide specific styles to show the different states.
Use selector to make the style to the list item. some sample code i am provided here,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:drawable="#drawable/list_item_gradient" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>
And in the listview
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="#drawable/list_selector_background" />

Related

Listview Selector

I have a State List Drawable xml
<item android:state_pressed="true" android:drawable="#drawable/list_selector_pressed" />
<item android:state_enabled="false" android:drawable="#drawable/list_selector_disabled" />
<item android:drawable="#android:color/transparent" />
Whenever I select the row in the listview I want it to show my pressed image and then when it releases it shows the transparent background (normal). However, it always shows my disabled image after releasing. I need to have the disabled image whenever I disable a row (grey). Any ideas what i'm doing wrong? Also, is there a way to capture the different states (focused, pressed, enabled) to have a better idea what is happening behind the scenes, this might help me in understanding what is happening?
This example should be a good help.
Here's how XML should look like:
<?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/listitem_pressed" />
<item android:state_focused="true" android:drawable="#drawable/listitem_selected" />
</selector>

Android Listview Listitem state color

I've the following problem: I made a custom state selector for Android Buttons. They work like expected. The problem begins when I want the same states to apply to a ListView item row.
So I set the style in the Adapter and now I discover that the same state looks different on the listbox view item. It is more of a blend between the default style and the color I want to set. When I long press the item or release the item, I notice that it fades away first into the default color (blue) and then transparent.
I guess I do miss some state/state combination, but I can't figure out which. I tried a lot, also examples from StackOverflow, but nothing works. Hopefully someone here had the same challenge and has a correct override. Below is my drawable selector.
Thanks in advance!
XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime"
android:enterFadeDuration="#android:integer/config_shortAnimTime"
>
<item android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#android:color/transparent" />
<item android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#android:color/transparent" />
<item android:state_window_focused="false"
android:state_focused="true"
android:drawable="#android:color/transparent" />
<item android:state_pressed="true"
android:drawable="#android:color/holo_green_light" />
<item android:state_focused="true"
android:state_pressed="false"
android:state_enabled="true"
android:drawable="#android:color/holo_green_light" />
<item android:drawable="#android:color/transparent" />
</selector>
In touch mode there is no such thing as selected or focused when you are not pressing on an item.
But you can work around it. You need to set choice mode for the list (single or multiple, the default is.none) and then use state_activated in your selector.

Android:Styling ListView

I want to put some style in the ListView in such a way that when an item of ListView is hovered, the color of that list item changes. I dont have any idea about how to achieve this.
Please suggest some other ideas about styling the ListView as well.
Regards,
Use selector to define what should happen when you focus the item, select the item and press the item, I doubt you cannot hover over a view, since Android devices use touch screen interface and not a pointing interface like a mouse..
This should help you:
Create a XML file in drawable folder named listselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/blue" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/blue" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/green" /> <!-- pressed -->
<item android:drawable="#color/white" /> <!-- default -->
</selector>
Then set the background of your TextView which you use in ListView to this like
android:background = "#drawable/listselector",
that should do it.

Android: ImageButton not showing states pressed

I have an ImageButton and I swear I have everything set up right but it is not showing the different icon when a state is pressed. Here is what I have.
ImageButton
<!-- HOME BUTTON -->
<ImageButton
android:id="#+id/home"
android:layout_width="45dp"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:contentDescription="Home"
android:src="#drawable/title_home"
android:layout_alignParentLeft="true"
android:onClick="onClickHome" />
Title Home
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/title_home"/>
</selector>
And yes these are all valid images, and they are different. title_home is the only one that is showing up.
I would appreciate any ideas?
Notice how you are referring to #drawable/title_home in the first block of code, but you are intending to refer to the xml file. Although in the second block of code you are referring to another #drawable/title_home which I am assuming is an image.
Because of this I can only assume that from your information your first block of code is reading the image and not the xml file. To fix this, I believe changing the names of one of them would help.
android:src="#drawable/title_home"
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/title_home"/>
Same name. Change your xml file name to title_home_btn_selector.
Then make the first one as
android:src="#drawable/title_home_btn_selector"

Android: Custom radiobuttons, is it possible to layer images?

I'm making some Custom Android RadioButtons following this tutorial:
http://blog.devminded.com/posts/custom-android-radiobutton
However.. In my project every button is different meaning I have to make very four almost identical images for every button I have.
Is there any way of having a single file for the state_pressed and focus and using a single file for the unique parts?
The code in question is this:
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/red_focused" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/red_unfocused" />
<item android:state_checked="false"
android:drawable="#drawable/red_img" />
<item android:state_checked="true"
android:drawable="#drawable/red_checked" />
Thanks for any help!

Categories

Resources