Ahmed
Ahmed

Reputation: 15069

layout_gravity not doing what it expect

I have this linear layout vertical orientation, containing three views,

I want first view to be on top , second to be in the centre and third to be at the bottom of the parent linear layout, I am applying layout_gravity on the children of the layout but this property seems not working at all.. All of them show at the top of the screen one after the other

Edit : This might get solved if weight is applied to the second view , but what's going on with layout_gravity, why is it not serving the purpose ?

here is the code

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="Wifi not enabled"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="App require wifi to be enabled on your phone"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/btn_wifion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"      
        android:text="Turn on my Wifi" />


  </LinearLayout>

Upvotes: 2

Views: 137

Answers (3)

RobinHood
RobinHood

Reputation: 10969

you better use RelativeLayout: and why layout_gravity is not working within linear-layout, have a once look Class Overview of linear layout.

To achieve your desire goal use Relative Layout and use android:layout_alignParent check below code#

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"    
android:orientation="vertical" >   

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

       android:layout_centerHorizontal="true"
        android:text="Wifi not enabled"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="App require wifi to be enabled on your phone"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/btn_wifion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"     
        android:text="Turn on my Wifi" />


  </RelativeLayout>

Upvotes: 1

Lokesh Mehra
Lokesh Mehra

Reputation: 545

Try reading the [UPDATE] part here. It says -

“layout_gravity” property can only be used orthogonally with the orientation of the LinearLayout.

In other words, if you have a horizontal LinearLayout, then by construction, each inside child view can only have layout_gravity top, bottom, and center. The intuition behind this is that the LinearLayout is already told to place each child view horizontally adjacent to each other (left to right), and so it only allows vertical specification for the layout_gravity of each child. Vice versa for a vertical LinearLayout.

The same goes with vertical n layout_gravity now does not allow you to align the child views to the parent's top/bottom/... for that there is RelativeLayout.

Maybe this is what you were looking for. :)

UPDATE And refer to this link for more. :)

Upvotes: 1

G_S
G_S

Reputation: 7110

Is this what you need?

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="0"
        android:text="Wifi not enabled"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
         android:layout_weight="1"
        android:text="App require wifi to be enabled on your phone"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/btn_wifion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"      
         android:layout_weight="0"
        android:text="Turn on my Wifi" />


  </LinearLayout>

Upvotes: 0

Related Questions