Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>using <code>getActivity()</code> is enough </p> <pre><code>setListAdapter(new CustomPasswordsList(getActivity(), titles)); </code></pre> <p>You need to override <code>onItemCheckedStateChanged</code></p> <pre><code> public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { final int checkedCount = getListView().getCheckedItemCount(); // get checked items count </code></pre> <p>Drawing from the samples @</p> <pre><code>android-sdk-linux/samples/android-17/ApiDemos/src/com/example/android/apis/view/List16 </code></pre> <p>Example : Modify the below according to your needs</p> <pre><code>public class MainActivity extends ListActivity { String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView lv = getListView(); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); lv.setMultiChoiceModeListener(new ModeCallback()); setListAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_activated_1, GENRES)); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); getActionBar().setSubtitle("Long press to start selection"); } private class ModeCallback implements ListView.MultiChoiceModeListener { public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.list_select_menu, menu); mode.setTitle("Select Items"); return true; } public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return true; } public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.share: Toast.makeText(MainActivity.this, "Shared " + getListView().getCheckedItemCount() + " items", Toast.LENGTH_SHORT).show(); mode.finish(); break; default: Toast.makeText(MainActivity.this, "Clicked " + item.getTitle(), Toast.LENGTH_SHORT).show(); break; } return true; } public void onDestroyActionMode(ActionMode mode) { } public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { final int checkedCount = getListView().getCheckedItemCount(); switch (checkedCount) { case 0: mode.setSubtitle(null); break; case 1: mode.setSubtitle("One item selected"); break; default: mode.setSubtitle("" + checkedCount + " items selected"); break; } } } } </code></pre> <p>list_select_menu.xml</p> <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/share" android:title="share" android:icon="@android:drawable/ic_menu_share" android:showAsAction="always" /&gt; &lt;/menu&gt; </code></pre> <p>Snap shot</p> <p><img src="https://i.stack.imgur.com/sdmNi.png" alt="enter image description here"></p> <p>Since you are doubting whether it would work with custom adapter</p> <p><img src="https://i.stack.imgur.com/wNaHv.png" alt="enter image description here"></p> <p>Edit: </p> <p>under res/values-v11/styles.xml</p> <pre><code>&lt;resources&gt; &lt;style name="AppTheme" parent="android:Theme.Holo.Light"&gt;&lt;/style&gt; &lt;style name="activated" parent="AppTheme"&gt; &lt;item name="android:background"&gt;?android:attr/activatedBackgroundIndicator&lt;/item&gt; &lt;/style&gt; &lt;/resources&gt; </code></pre> <p>In the custom layout for the root element add</p> <pre><code> style="@style/activated" </code></pre>
 

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