user3467178
user3467178

Reputation: 83

XML- Android CheckBox Widget

Thanks a lot.I am beginning of android.I am trying a CheckBox button.I make a small project. See my project photo:

1.why the list of CheckBox is not same position, it's like a random?

Check my Xml code:

2.I am using android:layout_marginTop="30dp", android:textSize="20dp", Is it perfect rules in declaretion? It will be same support all devices?

enter image description here

<CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wonton Soup " 
        android:layout_marginTop="30dp"
        android:layout_gravity="center"
        android:textSize="20dp"
        />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Egg Drop Soup " 
         android:layout_gravity="center"
         android:layout_marginTop="10dp"
         android:textSize="20dp"
        />
    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="French Fries " 
         android:layout_gravity="center"
         android:textSize="20dp"
         android:layout_marginTop="10dp"
        />

Again a lot of Thanks.

Upvotes: 0

Views: 294

Answers (3)

Jitendra Prajapati
Jitendra Prajapati

Reputation: 1163

Since your Text length is not same for all Checkbox. Put value in the max length filed of the text view. Like

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"

    android:maxWidth="125dp"
    android:text="French Fries /Testsatrs"
    android:textSize="20dp" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"

    android:text="French Fries "
    android:textSize="20dp" 
    android:maxWidth="125dp"/>

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="French Fries " 
    android:maxWidth="125dp"
     android:textSize="20dp"
     android:layout_marginTop="10dp"

    />

   </LinearLayout>

Upvotes: 0

Lavekush
Lavekush

Reputation: 6166

Step 1. Put all checkboxes into LinearLayout .

Step 2. Set all checkbox properties,

          android:layout_gravity = "left"

Upvotes: 0

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

1.why the list of CheckBox is not same position, it's like a random?

You are centering your CheckBoxes with android:layout_gravity="center". Observe that the bounds of each CheckBox is centered in its parent.

Perhaps what you are instead expecting is something along these lines:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">

    <CheckBox ... />

    <CheckBox ... />

    <CheckBox ... />

</LinearLayout>

The CheckBoxes would then appear left-aligned inside a centered box that wraps their width.

2.I am using android:layout_marginTop="30dp", android:textSize="20dp", Is it perfect rules in declaretion? It will be same support all devices?

I'm not sure what you mean with this question, but there are two possible problems:

  1. If the layout doesn't scroll, the layout may extend beyond its boundaries on devices with smaller screens. A simple solution may be to wrap the whole layout inside a ScrollView.

  2. Using DPs for text is not recommended as the user may have set a preference for text scaling: android:textSize="20sp"

    Alternatively, you may want to use one of Android's preset TextAppearances, for instance: android:textAppearance="?android:attr/textAppearanceLarge"

Upvotes: 1

Related Questions