Android:how to center individual views when layout's gravity is fixed

105 Views Asked by At

I am using Linear Layout for this xml file :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    >



    <EditText
        android:id="@+id/editTextUserNameToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="User Name">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextPasswordToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonSignIn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sign In" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/toggleButton"/>

</LinearLayout>

I want all text views to appear in the center of the screen, except for the the toggle button which i want it to be in the right-bottom of the screen.

As you can see, i am using android:gravity center in the layout's tag, because neither this nor android:layout_gravity was working on individual textviews.

But now i want toggle button to be independent of this. How can i achieve it?

4

There are 4 best solutions below

0
On BEST ANSWER

You can use relative layout look this code how you can set your xml as per your requirement.

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


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

    <EditText
        android:id="@+id/editTextUserNameToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_centerInParent="true"
        android:hint="User Name">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextPasswordToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:layout_below="@+id/editTextUserNameToLogin"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonSignIn"
        android:layout_width="fill_parent"
        android:layout_below="@+id/editTextPasswordToLogin"
        android:layout_height="wrap_content"
        android:text="Sign In" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="New ToggleButton" />
</RelativeLayout>

This is it what you want. Thanks

0
On

test this xml :

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

<LinearLayout
   android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:gravity="center"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editTextUserNameToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="User Name" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextPasswordToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonSignIn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sign In" />
</LinearLayout>

<ToggleButton
    android:id="@+id/toggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New ToggleButton" />

0
On

Just wrap the entire layout into a FrameLayout and keep the toggle button outside of the LinearLayout. then put layout_gravity as you wish. Thats the trick. cheers :) Try this

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    >
    <EditText
        android:id="@+id/editTextUserNameToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="User Name">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextPasswordToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonSignIn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sign In" />


    </LinearLayout>
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
    android:layout_gravity="bottom|right"
        android:id="@+id/toggleButton"/>
    </FrameLayout>
0
On

check this,

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


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

    <EditText
        android:id="@+id/editTextUserNameToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_centerInParent="true"
        android:hint="User Name">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextPasswordToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:layout_below="@+id/editTextUserNameToLogin"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/buttonSignIn"
        android:layout_width="fill_parent"
        android:layout_below="@+id/editTextPasswordToLogin"
        android:layout_height="wrap_content"
        android:text="Sign In" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="New ToggleButton" />
</RelativeLayout>