2

I'm currently trying to put together a layout consisting of a few input controls and a listview, and using layout_weight I've gotten it to look the way I want it in the Android Studio preview. However, on an actual device, the layout looks completely different.

I'm fairly new to Android and have been having a few problems with their layouts before, so I'm probably missing something. However, I've been reading up on different kinds of layouts and layout_weight in particular, and everything I've found so far makes me think the result would look like in Android Studio's preview. I could easily settle for another layout in this case, but I would prefer to actually learn what's wrong, as I may face the same issue again in the future.

Below is my layout code and comparison images. Any ideas or hints are much appreciated.

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

    <EditText
        android:id="@+id/input_title"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:hint="@string/title"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/input_submit"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:text="@string/input_submit"/>

        <Button
            android:id="@+id/input_date"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@id/input_submit"
            android:text="@string/input_date"/>

        <EditText
            android:id="@+id/input_amount"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@id/input_date"
            android:layout_toLeftOf="@id/input_date"
            android:hint="@string/amount"
            android:inputType="numberDecimal"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"/>

</LinearLayout>

Android Studio preview: https://i.stack.imgur.com/4bWM6.png

Actual layout on device: https://i.stack.imgur.com/MJbOX.png

Robin
  • 47
  • 2
  • 5

2 Answers2

0

this happens because you didn't declare a standard WeightSum to the parent Layout, So add android:weightSum="8" to your main LinearLayout :

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

<!--  your code -->

</LinearLayout>

and the weight must be 8 as you gave your Views and layouts the weights (1 , 1 , 6), So the whole weight_sum of their parent layout must be 8

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    Thanks for the suggestion, although the result is exactly the same. I actually considered this track earlier, but [this comment](http://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work#comment17177512_7452788) seemed to indicate that weightSum would have no effect. – Robin Dec 04 '14 at 13:28
  • @Robin well, in fact this is something I didn't know before – Muhammed Refaat Dec 04 '14 at 13:49
0

layout_weight is need for ListView and input_amount EditText rest are wrap_content :

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

    <EditText
        android:id="@+id/input_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/title"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/input_amount"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:hint="@string/amount"
            android:inputType="numberDecimal"/>
        <Button
            android:id="@+id/input_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/input_date"/>

        <Button
            android:id="@+id/input_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/input_submit"/>

    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Thanks, this is closer, but it suffers from the same issue that my layout had before using weights: the height of `input_title` won't match the height of `input_amount`. Any ideas on how to fix that in your proposed solution? – Robin Dec 04 '14 at 13:37