The code below is about making custom list view, the adapter create for each item a view "list_view_child" and in the getView method there's a for loop composed 3 repetition, in this for loop we add 3 new views "list_item" on "list_items_container" using method addView(View v), the "list_item" has a selector to change the background of "list_item" view and here is the problem. if I click on one of these views their backgrounds change together.
Display these images to understand the problem well :
Pressed_false : http://i44.tinypic.com/2jfaqzb.png
Pressed_true (if "Item 2 & View : 1" Or "Item 2 & View : 2" Or "Item 2 & View : 3" pressed) : http://i42.tinypic.com/2l97ayw.png
list_view_child XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d2d2d2"
android:orientation="vertical" >
<TextView
android:id="#+id/child_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="#ff0000"
android:text="Test" />
<LinearLayout
android:id="#+id/list_items_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
</LinearLayout>
</LinearLayout>
list_item XML Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:orientation="horizontal"
android:background="#drawable/selector"
android:layout_marginTop="5dp" >
<TextView
android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="#ffff00" />
</LinearLayout>
Selector:
<?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/pressed_true" />
<item android:state_pressed="false" android:drawable="#drawable/pressed_false" />
</selector>
pressed_true :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000" />
</shape>
pressed_false :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#0000ff" />
</shape>
List View Adapter Class :
public class ListViewAdapter extends BaseAdapter {
private Activity activity;
private HashMap<String, String> data;
public ListViewAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
HashMap<String, String> datapos = new HashMap<String, String>();
datapos = data.get(position);
LinearLayout listitemcontainer = (LinearLayout) vi.findViewById(R.id.list_item_container);
for (int i = 1; i <= 3; i++) {
View listitem = inflater.inflate(R.layout.list_item, null);
TextView itemtitle = (TextView) listitem.findViewById(R.id.item_title);
itemtitle.setText("Item : " + position + " & View : " + i);
LinearLayout.LayoutParams listitemparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
listitemparams.setMargins(0, 15, 22, 0);
listitem.setLayoutParams(listitemparams);
listitemcontainer.addView(listitem);
}
}
}
If your views don't have click listener, by default they will take the selection state of their parent. To prevent that, you can use this property on your list_item:
android:duplicateParentState="false"
Related
I would like to have a Listview in which the rows:
have rounded corners;
are partially colored (the left part of each row);
contain a shape in their colored area which is filled with another color.
Here is an example from another app:
I currently have two leads but I did not succeed with any of them.
My first lead (shape)
I found how to get rounded corners by using a shape and use it as a background for the Listview.
The drawable shape file "rounded_rectangle.xml":
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
The activity file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
// ...
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/splitView"
android:id="#+id/lv_player"
android:layout_alignParentTop="true"
android:background="#drawable/rounded_rectangle"/>
// ...
</FrameLayout>
However, I don't know how to use a shape to have a row partially colored or draw an icon. I can fill the shape with a color but then the rows are completely colored.
My second lead (vector)
I also found that I can use a vector as a background of my Listview. The advantage is that I can draw whatever I want using paths.
An example of a vector file:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<path
android:name="w"
android:fillColor="#000000"
android:pathData="m 290,103 l-200,0 c -50,0 -50,-100 0,-100 l200,0 z"
/>
</vector>
I can easily create a path for the colored area of my rows and another for the icon. However, now the problem is that I don't know how to scale the size of the vector to the size of the line.
Do you know what would be the best option and how can I obtain the expected result?
Thanks in advance.
Consider using a CardView, it will help you to get the rounded corners. Inside the CardView create a RelativeLayout in order to get the rest of the stuff you've mentioned above.
Here is a good tutorial : Using the CardView | CodePath Android Cliffnotes
First of all you need to create your fancy item separately like having xml layout contain the description of the custom item :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/optionName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_weight="10"
android:fontFamily="sans-serif-medium"
android:text="#string/option"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
<CheckBox
android:id="#+id/activated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
than create your adapter that will make your list have your layout as an item
public class ListOptionsAdapter extends BaseAdapter {
List<OptionsObject> Features = new ArrayList<OptionsObject>();
LayoutInflater inflater;
Context context;
public ListOptionsAdapter(List<OptionsObject> features, Context context) {
super();
Features = features;
inflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public int getCount() {
return Features.size();
}
#Override
public OptionsObject getItem(int position) {
return Features.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list_view, parent, false);
holder = new ViewHolder();
holder.optionName = (TextView) convertView.findViewById(R.id.optionName);
holder.isActivate = (CheckBox) convertView.findViewById(R.id.activated);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView optionName;
CheckBox isActivate;
}
}
after that as above i specified the name of the layout i will use as item in the method getView() and affect every element to the view holder
NB : in getView you have to specify the values for the elements that they will be displayed or leave them blank if u dont need
after that simply in my activity class i call my listview and my adapter
OptionList = (ListView) findViewById(R.id.optionList);
adapter = new ListOptionsAdapter(Features, getApplicationContext(), this);
OptionList.setAdapter(adapter);
I'm trying to use a keyboard to navigate through a GridView of items.
In the short demo, which only contains a GridView (containing item views with android:focusable="true"), you can see that none of the items gain focus - the grid is the thing that scrolls (the items don't turn orange).
Adding the android:descendantFocusability="afterDescendants" allows each item to be focused first, but still, doesn't continue to scroll the GridView so you can navigate down:
Clicking on an item (with mouse or by pressing Return if the item was focused, after the descendent focusability fix) turns an item blue - indicating it's in pressed state.
#layout/activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" />
#layout/dummy_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dummy_item_parent"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:focusable="true"
android:clickable="true"
android:background="#drawable/item_background" />
#drawable/item_background.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="#android:color/holo_blue_light" />
<item android:state_focused="true" android:drawable="#android:color/holo_orange_light" />
<item android:drawable="#android:color/holo_red_light" />
</selector>
MyActivity.java (with ListAdapter):
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
AbsListView listview = (AbsListView) findViewById(R.id.listview);
listview.setAdapter(new DummyAdapter(getLayoutInflater()));
}
private static class DummyAdapter extends BaseAdapter {
private static final int COUNT = 25;
private final LayoutInflater layoutInflater;
DummyAdapter(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}
#Override
public int getCount() {
return COUNT;
}
#Override
public String getItem(int position) {
return "Item " + position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = layoutInflater.inflate(R.layout.dummy_item, parent, false);
}
((TextView) view).setText(getItem(position));
return view;
}
}
}
Swapping the GridView for a ListView, without changing the adapter, or the item views/item view attributes, will behave as expected - items can be focused and the ListView will scroll to the bottom, so that all items can be focused using just keyboard input. Also, using a RecyclerView with LinearLayoutManager / GridLayoutManager will also work correctly.
I have tried this on API 16 and 22 with the same results. I have tried settings the android:descendantFocusability attribute on the GridView to afterDescendents with the same result.
I tried your code and it doesn't work with GridView, as you said, but i have some issue also with ListView. The ListView scrolls correctly, but only the first item of every page is highlighted.
With these little modifications you can achieve the effect you want on both GridView and ListView.
Remove android:descendantFocusability in activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" />
Remove android:focusable in dummy_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dummy_item_parent"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:clickable="true"
android:background="#drawable/item_background" />
Change from android:state_focused to android:state_selected in item_background.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="#android:color/holo_blue_light" />
<item android:state_selected="true" android:drawable="#android:color/holo_orange_light" />
<item android:drawable="#android:color/holo_red_light" />
</selector>
In my project I have an Activity with a ListView with a BaseAdapter that shows a custom item list layout.
This list item layout has a textview and his text is cut off if with more than one line.
The code that I have written is like this:
Activity's onCreate method:
lv = (ListView) findViewById(R.id.listView3);
listaCompleta = new ArrayList<String>();
listaCompleta.add ("Item1");
listaCompleta.add ("Example text");
listaCompleta.add ("This is an example text for writing more than a line in the listview item row blablabla blablabla blablabla");
adapter = new BaseAdapter () {
#Override
public int getCount() {
return listaCompleta.size();
}
#Override
public Object getItem(int position) {
return listaCompleta.get(position);
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.item_list_layout, null);
TextView tv1 = (TextView) view.findViewById(R.id.testoItemList);
tv1.setText(listaCompleta.get(position));
return view;
}
};
lv.setAdapter(adapter);
item_list_layout.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/item_list_layout">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testoItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"/>
<CheckBox
android:id="#+id/checkBoxItemList"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="#drawable/checkbox_selector"/>
And checkbox_selector.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/star" android:state_checked="false"/>
<item android:drawable="#drawable/star_active" android:state_checked="true"/>
<item android:drawable="#drawable/star"/>
I found online many post as mine but all without solution.
How can I fix this problem?
Try replacing your TextView with this
<TextView
android:id="#+id/testoItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="false"
android:gravity="center_vertical"/>
I think you should change your layour_heigth in the text view and Linearlayout for wrap_content. If textview have more lines de container expand to adjust. I you choose match_parent the size of container set by the listview.
You change this line:
View view = getLayoutInflater().inflate(R.layout.item_list_layout, null);
for this:
View view = getLayoutInflater().inflate(R.layout.item_list_layout, parent,false);
or layout parameters are ignored.
This works fine
Try this. Change
android:layout_height="match_parent"
to
android:layout_height="wrap_content"
for example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testoItemList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"/>
I want to have rows with gradient background in my ListView and i want them to change to a solid color when pressed. But i can't get it right.
I know about the problem ListViews have and i read some solutions in other topics but none of them seems to work for me. Any help would be great.
When i run the following code i get this image that is ok:
But when i press on item 2, all rows will be green!:
Here is my Adapter:
public class MyAdapter extends BaseAdapter {
private final Context mContext;
private ArrayList<String> mMenuItems;
StateListDrawable mBackgroundDrawable;
public MyAdapter(Context context) {
this.mContext = context;
mMenuItems= new ArrayList<>();
for (int i = 0; i < 10; i++) {
mMenuItems.add(String.format("Item %d", i));
}
// make background drawable
GradientDrawable backgroundDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{ Color.RED, Color.YELLOW, Color.RED});
backgroundDrawable.setCornerRadius(0f);
ColorDrawable selectedColorDrawable= new ColorDrawable(Color.GREEN);
mBackgroundDrawable = new StateListDrawable();
mBackgroundDrawable.addState(new int[] {android.R.attr.state_pressed}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_focused}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_selected}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {}, backgroundDrawable);
}
#Override
public int getCount() {
return mMenuItems.size();
}
#Override
public Object getItem(int position) {
return mMenuItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup item = getViewGroup(convertView);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN) {
item.setBackgroundDrawable(mBackgroundDrawable);
} else {
item.setBackground(mBackgroundDrawable);
}
// set title
TextView titleView= (TextView) item.findViewById(R.id.menu_title);
titleView.setText(mMenuItems.get(position));
return item;
}
public ViewGroup getViewGroup(View reuse)
{
if(reuse instanceof ViewGroup)
return (ViewGroup)reuse;
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup item = (ViewGroup)inflater.inflate(R.layout.row_menu, null);
return item;
}
}
My row_menu.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="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/menu_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
</RelativeLayout>
My activity's layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ListView
android:id="#+id/activity_main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
My activity:
public class MainActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.activity_main_listview);
list.setAdapter(new MyAdapter(this));
}
}
mBackgroundDrawable is common for all your Rows. You need to attach a unique mBackgroundDrawable to every row (for example inside your loop where you add it). This is pretty much straightforward, but feel free to ask if you need a specific example ! ;)
You could achieve it through the XML it self.
ListView
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
drawables/list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="#drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
drawables/list_item_bg_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/list_background_pressed"
android:endColor="#color/list_background_pressed"
android:angle="90" />
</shape>
drawables/list_item_bg_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/list_background"
android:endColor="#color/list_background"
android:angle="90" />
</shape>
As you can see the ListView has an attr named android:listSelector and it's value is "#drawable/list_selector". This is where we setting the selector.
and i think the rest is Self Explanatory.
If you need more clarification, read this http://javatechig.com/android/android-listview-tutorial
i got the following problem.
i have a ListView with custom rows consisting of an imageview and a textview.
the textview's xml code is
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:textColorHighlight="#FEC403"
/>
then i have an itemclicklistener that works fine and i want to highlight the textview that has been clicked by doing the following.
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
//TODO: ACTIONS
String pathtofile = (String) adaptview.getItemAtPosition(position);
View rowview = (View) adaptview.getChildAt(position);
rowview.setSelected(true);}
i wanted the highlight color to be "#FEC403" in the xml (that is a light orange) but the highlightcolor still is gray. so how to set the highlightcolor correctly?
thanks in advance
EDIT:
here is how i did it finally:
this is my ListView Item xml file:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
and the rowselector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/orange" />
</selector>
and at last my OnItemClick is very short now:
#Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
//TODO: ACTIONS
clickedview.setSelected(true);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.BLACK);
}
view.setBackgroundColor(Color.RED);
}
You should use a Selector.
This question and its answer might help....
In your Activity:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long itemid) {
itemOnclickList(position, view);
}
});
public void itemOnclickList(int position, View view) {
if (listview.isItemChecked(position)) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));
} else {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));
}
adapter.notifyDataSetChanged();
}
In your Adapter:
public View getView(int position, View view, ViewGroup parent) {
View convertView = inflater.inflate(R.layout.listdocument_item, null);
ListView lvDocument = (ListView) parent;
if (lvDocument.isItemChecked(position)) {
convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));
} else {
convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));
}
return convertView;
}
Good luck!
This is how it should look using selector:
Create a file within drawable called selector_listview_item.xml, which looks like (change the colors to your own):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#color/md_grey_200" android:state_pressed="true" />
<!-- default -->
<item android:drawable="#color/white" />
</selector>
Now in your layout which describes the list row (e.g. layout_list_row.xml), On the root layout add the android:background="#drawable/selector_listview_item", So the layout would look something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/selector_listview_item" <!-- this is the important line -->
android:orientation="vertical">
<!-- more stuff here -->
</LinearLayout>
(Note: you might want to add android:focusable="false" on you items within that listview row, to make the row clickable, as mentioned here)
Done.