Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did like <strong>heepie</strong> advised above. <br> And as I understand there is not a property that allow to set background drawable to single row of <code>ListView</code> directly. It's should be done by creating custom view in <code>getView</code> method of a custom adapter. This is my final code. May be it will be useful to someone.<br><br> <strong>layout/main.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/my_list" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>layout/my_list_item.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="left|center_vertical" android:paddingLeft="6dip" android:textColor="#FFFFFF" android:background="@drawable/list_selector" /&gt; </code></pre> <p><strong>drawable/list_selector.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/bg_highlighted" /&gt; &lt;item android:drawable="@drawable/bg_default" /&gt; &lt;/selector&gt; </code></pre> <p>Drawable png images: <br> <strong>drawable/bg_default.png:</strong><br> <img src="https://i.stack.imgur.com/yLr94.png" alt="bg_default"></p> <p><strong>drawable/bg_highlighted.png:</strong><br> <img src="https://i.stack.imgur.com/oVP6b.png" alt="bg_highlighted"></p> <p><strong>src/main.java:</strong></p> <pre><code>public class main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyAdapter adapter; ArrayList&lt;String&gt; mStrings = new ArrayList&lt;String&gt;(); mStrings.add("Item 1"); mStrings.add("Item 2"); mStrings.add("Item 3"); mStrings.add("Item 4"); ListView lv = (ListView) findViewById(R.id.my_list); adapter = new MyAdapter(this, mStrings); lv.setAdapter(adapter); } } </code></pre> <p><strong>src/MyAdapter.java:</strong></p> <pre><code>public class MyAdapter extends BaseAdapter { public Activity activity; ArrayList&lt;String&gt; data = new ArrayList&lt;String&gt;(); private static LayoutInflater inflater = null; public MyAdapter(Activity a, ArrayList&lt;String&gt; d) { activity = a; data = d; inflater = LayoutInflater.from(activity); } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = inflater.inflate(R.layout.my_list_item, null); holder = new ViewHolder(); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Touch on view handle Log.d("", "Touched row "+position); } }); //customizing view holder.textView = (TextView)convertView.findViewById(R.id.my_textview); holder.textView.setText(data.get(position)); return convertView; } public static class ViewHolder { public TextView textView; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } } </code></pre> <p><strong>And this is a result</strong>:<br><br> <img src="https://i.stack.imgur.com/ucPNX.png" alt="result.png"> <br></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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