CheckBox is used when more than one option need to select
like - in Quiz, Exam and more. A checkbox is two-states button that can be either checked
or unchecked. It is <CheckBox> tag.
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="@+id/chk1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 1"
/>
<CheckBox
android:id="@+id/chk2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 2"
/>
<CheckBox
android:id="@+id/chk3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3"
/>
<CheckBox
android:id="@+id/chk4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 4"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="@+id/chk1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 1"
/>
<CheckBox
android:id="@+id/chk2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 2"
/>
<CheckBox
android:id="@+id/chk3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3"
/>
<CheckBox
android:id="@+id/chk4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 4"
/>
</LinearLayout>
Programattically
XML
?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ll1" > </LinearLayout>
Java
public class MainActivity extends AppCompatActivity { LinearLayout layout1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout1=(LinearLayout)findViewById(R.id.ll1); CheckBox checkBox=new CheckBox(this); checkBox.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); // You can also use this code instead above line // checkBox.setWidth(300); // checkBox.setHeight(200); checkBox.setText("Option 1"); layout1.addView(checkBox); } }
********************************************************************************************
Get Text
To get text from checkbox, we use
"getText()" method
Set Text
To set or change of checkbox, we use
"setText()" method
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/txtview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Show Data" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" /> <CheckBox android:id="@+id/chk1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 1" /> <CheckBox android:id="@+id/chk2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 2" /> <CheckBox android:id="@+id/chk3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 3" /> <CheckBox android:id="@+id/chk4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get All Text" android:onClick="onGet" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change All Text" android:onClick="onSet" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get selected Text" android:onClick="onGetSelect" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change selected Text" android:onClick="onSetSelect" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reset" android:onClick="onReset" /> </LinearLayout>
java
public class MainActivity extends AppCompatActivity { CheckBox checkBox1, checkBox2, checkBox3, checkBox4; TextView tv1; String s1, s2, s3, s4, str; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.txtview1); checkBox1 = (CheckBox) findViewById(R.id.chk1); checkBox2 = (CheckBox) findViewById(R.id.chk2); checkBox3 = (CheckBox) findViewById(R.id.chk3); checkBox4 = (CheckBox) findViewById(R.id.chk4); s2 = checkBox2.getText().toString(); s3 = checkBox3.getText().toString(); s4 = checkBox4.getText().toString(); str = s1 + s2 + s3 + s4; } public void onGet(View view) { tv1.setText(str); } public void onSet(View view) { checkBox1.setText("Change1"); checkBox2.setText("Change2"); checkBox3.setText("Change3"); checkBox4.setText("Change4"); } public void onGetSelect(View v) { tv1.setText(""); if (checkBox1.isChecked()) { tv1.append(checkBox1.getText().toString()); } if (checkBox2.isChecked()) { tv1.append(checkBox2.getText().toString()); } if (checkBox3.isChecked()) { tv1.append(checkBox3.getText().toString()); } if (checkBox4.isChecked()) { tv1.append(checkBox4.getText().toString()); } } public void onSetSelect(View v) { if (checkBox1.isChecked()) { checkBox1.setText("Change1"); } if (checkBox2.isChecked()) { checkBox2.setText("Change2"); } if (checkBox3.isChecked()) { checkBox3.setText("Change3"); } if (checkBox4.isChecked()) { checkBox4.setText("Change4"); } } public void onReset(View v) { checkBox1.setText("Option1"); checkBox2.setText("Option2"); checkBox3.setText("Option3"); tv1.setText(""); } }
Check and Uncheck
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/txtview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Show Data" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" /> <CheckBox android:id="@+id/chk1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 1" /> <CheckBox android:id="@+id/chk2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 2" /> <CheckBox android:id="@+id/chk3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 3" /> <CheckBox android:id="@+id/chk4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Option 4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check" android:onClick="onCheck" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Uncheck" android:onClick="onUncheck" /> </LinearLayout>
java
public class MainActivity extends AppCompatActivity { CheckBox checkBox1, checkBox2, checkBox3, checkBox4; TextView tv1; String s1, s2, s3, s4, str; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.txtview1); checkBox1 = (CheckBox) findViewById(R.id.chk1); checkBox2 = (CheckBox) findViewById(R.id.chk2); checkBox3 = (CheckBox) findViewById(R.id.chk3); checkBox4 = (CheckBox) findViewById(R.id.chk4); s2 = checkBox2.getText().toString(); s3 = checkBox3.getText().toString(); s4 = checkBox4.getText().toString(); } public void onCheck(View v) { checkBox1.setChecked(true); checkBox2.setChecked(true); checkBox3.setChecked(true); checkBox4.setChecked(true); } public void onUncheck(View v) { checkBox1.setChecked(false); checkBox2.setChecked(false); checkBox3.setChecked(false); checkBox4.setChecked(false); } }
A. Change check box style & button position (Left,
Right, Top, Bottom, start, End )
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sub-Services 9"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:drawablePadding="10dp"
android:gravity="center"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sub-Services 9"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:drawablePadding="10dp"
android:gravity="center"
/>
1. Make button null.
android:button="@null"
2. Choose position of
your checkbox button and style.
For Left
android:drawableLeft="@android:drawable/btn_radio"
For Right
android:drawableRight="@android:drawable/btn_radio"
For Top
android:drawableTop="@android:drawable/btn_radio"
For Bottom
android:drawableBottom="@android:drawable/btn_radio"
No comments:
Post a Comment