Sajeetharan
Sajeetharan

Reputation: 222522

Linear layout inside Lineary layout in android

I am trying to create a layout in android, when i create a linear layout inside a linear layout its getting added but not visible in the interface. here is my code

<?xml version="1.0" encoding="utf-8"?>
<!-- Parent linear layout with vertical orientation -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="_baseline"
    android:orientation="horizontal" >

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dip"
      android:text="@string/name" />

  <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="top"
      android:layout_marginTop="0dp"
      android:ems="10" >

     <requestFocus />
  </EditText>

  <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="290dp"
      android:text="@string/submit" />

  <!-- Child linear layout with horizontal orientation -->

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#2a2a2a"
      android:baselineAligned="_baseline"
      android:orientation="horizontal" >

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:text="Previous" android:padding="15dip" android:layout_weight="1"
         android:gravity="center"/>

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:text="Next" android:padding="15dip" android:layout_weight="1"
         android:gravity="center"/>  

  </LinearLayout>

</LinearLayout>

Upvotes: 3

Views: 2676

Answers (5)

Mr.Sandy
Mr.Sandy

Reputation: 4349

Try this I think you want this type of UI.

<?xml version="1.0" encoding="utf-8"?>

<!-- Parent linear layout with vertical orientation -->

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

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

   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:padding="5dip"
     android:text="@string/app_name" />

   <EditText
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="top"
     android:layout_marginTop="0dp"
     android:ems="10" >

    <requestFocus />
   </EditText>

 </LinearLayout>

 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="290dp"
   />

 <!-- Child linear layout with horizontal orientation -->

 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#2a2a2a"
  android:orientation="horizontal" >

 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
     android:text="Previous" android:padding="15dip" android:layout_weight="1"
     android:gravity="center"/>

 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
     android:text="Next" android:padding="15dip" android:layout_weight="1"
     android:gravity="center"/>  

 </LinearLayout>

 </LinearLayout>

the prob. is that you want to TextView and EditText in single line for that you put parent LinearLayout in Horizontal orientation but because of that all content saperated in gorizontalway..

so, you need to parent Layout Orientation as Vertical and take new Layout in that for EditText and TextView for horizontal separation. and that new Layout you need in Horizontal Orientation.

Show your UI here:

UI based on Above code.

Upvotes: 2

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21087

try this...

<?xml version="1.0" encoding="utf-8"?>
<!-- Parent linear layout with vertical orientation -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="_baseline"
    android:layout_weight="1"
    android:orientation="horizontal" >

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dip"
      android:text="@string/name" />

  <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="top"
      android:layout_marginTop="0dp"
      android:ems="10" >

     <requestFocus />
  </EditText>

  <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="290dp"
      android:text="@string/submit" />
</LinearLayout>

  <!-- Child linear layout with horizontal orientation -->

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#2a2a2a"
      android:layout_weight="1"
      android:baselineAligned="_baseline"
      android:orientation="horizontal" >

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:text="Previous" android:padding="15dip" android:layout_weight="1"
         android:gravity="center"/>

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:text="Next" android:padding="15dip" android:layout_weight="1"
         android:gravity="center"/>  

  </LinearLayout>

</LinearLayout>

Upvotes: 0

Yog Guru
Yog Guru

Reputation: 2104

This is your description as you posted question, You done mistake in orientation, In Comment you write

< !-- Parent linear layout with vertical orientation -->

but in LinearLayout you set Orientation

android:orientation="horizontal"

make it to

android:orientation="vertical"

and your problem is solved.

Upvotes: 0

Vamsi Challa
Vamsi Challa

Reputation: 11109

Your parent layout is showing the child layout, but it is out of view. Change the Parent's layout orientation to Vertical and it will be visible.

Make these changes..

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

To get edittext in the same line,

Create a child layout for textview and edittext and make its orientation as horizontal.. it will be in the same line

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="_baseline"
    android:orientation="horizontal" >

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dip"
      android:text="@string/name" />

  <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="top"
      android:layout_marginTop="0dp"
      android:ems="10" >

     <requestFocus />
  </EditText>
</LinearLayout>

...
...
</LinearLayout>

Upvotes: 1

Emre Ozdemir
Emre Ozdemir

Reputation: 122

Change the first Linear Layout orientation "vertical". It should be works..

Upvotes: 2

Related Questions