Shweta
Shweta

Reputation: 807

Unable to align checkbox(horizontally/vertically) in Android app

I need to display checkbox. All the check-boxes should align themselves at the extreme right of the screen.However, if I change the orientation of my screen the check-box comes at the middle of the screen.Please help me to solve the issue.Thanks in advance

<?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:background="#000000" 
  >




    <LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:orientation="vertical"
android:layout_margin="10dp">




        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#000000" 
            >


         <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/sf_table"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="50dp"
            android:background="@drawable/test"

            >

          <View   android:id="@+id/Divider_7"
        android:layout_height="0.5dp"
        android:layout_width="fill_parent"
        android:background="#808080"

        android:layout_marginBottom="5dp"
         />
           <TableRow
                android:id="@+id/tbr6"

                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                 >

             <TextView
                 android:textStyle="bold"
                 android:layout_marginLeft="5dp"
                 android:layout_marginRight="5dp"
                 android:layout_marginTop="2dp"
                 android:layout_marginBottom="2dp"
                 android:textColor="#3399CC"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="Pages For Website (existing pages plus new ones you need)-Contact Us"
                 ></TextView> 

</TableRow>

            <View   android:id="@+id/Divider_2"
        android:layout_height="1.5dp"
        android:layout_width="fill_parent"
        android:background="#C0C0C0"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
         />   
            <TableRow
                android:id="@+id/tbr9"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                 >


  <TextView
                 android:textStyle="bold"
                 android:layout_marginLeft="5dp"
                 android:layout_marginRight="5dp"
                 android:layout_marginTop="2dp"
                 android:layout_marginBottom="2dp"
                 android:textColor="#ffffff"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="Feedback"
                 ></TextView> 

                  <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                   android:id="@+id/feedback_id"
                        android:layout_gravity="right"
                    ></CheckBox>


   </TableRow>



            <TableRow
                android:id="@+id/tbr9"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                 >


  <TextView
                 android:textStyle="bold"
                 android:layout_marginLeft="5dp"
                 android:layout_marginRight="5dp"
                 android:layout_marginTop="2dp"
                 android:layout_marginBottom="2dp"
                 android:textColor="#ffffff"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="Tech Support"
                 ></TextView> 

                  <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/techsupport_id"
                        android:layout_gravity="right"
                    ></CheckBox>


   </TableRow>


                <TableRow
                android:id="@+id/tbr9"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_margin="2dip"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                 >


  <TextView
                 android:textStyle="bold"
                 android:layout_marginLeft="5dp"
                 android:layout_marginRight="5dp"
                 android:layout_marginTop="2dp"
                 android:layout_marginBottom="2dp"
                 android:textColor="#ffffff"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="Quote Form"
                 ></TextView> 

                  <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/quoteform_id"
                        android:layout_gravity="right"
                    ></CheckBox>


   </TableRow>

          </TableLayout>





         </RelativeLayout> 

            </ScrollView>


            </LinearLayout>

Upvotes: 1

Views: 5855

Answers (3)

Diah Permatasari
Diah Permatasari

Reputation: 1

use tablelayout an then use tablerowlayout. and put your checkboxes.

<?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" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textColor="#ffffffff"
                    android:text="ARE YOU CLASS 2B?" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="50dp"
                android:layout_height="50dp" >
                <RadioGroup
                    android:id="@+id/radioGroup" android:textColor="#000000"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/yes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Yes" />

                <RadioButton
                    android:id="@+id/no"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No" />
            </RadioGroup>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/jimmy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Jimmy" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/diana"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Diana" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/dina"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Dina" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:id="@+id/jack"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Jack" />

            </TableRow>

            <TextView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TableRow
                android:id="@+id/tableRow7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

            </TableRow>

        </TableLayout>

    </LinearLayout> 

It's example hehe

Upvotes: 0

Itai Hanski
Itai Hanski

Reputation: 8680

android:layout_gravity is not relevant in a LinearLayout. It is meant for a FrameLayout. It means "what should be my gravity in relation to my (FrameLayout) father". android:gravity on the other hand means "what is the gravity inside me" and it applies to different views differently (like in a TextView it will align the text according to the gravity).

I think you could probably throw out half of the layouts you are using to display views (it's kind of confusing and hard to read) by simplifying your layout scheme and use just using a RelativeLayout for alignment issues (align to parent or to other views) and LinearLayout for grouping vertical and horizontal groups.

Upvotes: 0

krishna
krishna

Reputation: 142

try the following

android:layout_gravity="right"
android:gravity="right"

Upvotes: 1

Related Questions