Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Building the Weather UI Using layout_weight

We've added a LinearLayout and some more TextViews to this layout. Using only the layout_weight attribute, make these

please help me.Am stuck

activity_movie.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/movieTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text=""
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <ImageView
        android:id="@+id/movieImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/movieTitle" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView2"/>

    </LinearLayout>

</RelativeLayout>

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi

I haven't done this course yet so i'm unsure if you have covered all of the below. I will talk you through it. Inside the parent view (LinerLayout) you need to assign a value that defines how much space is available (I used 100 as it divides by 2 easy, however using any other number divisible by 2 would have been fine) using the following line

android:weightSum="100"

I then added the line android:layout_weight = "50" to each of the text view's, what this means is that 50 of the available 100 should be used by this view (so i had used 80 and 20 instead, the text views would have used 80% and 20% of the space).

I always set the layout_width or layout_height to 0dp (depending on orientation of course) so it doesn't interfere with the weight attributes.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/movieTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text=""
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <ImageView
        android:id="@+id/movieImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/movieTitle" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="100"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="50"
            android:id="@+id/textView"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="50"     
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView2"/>

    </LinearLayout>

</RelativeLayout>

Hope this was clear

Thanks Daniel

Thanks Daniel, I Kinda Got Confused a bit But..You saved me! Thanks!