Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The best way to customize list view items is to create them in an xml file and then, using a BaseAdapter or whatever kind of list adapter, inflate that .xml layout and fill up with your data. In order to customize a highlight animation, all you need is to create a state Drawable for the different states of an item.</p> <p>So you go to a "New xml file" -> "resource type drawable" -> "shape" name it and finish you'll see this piece of code:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;/shape&gt; </code></pre> <p>Let's create a background for a ListItem pressed state:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" &gt; &lt;solid android:color="#86d47f" this is your custom color /&gt; &lt;/shape&gt; </code></pre> <p>and non-pressed state is another file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" &gt; &lt;solid android:color="#00000000" /&gt; &lt;/shape&gt; </code></pre> <p>then a list-selector file that will be used as a background of a listview item: go to "new xml file"->"drawable"->"list selector" name it as "item_background"->finish</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="300" !!this is the fade duration time for your animation&gt; &lt;item android:drawable="@drawable/bg_drawable" android:state_pressed="true"&gt;&lt;/item&gt; &lt;item android:drawable="@drawable/transparend"&gt;&lt;/item&gt; &lt;/selector&gt; </code></pre> <p>then create an xml file for an item, customize it as you want, but for the main layout set </p> <pre><code>android:background="@drawable/item_background" </code></pre> <p>here you go, everything works perfect... here is my adapter class:</p> <pre><code>public class ListAdapter extends ArrayAdapter&lt;String&gt; { LayoutInflater inflater; public ListAdapter(Context context, int textViewResourceId, List&lt;String&gt; objects) { super(context, textViewResourceId, objects); inflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { String str_value = this.getItem(position); if (convertView == null) { convertView = inflater.inflate(R.layout.list_item, null); } TextView tv = (TextView) convertView.findViewById(R.id.textView1); tv.setText(str_value); return convertView; } } </code></pre> <p>and here you are, supposed you'll mark it as an answer.. <img src="https://i.stack.imgur.com/CMmyx.png" alt="list"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload