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.
Related
I'm selecting
lst_Center.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
and adding a listView:
android:choiceMode="singleChoice"
and LinearLayout:
android:background="#drawable/list_selector"
And adding a list_selector.xml:
<item android:drawable="#color/Blue" android:state_enabled="true"/>
<item android:drawable="#color/Red" android:state_pressed="true"/>
But it's not working, how can I fix it?
Create list_background.xml in drawable folder and add below lines.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/anycolor" />
<item android:state_focused="true" android:drawable="#color/anycolor" />
<item android:drawable="#color/anycolor" />
</selector>
Set background for your ListView.
android:background="#drawable/list_background"
If you want to highlight only the selecting list item means then you should set the background for textview. For that just go through about custom adapter.
Hope it helps you.
I have an image for my button.
In order to make use of it I have to have 1 additional image for each state:
1. disabled
2. selected
3. pressed
etc..
in iOS all those additional states are handled automatically and deferred from original image provided.
Is it possible to accomplish this for Android?
Is it possible to accomplish this for Android?
No, It is NOT. You need to have all states's images with you to define the selector
You can define button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state -->
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<!-- Pressed state -->
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<!-- Disabled state -->
<item android:drawable="#drawable/button_bg_disabled" android:state_enabled="false"></item>
<!-- Normal state -->
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
Then simply assign this selector as a background of a button
<Button
android:id="#+id/button1"
android:background="#drawable/button_selector"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Refer : Button Selector
Hope it will help you ツ
For selected and pressed you can use an xml file with root for button's background but for disabled I guess you need to handle it in java code. I'm not sure about disabled state.
You will have to use selector for android.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/focused"
android:state_focused="true" />
<item android:drawable="#drawable/normal" />
</selector>
android:background="#drawable/selector_button" />
just have a look of this link.
As far as I know you need to handle this states in a new resource file called cursor: e.g:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/edittext_selector" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Image display in background in select state -->
<item
android:state_pressed="true"
android:drawable="#drawable/edittextback1">
</item>
<!-- Image display in background in select state -->
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/edittextback2">
</item>
<!-- Default state -->
<!--<item android:state_enabled="true"-->
<!--android:drawable="#drawable/your_ninepath_image">-->
<!--</item>-->
</selector>
This answer refers to how you can handle button states in Android
You can create a button layout file separately in put that in res > layout folder
For Instance:
if the layout file name is 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:drawable="#drawable/btn_normal"/>
</selector>
You can set BackgroundResource of a Button
yourButton.setBackgroundResource(R.layout.btn);
EDIT
In addition you can refer to this link which is more closer to answer your question
I want to change the background colour of my StaggeredGridView item when pressed, but currently I am getting an ugly gingerbread orange by default, as shown
here. I tried setting the gridview items background as a selector, but when I did that, if I click one item, all the items' background colours are changed.
<!-- My selector -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/holo_blue_light"
android:state_pressed="true" />
<item android:drawable="#android:color/transparent" />
</selector>
And this was in my StaggeredGridView, but it didn't help:
<!-- In StaggeredGridView -->
android:listSelector="#drawable/selector"
By the way, I am using this StaggeredGridView Library. Thanks in advance!
at the implementation of staggeredGridViews onClick event:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for(int i=0;i<mGridView.getChildCount();i++) {
mGridView.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.yellow));
// set all items to yellow
}
mGridView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.blue));
//set the selected items color to blue
}
hope this helps
The solution was to stay away from any #color or #drawable that refers
to a color inside listSelector. I created two 3x3 pixel .png files.
Each saved with the gamma layer. In my case it's two of the same color
each mixed down in Gimp with a different transparency on the color
layer. So when you select an item you get an overlay with 25% color,
and when you press it you get a png with 50% color. I put them in my
drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png
<?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_pressed="false"
android:drawable="#drawable/bg_list_item_highlighted" /> <!-- #drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/bg_list_item_pressed" /> <!-- #drawable/tab_press -->
</selector>
then
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"
you should change a line in that library and than make your own selector
1) in the library there is a class called: StaggeredGridView
there is a method in that class called: useDefaultSelector()
inside that comment the setSelector line and add this line:
setSelector(getResources().getDrawable(R.drawable.selector));
2)make an xml file inside drawable folder in the library as follow: selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/yourColor1" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/yourColor2" />
<item android:state_focused="true" android:drawable="#color/yourColor3" />
</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>
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" />