Prince
Prince

Reputation: 111

Android: ListView ..Convert between Click mode and Multiple Select Mode ( Like: Messaging App )

The ListView will display Contacts.

options:

1-Click Mode. If the user clicks on an item (TextView is displayed only), the app will start an new activity(contact profile).

2-Select Mode. If the user chooses Add to contacts from menu or actionbar item, the Listview will change to Select mode (Textview + Checkbox for every item).

1-How to hide and show the checkbox ?

2-How to respond to the events ?

The Code:

The layout of the Activity:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:background="@drawable/screen_background"
    android:orientation="vertical" >
    <SearchView
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:iconifiedByDefault="false" >
  </SearchView>
   <View
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:background="#000000" />
   <ListView
    android:id="@+id/listview_Menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice" >
   </ListView>

  </LinearLayout>

The List View item Layout:

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:orientation="horizontal">

    <CheckBox
     android:id="@+id/listview_checkbox_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

   <TextView
    android:id="@+id/listview_textview_item"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:textColor="#fefefe"
    android:textSize="25sp" >
   </TextView>

  </LinearLayout>

The ActivityClass

    protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_screen);

    String[] contacts = {"aaaa aaa", "bbbb bbbb" ,"ccccccc" ,"ddddd dddd" ,"ffffff ffffff"};

    final ArrayAdapter<String> adapt = 
     new ArrayAdapter<String>( this, R.layout.list_view_item,   
                              R.id.listview_textview_item,items);


    ListView menuList = (ListView) findViewById(R.id.listview_Menu);
      menuList.setAdapter(adapt);

       menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View itemClicked,
                int position, long id) {
            // TODO Auto-generated method stub
            startActivity(new Intent(this,
                    ContactProfile.class));

        }

    });

    menuList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }


    });

 }

Upvotes: 2

Views: 1246

Answers (2)

Prince
Prince

Reputation: 111

I think I found the Answer.

Firstly:

In the list_view_item Layout. For the

TextView android:textIsSelectable="false" CheckBox android:visibility="invisible"

to Allow the click mode. (setOnItemClickListener(new AdapterView.OnItemClickListener() will work)

Secondly:

Show or Hide all the checkboxes.

        ListView menuList = (ListView) findViewById(R.id.listview_Menu);
        for(int i=0; i < menuList.getChildCount(); i++){
            LinearLayout itemLayout = (LinearLayout)menuList.getChildAt(i);
            CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.listview_checkbox_item);
           cb.setVisibility((cb.getVisibility() == View.VISIBLE)? View.INVISIBLE :  View.VISIBLE);
        }

Upvotes: 3

dberm22
dberm22

Reputation: 3203

Assuming you set up the list correctly, you want to put android:visibility="invisible" in the checkbox definition and then use listview_checkbox_item.setVisibility() in your action bar button callback function to toggle the visiblity.

Upvotes: 1

Related Questions