globalninja
globalninja

Reputation: 25

Layout not readjusting properly to orientation change

I have been working on an app to start out on android. I have run into the problem that my layout will not show correctly when it goes to a different orientation other like this:

https://i.sstatic.net/0iBxd.jpg

Here is my vertical layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="3dip" >

    <EditText
        android:id="@+id/passText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:hint="Passcode"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/messageText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="top|left"
        android:hint="Message" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/encodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Encode" />

        <Button
            android:id="@+id/decodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Decode" />
    </LinearLayout>

</LinearLayout>

Here is my landscape layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dip" >

    <EditText
        android:id="@+id/passText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:hint="Passcode"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/messageText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="top|left"
        android:hint="Message" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/encodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Encode" />

        <Button
            android:id="@+id/decodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Decode" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Views: 352

Answers (1)

Rawr
Rawr

Reputation: 2224

Try setting your android:layout_height on the items expanding/shrinking to wrap_content. If you read here:

http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#attr_android:layout_height

It talks about the three possible values and what they do.

fill_parent: The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.

match_parent: The view should be as big as its parent (minus padding). Introduced in API Level 8.

wrap_content: The view should be only big enough to enclose its content (plus padding).

You should be using wrap_content on a couple of those items. Below I added an example of which heights I would have changed for your vertical layout:

<EditText
    android:id="@+id/passText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:hint="Passcode"
    android:inputType="textPassword" />

<EditText
    android:id="@+id/messageText"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="top|left"
    android:hint="Message" >

    <requestFocus />
</EditText>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:orientation="horizontal"
    android:weightSum="100" >

    <Button
        android:id="@+id/encodeButton"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:text="Encode" />

    <Button
        android:id="@+id/decodeButton"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:text="Decode" />
</LinearLayout>

Upvotes: 1

Related Questions