04 Aug Android CheckBox Control
Android CheckBox is a two-states button, which you can either check or uncheck.
Usage
Let’s say you want to create a form to list skills. For that you can use the Android Checkbox control. Another example, language a user is fluent in. The fluency can be in more than one language, so we’re using checkbox to select one of more options at a time.
Another example, asking users to select which sports they like, it can have one or more selections, so use CheckBox i.e. Football, Cricket, Basketball, etc.
Android CheckBox Control
Example
Here’s a simple example showing the working on Android CheckBox on Android Studio. Here, we will be showing result of selected checkbox on button click.
If you’re beginning with Android, then learn how to run your first Android app in Android Studio.
Let’s start with the usage of Android CheckBox control.
MainActivity.java
The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here, we will set the View to be displayed with the setContentView.
We have 5 checkboxes here: check1, check2, check3, check4, and check5, for sports football, tennis, basketball, hockey, and cricket. We also have a TextView i.e. tv1, which prints the result on button click. The result in our example is the codes assigned to each sport by the club. Whatever the user selects, gets appended and result gets displayed in TextView tv1,
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
Package com.studyopediaapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private CheckBox football, tennis, hockey, cricket, basketball; private Button buttonDetails; private TextView tv1; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Buttons football=(CheckBox)findViewById(R.id.check1); tennis=(CheckBox)findViewById(R.id.check2); basketball=(CheckBox)findViewById(R.id.check3); hockey=(CheckBox)findViewById(R.id.check4); cricket=(CheckBox)findViewById(R.id.check5); buttonDetails=(Button)findViewById(R.id.btDetails); // TextView to display result tv1 = (TextView) findViewById(R.id.textCode); // Listener buttonDetails.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tv1.setText(""); tv1.setText("CODES:"); // Checking the sports name one by one if(football.isChecked()){ // append() method appends the result tv1.append("\nFB: Football"); } if(tennis.isChecked()){ tv1.append("\nTT: Table Tennis"); } if(basketball.isChecked()){ tv1.append("\nBB: Basketball"); } if(hockey.isChecked()){ tv1.append("\nHK: Hockey"); } if(cricket.isChecked()){ tv1.append("\nCK: Cricket"); } } }); } } |
activity_main.xml
For src\main\res\layout\activity_main.xml,
We have 5 checkboxes and 3 TextView. The result gets displayed in TextView textCode
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
<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" tools:context=".MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title" android:textColor="#000000" android:textSize="20sp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ques" android:textColor="#000000" android:textSize="22sp" android:layout_marginTop="53dp" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textCode" android:layout_width="230dp" android:layout_height="280dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView3" android:background="#daf7a6" android:textAlignment="center" android:text="@string/club_code" android:textColor="#000000" android:textSize="22sp" android:textStyle="bold" /> <CheckBox android:id="@+id/check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/football_check" android:layout_marginTop="22dp" android:layout_below="@+id/textView3" android:layout_alignLeft="@+id/check2" android:layout_alignStart="@+id/check2" /> <CheckBox android:id="@+id/check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tennis_check" android:layout_marginTop="20dp" android:layout_below="@+id/check1" android:layout_alignRight="@+id/check5" android:layout_alignEnd="@+id/check5" /> <CheckBox android:id="@+id/check3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/basketball_check" android:layout_marginTop="17dp" android:layout_below="@+id/check2" /> <CheckBox android:id="@+id/check4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hockey_check" android:layout_below="@+id/check3" android:layout_alignLeft="@+id/check3" android:layout_alignStart="@+id/check3" android:layout_marginTop="21dp" /> <CheckBox android:id="@+id/check5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cricket_check" android:layout_marginTop="26dp" android:layout_below="@+id/check4" android:layout_alignLeft="@+id/check4" android:layout_alignStart="@+id/check4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btDetails" android:text="@string/detail" android:background="#0000A0" android:textColor="#ffffff" android:textSize="18sp" android:layout_below="@+id/check5" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:layout_marginLeft="96dp" android:layout_marginStart="96dp" android:layout_marginTop="46dp" /> </RelativeLayout> |
AndroidManifest.xml
The content of \src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.studyopediaapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
strings.xml
Here’s the code for res/values/strings.xml. The app name and any hardcoded strings will be visible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<resources> <string name="app_name">StudyopediaApplication</string> <string name="title">Studyopedia Checkbox Application</string> <string name="detail">CLUB CODES</string> <string name="ques">Which sports do you want to play in the Sports Club?</string> <string name="football_check">Football</string> <string name="tennis_check">Table Tennis</string> <string name="basketball_check">Basketball</string> <string name="hockey_check">Hockey</string> <string name="cricket_check">Cricket</string> <string name="club_code">CHECK THIS SPACE Code with the associated sport will display here.</string> </resources> |
Output
For example,
We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + f10. This will install your app on the AVD and the following emulator with your app opens up,
Here’s the output,
Select one of more checkbox and click the CLUB CODES button to get the codes for each sport,
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments