Have you already seen the checkbox in Android right? Probably while filling the registration form on some website. Well, they can be added to mobile apps as well. The Android checkbox is a two-state button that can be either checked or unchecked. If the Android checkbox button is unchecked, the user can click on it to select and vice-versa.
Android Checkbox Example
Today, we’ll be creating a simple app to demonstrate three checkbox Android Studio that responds to user click event. Whenever the user selects a checkbox button, it will display a toast with its state.
Let’s Get Started
👉 Create a new project under the file menu and modify your project details.
👉 Select Mini SDK Version in the next step.
👉 Add an Empty Activity.
👉 And lastly, customize the Activity.
Start Code Integration
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private CheckBox cbAndroid; private CheckBox cbIos; private CheckBox cbWindows; private Button btnShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initControls(); } private void initControls() { cbAndroid = (CheckBox) findViewById(R.id.cbAndroid); cbIos = (CheckBox) findViewById(R.id.cbIos); cbWindows = (CheckBox) findViewById(R.id.cbWindows); btnShow = (Button) findViewById(R.id.btnShow); btnShow.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnShow: StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(cbAndroid.isChecked() ? "Android is checked" : "Android is unchecked"); stringBuffer.append(cbIos.isChecked() ? "\nIos is checked" : "\nIos is unchecked"); stringBuffer.append(cbWindows.isChecked() ? "\nWindows is checked" : "\nWindows is unchecked"); Toast.makeText(this, stringBuffer, Toast.LENGTH_SHORT).show(); break; default: break; } } }
Want To Create An Android Application?
Looking to Create An Android app? Get in touch with our experienced Android app developers for a free consultation.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context="com.checkboxdemo.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <CheckBox android:id="@+id/cbAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/android" /> <CheckBox android:id="@+id/cbIos" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ios" /> <CheckBox android:id="@+id/cbWindows" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/windows" /> <Button android:id="@+id/btnShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:text="@string/show" /> </LinearLayout> </LinearLayout>
Strings.xml
<resources> <string name="android">Android</string> <string name="ios">Ios</string> <string name="windows">Windows</string> <string name="show">Show</string> </resources>
Dimens.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
And done!
If you followed all the steps, demo should run just fine and look like this:
If you face any difficulty while implementing it, you can contact our Android developers for help. And if you’re not aware why the checkbox still really matter, you should know that they do contribute to making your contact & registration forms user-friendly. So if you’re developing a mobile app, do remember this point, or hire Android app development company to create a strategic development plan for your Android app.
Grab a free copy of Android checkbox example demo from Github.