Show or hide a widget in ListView
In this tutorial we will learn how to show or hide a widget in listview when checkbox is checked or unchecked with a simple example in Android Studio.
In this example we will have a TextView, CheckBox and Spinner in a listview cell. When checkbox is checked the spinner will disable and when it is unchecked the spinner will enable.
Example:
- Create a new project in Android Studio and name it as ListView.
- In activity main.xml copy and paste the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
</RelativeLayout>
- Create a new xml file in layout directory and name it as my_cell for listview cell.
- Copy and paste the following code in my_cell.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dp" android:gravity="center" android:orientation="horizontal" android:weightSum="2"> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textStyle="bold" android:textColor="@color/colorAccent" android:text="TextView" /> <CheckBox android:id="@+id/checkbox" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.6" android:text="Check" /> <Spinner android:id="@+id/spinner" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.4"> </Spinner> </LinearLayout>
- Now its time to code, Copy and paste the following code in main java file.
import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listview; MyAdapter myAdapter; ArrayList<String> myValues = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listView); // adding array items for listview myValues.add("Android Lovers"); myValues.add("Cupcake"); myValues.add("Donut"); myValues.add("Eclair"); myValues.add("Froyo"); myValues.add("Gingerbread"); myValues.add("Honeycomb"); myValues.add("IceCream Sandwich"); myValues.add("Jelly Bean"); myValues.add("Lollipop"); myValues.add("Marshmallow"); myAdapter = new MyAdapter(); // set adapter for listview listview.setAdapter(myAdapter); } // Custom adapter for listview class MyAdapter extends BaseAdapter { // spinner items String[] spinnerValues = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; @Override public int getCount() { return myValues.size(); } @Override public Object getItem(int arg0) { return myValues.get(arg0); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View myView = convertView; if (convertView == null) myView = inflater.inflate(R.layout.my_cell, null); TextView names = (TextView) myView.findViewById(R.id.textView); final CheckBox checkBox = (CheckBox) myView.findViewById(R.id.checkbox); final Spinner spinner = (Spinner) myView.findViewById(R.id.spinner); names.setText(myValues.get(position)); // set adapter for spinner final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, spinnerValues); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { CheckBox checkBox = (CheckBox) compoundButton.findViewById(R.id.checkbox); if (checkBox.isChecked()) { spinner.setEnabled(false); myAdapter.notifyDataSetChanged(); } else { spinner.setEnabled(true); myAdapter.notifyDataSetChanged(); } } }); return myView; } } }
- Now run the app and enjoy the output.
Download the full project here
i like your post what about i add dialogbox to check more i try it do not work my file like is here please answer mesfun@hotmail.com
ReplyDeletemy file link is here https://www.dropbox.com/sh/kbkgl5a10lz2kzf/AADwVn6CC4PRdjSz76l2DkYva?dl=0