Reputation: 3163
In my android app, I want to define a check box which should be marked as checked. Can anyone plz explain how to do it ?
Thanx in advance
Upvotes: 1
Views: 1602
Reputation: 6403
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/chkIos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_ios" />
<CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_android"
android:checked="true" />
<CheckBox
android:id="@+id/chkWindows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_windows" />
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />
</LinearLayout>
Make CheckBox is checked by default : Put android:checked="true" inside checkbox element to make it checked bu default. In this case, “Android” option is checked by default.
Upvotes: 0
Reputation: 24853
You can define in xml like the following:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text"
android:checked="true"
/>
If you want to attain the same in java, you can use setChecked(boolean)
attribute of checkbox
.
Like,
checkbox.setChecked(true);
Upvotes: 6