Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I got solution for your problem. Do as following.</p> <p>1) open your main layout file where <strong>ListView</strong> you have created.<br> Add <code>android:choiceMode="singleChoice"</code>. This will look like below.</p> <pre><code>&lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:choiceMode="singleChoice" &gt; &lt;/ListView&gt; </code></pre> <p>2) Open your <strong>list_item.xml</strong> layout file. In which, to your root view, add <code>android:background="?android:attr/activatedBackgroundIndicator"</code>. In my sample project, its look like below.</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="match_parent" android:layout_height="match_parent" android:background="?android:attr/activatedBackgroundIndicator" android:orientation="vertical" &gt; //your views &lt;/LinearLayout&gt; </code></pre> <p>3) Open your activity file. After setting adapter to your <code>ListView</code>, add <code>list.setSelector(R.drawable.selection_effect);</code>. This will look like below.</p> <pre><code>ListView ls = (ListView) findViewById(R.id.listView1); ListAdapter adapter = new ListAdapter(this, data); ls.setAdapter(adapter); ls.setSelector(R.drawable.selection_effect); </code></pre> <p>Here, <code>selection_effect</code> is drawable file which you have created in <code>drawable</code> directory. I tested my code. Which is working fine.</p> <p>4) To select first view by default, remove your code in <code>BaseAdapter</code> and put following code after completing 3rd step.</p> <pre><code>ls.setItemChecked(0,true); </code></pre> <p>You need to put it after above code like below.</p> <pre><code>ListAdapter adapter = new ListAdapter(data); ls.setAdapter(adapter); ls.setSelector(R.drawable.selection_effect); ls.setItemChecked(0, true); </code></pre> <p><strong>Explanation</strong></p> <blockquote> <p>ls.setSelector(R.drawable.selection_effect);</p> </blockquote> <p>This will select row item based on <code>selector</code> you have defined in drawable directory.</p> <blockquote> <p>ls.setItemChecked(0, true);</p> </blockquote> <p>This will select first item by default at first time run. After you can select other items by clicking on them.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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