0

I am building an application where I need to make some layout. Normally I use Relative Layout and custom views which makes my work however this time, I think of using Liner Layout so I made this below XML. My aim was to make a child linear layout inside a parent and make than child's gravity to "bottom" which will hold my two bottom, one should be right aligned and one should be left aligned.

<LinearLayout 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:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom"       
    >       
   <Button            
        android:gravity="left"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Previous" />
   <Button            
        android:id="@+id/button3"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"/>
</LinearLayout>

The child is sitting in the bottom however two views that is buttons are not aligned properly. Am I doing anything wrong, or can these things not be achieved through this Layout?

halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88

5 Answers5

2

Try this layout..hope this would helpful to you

<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"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Previous" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />
    </LinearLayout>

</RelativeLayout>
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • Dont have to use Relative Layout only layout_weight will solve the issue. Thanks you for reminding me that!!! thanks – Saty May 27 '14 at 10:14
2

Pure LinearLayout used:

<LinearLayout 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:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:orientation="vertical"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Previous"
            android:id="@+id/button"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Next"
            android:id="@+id/button2"
            android:layout_weight="1" />
    </LinearLayout>

To explain you need to maximize the use of weightSum for this. also the another layout that doesn't contain anything can contain your other widgets just in case you need to add some TextViews, etc. Also this is applied so that the other layout will be forced to be placed in bottom of the whole parent LinearLayout.

Just in case you wanted to have a space between those two buttons on the middle, you can always make use of the padding. ;)

KaHeL
  • 4,301
  • 16
  • 54
  • 78
0
you can do this using Relative layout easily

<LinearLayout 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:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom"       
    >  
      <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >      
   <Button            
        **android:layout_alignParentLeft=true**
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Previous" />
   <Button            
        android:id="@+id/button3"
        **android:layout_alignParentRight=true**
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
0

Try the following code.

<LinearLayout 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:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical"
android:gravity="bottom" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom"       
    >       
   <Button            
        android:gravity="left"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Previous" />
   <Button            
        android:id="@+id/button3"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"/>
</LinearLayout>
Ravi Bhayani
  • 151
  • 3
  • this one can do, but do take note: you're making the whole parent layout useless after adding the gravity="bottom" since in case you need to add another widget on this part it will be placed on bottom as well. Always make sure that it won't restrict the design when creating a layout. :) – KaHeL May 27 '14 at 10:23
0
// Try this way,hope this will help you to solve your problem...

<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="bottom"
    android:padding="5dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"/>
    </LinearLayout>
</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67