Hey Buddy You want to add a checkbox in the spinner with the SharedPreference then you are in the right place to learn this way.
Before going forward first I’ll explain to you the flow of the app. When you open the app there will be a spinner which contains a set of checkboxes and text when the user clicks the check box value should store in SharedPreference and if again clicked on the same checkbox then the key data should get removed.
If you have a custom implementation like you want to show check-in spinner Then you are at the right place I’ll show you how you can implement it So Let’s Begin to develop this app.
How to add a checkbox in the Spinner?
For spinner Three classes are used are the following
We will go step by step before this first create a new android project and open the activity_main.xml.
Inside the constraint layout add spinner
Now create spinner_list.xml in the layout file and add a checkbox, textview inside of it.
After this add all the text which you want to show with a checkbox into <string-array> <item> itemName </item> </string-array> Inside the strings.xml
This above was all UI part,Now we will go to class file and create Model.class
Create Model.class and type copy the below after this generate getter and setter of both. To create getter and setter press Alt + Insert or right inside the class file and click on generate then click on getter and setter options.
private boolean selected; private String title;
Now create Adapter class and extends to ArrayAdapter<ModelClassName> and hook the context,ArrayList,MyAdapter,boolean.
After doing this create constructor using shortcut Alt + Insert and ,if it show red balloon then click on red balloon and implement rest methods.
Next create three methods for this type on editor getDropDownView, getView, getCustomView, and ViewHolder.
Inside the ViewHolder Declare the textview, and checkbox.
Now in getCustomView, you need to make ViewHolder final and check the convertView should be not null, ConvertView is like a list.
Inside If statement create LayoutInflater and assign the layout spinner_list, and instantiate textview and checkbox using holder.
After this set text and checkbox data using .setText and .setChecked function.Through (listState.get(position)); will give the data for the textView and checkBox.
When the spinner load we don’t want to show First spinner with check for this we will hide the visibility of the checkbox.
The next step is to create a method onCheckedChanged inside of this we will check the isFromView is true when we click on the checkbox to check this pass the statement listState.get(position).setSelected(isChecked);
We have use the Log and toast to check what happen when we click on Checkbox.
Above using this step you can successfully build spinnerWithCheckbox,If you want to add SharedPreference you can continue to read.
How to store and remove checkBox data from SharedPreference?
Inside the If statement runs another if and else statement in a statement set expression that if the checkbox is checked then SharedPrefernces should save the data inside of sharedPreference store using key and value.
If the else statement is false then remove the stored key and value from SharedPreference.
That’s it for store and remove the value from sharedPreference and now we will show you how to retrieve value from sharedPreference.
In this MainActivity you need to first instantiate spinner and strings-array after this we run for conditions to callData then in MyAdapter class we will set data to spinner.
Inside of this we will call the sharedPreference using
SharedPreferences sharedPreferences = this.getPreferences(Context.MODE_PRIVATE); Map<String, ?> category = sharedPreferences.getAll();
To check the value is store we have use Toast message to display the select text.
You can also check whether data is getting store or not to check that you have to open Device explorer which will be situated on the right side of the screen click and go to the directory /data/data/com.appname/shared_prefs
Inside of this, you see the XML with the given name which you provided at the time of making.
Open the file and you will see the value when you clicked on the checkbox and the value will get remove as you click again on the same checkbox.
This was a basic demo app, I think so you can develop a spinner with checkbox and store checkbox value in sharedPreference even remove data too.
If you having any doubt feel free to comment down or contact us.
A man with a tech effusive, who has explored some of the amazing technology stuff and exploring more, While moving towards, I had a chance to work on Android Development, Linux, AWS, DevOps with several Open source tools.
One of my life mottos “Always be lifelong Students.”