Rose18
Rose18

Reputation: 3163

Android : mark as checked a check box

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

Answers (4)

chinna_82
chinna_82

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

Rajj
Rajj

Reputation: 101

try this one,

public void setSelected (boolean selected)

Upvotes: 0

Hariharan
Hariharan

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

2Dee
2Dee

Reputation: 8621

Use the setChecked(boolean) method. It is a method from CompoundButtons, which CheckBox extends. See docs here.

Upvotes: 0

Related Questions