Hello guys, how are you today? Its super Saturday, i hope you guys will be enjoying your weekend.
In this blog we are going to see how to work with the checkboxes in android. So lets get started.
1) First of all create an Android Application Project.
2) Go to "res/layout/activity_main.xml" and create 4 checkboxes thereselect, android, windows, ios. The code looks like
3) Next go to "src/your_package_name/MainActivity.java" and here we will implement OnCheckedChangeListener for all the checkboxes. On clicking the "select all" checkbox all the rest checkboxes should get selected and on unselecting it the rest should get unselected.
The code for MainActivity is given below
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
In this blog we are going to see how to work with the checkboxes in android. So lets get started.
1) First of all create an Android Application Project.
2) Go to "res/layout/activity_main.xml" and create 4 checkboxes thereselect, android, windows, ios. The code looks like
<LinearLayout 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" android:orientation="vertical" > <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select all" android:id="@+id/select" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android" android:id="@+id/android" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Windows" android:id="@+id/win" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="iOS" android:id="@+id/ios" /> </LinearLayout>
3) Next go to "src/your_package_name/MainActivity.java" and here we will implement OnCheckedChangeListener for all the checkboxes. On clicking the "select all" checkbox all the rest checkboxes should get selected and on unselecting it the rest should get unselected.
The code for MainActivity is given below
import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends Activity implements OnCheckedChangeListener { CheckBox select, android, win, ios; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); select = (CheckBox) findViewById(R.id.select); android = (CheckBox) findViewById(R.id.android); win = (CheckBox) findViewById(R.id.win); ios = (CheckBox) findViewById(R.id.ios); select.setOnCheckedChangeListener(this); android.setOnCheckedChangeListener(this); win.setOnCheckedChangeListener(this); ios.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.select: if (select.isChecked()) select_all(); else unselect_all(); break; case R.id.android: if (android.isChecked()) Toast.makeText(this, "Android is selected", Toast.LENGTH_SHORT) .show(); else Toast.makeText(this, "Android is unselected", Toast.LENGTH_SHORT).show(); break; case R.id.win: if (win.isChecked()) Toast.makeText(this, "Windows is selected", Toast.LENGTH_SHORT) .show(); else Toast.makeText(this, "Windows is unselected", Toast.LENGTH_SHORT).show(); break; case R.id.ios: if (ios.isChecked()) Toast.makeText(this, "ios is selected", Toast.LENGTH_SHORT) .show(); else Toast.makeText(this, "ios is unselected", Toast.LENGTH_SHORT) .show(); break; } } private void select_all() { // TODO Auto-generated method stub android.setChecked(true); win.setChecked(true); ios.setChecked(true); } private void unselect_all() { // TODO Auto-generated method stub android.setChecked(false); win.setChecked(false); ios.setChecked(false); } }
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
0 comments:
Post a Comment