Introduction
In this article, we learn the Android spinner and checkbox Example. If you have not yet checked it out, I would highly recommend you to read a complete practical work of Android spinner and checkbox working.
You will learn in this section
- Spinner
- Checkbox
Prerequisite
It will good to cover my previous articles before this. Its help to learn more about Android development. In which I explained. How can create checkbox and spinner in android?
What is spinner
Spinner is same like drop-down menu allows to select something from the list. The spinner manages the data through the adapter. The android spinner like a box its show multiple values.
The spinners give a speedy method to choose one incentive from a set. It allows selecting one value from drop box menu.
Contacting the spinner shows a drop-down menu with all other accessible qualities, from which the user can choose another one. You will define the array adapter in Java as well in XML. What layout are shown in a drop-down menu?. When you click on spinner what happened.
Spinner working step by step
- Create a checkbox in XML or Java.
- Create the data source.
- Define the separate layout file in which adapter will map the data inside the spinner.
- Using onItemSelectedListener method what to do when the user clicks on it.
Open <res> click on <values> than open <string.xml>
<Paste the following code>
File:string.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string-array name="country"> <item>USA</item> <item>CHINA</item> <item>UK</item> <item>japan</item> <item>UAE</item> <item>Australia</item> </string-array> </resources> |
Create a new Layout
->Right click> the ->layout folder-> select ->New option->and ->click ><-layout resource folder-> and<-After the layout is created>
<Paste the following code>
File:activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 | ?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="92dp" /> </LinearLayout> |
Create a new Class
Create new class ->Right click-> the ->package name ->select ->New option-> and click-> java class <-Create new class ->.
<Paste the following code on an activity that hosts the layout>
File:MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{ Spinner spinner; Context contex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner=(Spinner)findViewById(R.id.spinner); ArrayAdapter adapter= ArrayAdapter.createFromResource(this,R.array.country,R.layout.simple_spinner_item); spinner.setAdapter(adapter); spinner.setOnItemClickListener((AdapterView.OnItemClickListener) this); } @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { TextView mytext=(TextView) view; Toast.makeText(contex,"Your message"+mytext.getText(), Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { }} |
What is Checkbox
It is a graphical display in which the user can select something. After selection particular feature has been displaying. The checkbox is used only in those case when the user has more than one option and want to select one of those.
Concept: Would you like a mobile. which one android or ios. You will select one option between these.
Checkbox working step by step
- Create a checkbox in XML or Java.
- Implementation of View.OnClickListner.
- For check or uncheck onClick method is defined.
- Using the isChecked method find that the box is checked or not.
Create a new Layout
->Right click> the ->layout folder-> select ->New option->and ->click ><-layout resource folder-> and<-After the layout is created>
<Paste the following code>
File:activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Demoactivity" android:id="@+id/d"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android" android:id="@+id/checkBox2" android:checked="false" android:layout_marginTop="65dp" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="would you like android mobile" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout> |
Create a new Class
Create new class ->Right click-> the ->package name ->select ->New option-> and click-> java class <-Create new class ->.
<Paste the following code on an activity that hosts the layout>
File:MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.Toast; public class Demoactivity extends Activity implements View.OnClickListener { CheckBox one; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demoactivity); one= (CheckBox) findViewById(R.id.checkBox2); one.setOnClickListener(this); } @Override public void onClick(View v) { CheckBox x=(CheckBox) v; if(x.isChecked()) { Toast.makeText(this,"I like android phone",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this,"I do not like android phone",Toast.LENGTH_SHORT).show(); }}} |
Output:
Conclusion
I wish I could tell you that a great site. You just understand the key element above post-Android Spinner and Checkbox Example. More, detail about More Detail about. You also read my other lecture. I hope you will understand this lecture. Hope you got the idea.