user2164685
user2164685

Reputation:

Linear layout arangment in android

I am learning Linear layout and I have some confusion.

<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"
    tools:context=".MainActivity" >   


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_weight=".33"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_weight=".33"/> 


     <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"   
        android:text="Button" 
        android:layout_weight=".3"/>


</LinearLayout>

In these code there are three elements.

  1. TextView 2. Button1 3. Button2

The linear layout orientation is vertical and I have given them equal weight adjusting in one weight value only.

Now in vertical orientation I am am giving

android:layout_width="fill_parent"
android:layout_height="wrap_content"

So it is filling both the height and width.

But when I am using horizontal layout and writing like

    android:layout_height="fill_parent"
    android:layout_width="wrap_content"

It is filling width but not filling height

Instead of giving android:layout_height="fill_parent" why it not filling the height?

Upvotes: 0

Views: 59

Answers (2)

Nicholas TJ
Nicholas TJ

Reputation: 1659

I think you need an understanding on this topic instead of someone throwing a code at you. The working code is as shown by @Nirali above.

So, in this crash course, you need to understand what is fill_parent and wrap_content. The linearlayout is your parent or you can see this as the screen of your device. If you set your height to fill_parent, it will fill the whole height of the screen.

Wrap_content is to wrap your object, for example your button. If you set height to wrap_content, it will just set the height to wrapping your button nicely.

Above are explanation that comes off my head, for a better understanding, refer to this post What's the difference between fill_parent and wrap_content?

Upvotes: 0

Nirali
Nirali

Reputation: 13805

Copy paste this code and try it.. It will fill height.

<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="horizontal"
    tools:context=".MainActivity" >   


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/hello_world" 
        android:layout_weight=".33"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button" 
        android:layout_weight=".33"/> 


     <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"   
        android:text="Button" 
        android:layout_weight=".3"/>


</LinearLayout>

Upvotes: 0

Related Questions