Selector for Multi Choice ListView - android

I can't highlight a multichoice list view with custom adapter in android. The activated and pressed state works just fine. What I want is when the user taps on an item it should be highlighted.
my new_list_selector.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/list_selector_pressed" />
<item android:state_activated="true" android:drawable="#drawable/list_selector_selected" />
<item android:state_selected="true" android:drawable="#drawable/list_selector_pressed" />
</selector>
in the above xml, the states for "activated" and "pressed" are working but not "selected".
In my listview_item.xml I set the background to selector as follows
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/new_list_selector">
<!--list items goes here-->
</RelativeLayout>
I define my list view like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/masterContainer">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friendViewList" />
</RelativeLayout>
I get the activated state when I tap and hold a list item. The pressed state works when I tap the list item. I want it to be highlighted when the user taps the item. Can someone help me out on this? Thanks for any input in advance.
PS: I prefer xml solution over coding. I have also tried selector with state_focused set to true with no luck

Do the following:
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_list_selector"/>
</RelativeLayout>
This will set the selector on ImageView. You can also do it with your two TexTviews.

Related

Android - Button's selector does not working when in toolbar

I'm trying to set a selector to a button which included in a toolbar. The selector is working when it sets to a button outside to the toolbar. Any help?
Toolbar
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/light_red"
android:elevation="4dp"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<ImageButton
android:id="#+id/btnSync"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/sync"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sinchronize_q" android:state_focused="true" />
<item android:drawable="#drawable/sinchronize" />
</selector>
The problem is in your drawable in particular this part
android:state_focused="true"
your ImageButton not getting focus because it is taken by its parent or some EditText view. In general state_focused used not quite often in drawables,
change it to state_pressed and it will work so here is what you need
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sinchronize_q"
android:state_pressed="true"/>
<item android:drawable="#android:drawable/sinchronize"/>
if you want to make sure that your item was not getting focus you can add android:focusableInTouchMode="true" to your ImageButton

ListView item not changing background

I'm trying to change the background of a listview item when it's pressed, and despite having tried what has been mentioned in other SO posts the item refuses to change background when clicked.
What I'm I doing wrong?
EDIT: What I want to achieve:
This is how the default listview reacts when clicking an item (talking about the background change), but since I'm using custom list elements layout the list doesn't respond to clicks at all and is very dull, so I'm trying to achieve similar behaviour with the background changing whenever I click a list element.
I have this simple selector:
selector.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/selected_state" />
</selector>
selected_state.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/darkblue" />
</shape>
I tried setting it to the selector of the listview and making the list view single choice
Main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp">
...
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/listViewCentral"
android:choiceMode="singleChoice"
android:listSelector="#drawable/selector" />
</LinearLayout>
...
</RelativeLayout>
List view item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/selector">
...
</RelativeLayout>
And I used OnItemClick listener to set the state to selected
Main activity:
ListView list = (ListView)findViewById(R.id.listViewCentral);
list.setAdapter(stringAdapter);
list.setOnItemClickListener(new TsClickListener());
Listener:
public class TsClickListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
}
}
Logcat is empty (no errors)
try adding the property android:state_focused
<?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/selected_state" />
<item android:state_focused="true" android:drawable="#drawable/selected_state" />
</selector>
The problem after all was the OnItemClick event wasn't being fired at all, and the solution was to add the 'clickable' attribute to the RelativeLayout of my list view element's layout and set it to true, everything works fine then.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true" <-- Problem
android:background="#drawable/selector">
...
</RelativeLayout>

How to change rectangle colour when user touches it

I place a rectangle on an Android screen by placing the drawable as the background for a RelativeLayout. The following is how I achieve it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="150dp"
android:background="#drawable/welcome_rectangle"
android:id="#+id/rectangular_orange"
android:layout_centerInParent="true"></RelativeLayout>
</RelativeLayout>
The following is the XML drawable for welcome_rectangle.
<?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/green_welcome_rectangle" />
<item android:drawable="#drawable/orange_welcome_rectangle" />
</selector>
I want the colour of the rectangle to change when the user touches it. This hasn't been achieved with the selector written. How can I change the selector code so that I can get what I want?

Android's selector for selected item does not work

I have a listview, where I want to highlight selected items in a custom way. I'm setting every item properties in the adapter's getView method, including itemView.setSelected(true).
The main layout defines the listview in the following way:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:listSelector="#drawable/list_selector" />
(Playing with choice mode does not help either).
The list_selector is an almost empty stub:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#android:color/transparent" />
</selector>
I don't need specific styles for listview as a whole, so I'd leave a default one, but according to this answer, we need a selector for a listview for item selector to work. Anyway, without the list_selector the problem remains.
The listview item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#drawable/listitem_background"
android:orientation="vertical">
and it references the following listitem_background selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:color/white" />
<item android:drawable="#android:color/transparent" />
</selector>
The problem is, that selected items do not have white background.
If I change android:state_selected="true" selector in the listitem_background to, for example, android:state_pressed="true", then the selector starts to work, that is item background becomes white if an item is touched.
So, I suppose, there is an error either in the selectors, or in the way how I select items.
I can write a workaround by setting background from Java or utilizing checkable states, but I want to understand and fix current problem with selectors. Thanks in advance.
What about adding this line in your item view?
android:background="?android:attr/activatedBackgroundIndicator"
For instance:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewListRowOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>
Then the selector looks something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_selected" android:state_activated="true"/>
<item android:drawable="#drawable/list_default"/>
</selector>
And the list view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewMainFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_item_selector"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>
Try this one to change the selected list item.Put it inside the list view.
if (currentSelectedView != null && currentSelectedView != v) {
unhighlightCurrentRow(currentSelectedView);
}
currentSelectedView = v;
highlightCurrentRow(currentSelectedView);
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundColor(Color.TRANSPARENT);//to set a color for un-seclected row
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.drawable.selector_img);//to set a color for seclected row
}

Customizing Android ListView Colors?

I've scoured the interwebs and found many posts regarding how to change the colors of a list view using a list selector. However it does not seem to work for me. So my question would be what am I doing wrong?
When I use the below files I get a list where all item backgrounds are initially blue. (I expected white)
When I move the focus up and down the text just changes to a dark grey and the item backgrounds are still blue. (This is when I would expect a single row to be blue with the rest white)
When I click on a row the background of the row I clicked on turns black and all other rows turned green. (I expected to have the row I clicked turn green and the rest be white)
Here is my main layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="#drawable/item_selector"
/>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty"
/>
</LinearLayout>
Here is my list item file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textStyle="bold"
android:padding="7dip"
android:textSize="18sp"
android:layout_toRightOf="#id/checkbox"
/>
</RelativeLayout>
Here is my colors file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff00</color>
<color name="green">#f0f0</color>
<color name="blue">#f00f</color>
<color name="white">#ffff</color>
</resources>
Here is my selector file:
<?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/green" />
<item
android:state_focused="true"
android:drawable="#color/blue" />
<item
android:drawable="#color/white" />
</selector>
Hopefully it's something dumb and simple that I'm doing wrong.
Thanks in advance.
I think that android:listSelector="#drawable/item_selector" is applied only on the root of the view not at all his subchild.
Try to add #drawable/item_selector as a backgroud of your RelativeLayout with android:background="#drawable/item_selector" and put a transparent layout to the listSelector:
android:listSelector="#android:color/transparent"
Actually if you want to do it programatically without the use of resources, you can set your element color. For example, to set text color #F123456 in a TextView:
objTextView.setTextColor(color.parseColor("#F12345"));
This is handy if you want to dynamically set a color in a list view that is user selectable, perhaps stored in a database where you simply cant use a static resource file for color lists.

Categories

Resources