How to show a custom shape as background behind the selected item inside a recyclerview. I've tried several things but all failed:
activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/nav_drawer_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/drawer_background"
android:theme="#style/drawer"></android.support.v7.widget.RecyclerView>
</LinearLayout>
drawer_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_shape_selected" android:state_checked="true"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
drawable_shape_selected.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFB74D"></solid>
</shape>
custom_layout.xml:For RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/TextViewDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
</LinearLayout>
Update:
itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
Toast.makeText(context, "Position " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
Change your drawer_background.xml like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_shape_selected"
android:state_pressed="true" />
<item android:drawable="#drawable/drawable_shape_selected"
android:state_focused="true" />
<item android:drawable="#android:color/transparent" />
</selector>
Also add android:focusableInTouchMode="true" to the linear Layout of custom_layout.xml
This is because by default when the view is touched it will take click instead of focus.If you want view to be focused and change its background when pressed add this to your view i.e LinearLayout in xml.
EDIT:focusableInTouchMode makes the view touch mode for the first time disabling click.So to make button click happen for the first time add focus listener in java class for the button and onFocuschange add view.performClick() to make button takes click.
Button button = view.findViewById(R.id.buttonPanel2);
button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean focus) {
if(focus){
view.performClick();
}
}
});
Related
I followed steps on other places, and I made an xml file like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/yeah2"
android:state_pressed="true" />
<item android:drawable="#drawable/yeah" />
My activity xml for my button looks like this:
<ImageButton
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:onClick="play1"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:src="#drawable/yeah"
android:clickable="false"
android:nestedScrollingEnabled="true"
android:background="#drawable/highlight" />
How to assign highlighted state image to an Android Image Button ?
make xml like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Remember: order is important. First matching state(s) is rendered) -->
<item
android:state_selected="true"
android:drawable="#drawable/yeah" />
<item
android:drawable="#drawable/yeah2" />
Then in Java do following
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.ImageButton));
//set the click listener
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View button) {
//Set the button's appearance
button.setSelected(!button.isSelected());
if (button.isSelected()) {
//Handle selected state change
} else {
//Handle de-select state change
}
}
});
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>
I have a ListFragment that contains a Button in every list item. When the Button is clicked, the colour of the Button changes (using a Drawable xml resource). Though the color of the Button changes during the click event, its color changes back to its default value after the event, i.e the color change is momentarily observable during the click and not afterwards. What I'd really like to happen is for the color change to persist after the event fires.
I have tried an approach to manipulate item views via the getView(int position, View convertView, ViewGroup parent) method of the ListFragment's custom adapter (a SimpleAdapter in my case) where I could store positions of the items and persist the background state of every Button. However I would like to replicate this solution using selectors.
I have two Drawable resources setting colours for the pressed and clicked states of each Button as well the selector resource that manages those states.
Here is my drawable resource that manipulates color states:
<?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/clicked_drawable" /> <!-- pressed -->
<item android:state_focused="false" android:state_pressed="false"
android:drawable="#drawable/clicked_drawable" />
<item android:drawable="#drawable/unclicked_drawable" /> <!-- default -->
</selector>
And the two resources that contain the different colours, clicked_drawable.xml & unclicked_drawable.xml, in order:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#F08080"
/>
</shape>
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#FFB00F"
/>
</shape>
And the layout file for the ListFragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:descendantFocusability="blocksDescendants"
android:layout_margin="5dp"
>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label"
android:gravity="start"
/>
<TextView
android:id="#+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
<Button
android:id="#+id/button_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button"
android:layout_margin="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:clickable="true"
android:background="#drawable/selector_drawable"
/>
</LinearLayout>
I am unable to solve this and any direction, guidance or a solution will be most appreciated.
Add the following code to your Adapter class' getView() method:
#Override
public void getView(int position, View view, ViewGroup parent){
final Button b = (Button) view.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
b.setBackgroundResource(R.drawable.clicked_drawable);
.....
.....
}
....
....
}
Try this. It will work.
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" />
And make your selector like this, set all the states to the same drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg_normal" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_normal" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
</selector>
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
}
I am having problems defining a ListView with a custom adapter and list item xml layout. The problem is that my ListView is not highlight items when pressed. I am doing the following:
I define a drawable selector xml resource 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="#android:color/holo_orange_light" />
<item android:state_pressed="false" android:drawable="#android:color/white" />
</selector>
I define an item layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn_voice_item"
>
<ImageButton
android:id="#+id/voiceItemFavorite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#drawable/btn_favorite" />
<TextView
android:id="#+id/voiceItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/voiceItemFavorite"
android:background="#drawable/btn_voice_item" />
</RelativeLayout>
Finally I wrote code in my custom data adapter to inflate and populate then view.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.voice_item, null);
}
VoiceItem i = getItem(position);
TextView t = (TextView) convertView.findViewById(R.id.voiceItemText);
t.setText(i.toString());
ImageButton b = (ImageButton) convertView.findViewById(R.id.voiceItemFavorite);
b.setTag(i);
b.setSelected(i.favorite);
b.setOnClickListener(new FavoriteClick());
return convertView;
}
Everything works except when I press items in the list there is no visual indication of the press. I expect the color of the pressed item's background to transition to the style defined by my drawable selector.
How can I both have a custom view items and retain styling highlighting of items while pressed?
If android:background="#drawable/btn_voice_item" is solid image so it'll cover the ListView selector. Here is very detail tutorial about custom ListView selector and item backround
ListViewTipsandTricks part3 (Cyril Mottier)
this is the xml i use. note that this is gradient. but put this in your #android:color/holo_orange_light
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#18d7e5"
android:centerColor="#16cedb"
android:endColor="#09adb9"
android:angle="270" />
</shape>
this is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
and this is what i have in the list xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
perhaps you find this useful and can work out your problem