Alexandre
Alexandre

Reputation: 47

Android layout design not the same when app is running

The design of my layout is not the same in Android Studio preview and in the running app :

Design in running app

enter image description here

Expected design

enter image description here

Here's the XML file :

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

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

        <Button
            android:text="Manage messages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bt_messages"
            android:drawableLeft="@mipmap/ic_message"
            android:layout_weight="1" />

        <Button
            android:text="Manage activities"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bt_activities"
            android:drawableLeft="@mipmap/ic_activities"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

Do you have an idea of what's happening ?

Thanks in advance for your help

Upvotes: 2

Views: 663

Answers (3)

Chetan Joshi
Chetan Joshi

Reputation: 5721

You are supposed to use android:weightSum in place of android:layout_weight for your inner LinearLayout
Also you should add android:layout_width="0dp" for the children while using weightSum

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

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

        <Button
            android:text="Manage messages"
            android:layout_width="0dp"
           android:layout_height="wrap_content"
            android:id="@+id/bt_messages"
            android:drawableLeft="@mipmap/ic_message"
            android:layout_weight="1" />

        <Button
            android:text="Manage activities"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/bt_activities"
            android:drawableLeft="@mipmap/ic_activities"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

GeekyInt
GeekyInt

Reputation: 393

You can do like this

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

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

    <Button
        android:text="Manage messages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bt_messages"
        android:drawableLeft="@mipmap/ic_message"
        android:layout_weight="1" />

    <Button
        android:text="Manage activities"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bt_activities"
        android:drawableLeft="@mipmap/ic_activities"
        android:layout_weight="1" />
</LinearLayout>

Upvotes: 1

Kuldeep Kulkarni
Kuldeep Kulkarni

Reputation: 796

You can try this:

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

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

        <Button
            android:text="Manage messages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bt_messages"
            android:drawableLeft="@mipmap/ic_message"
            android:layout_weight=".5" />

        <Button
            android:text="Manage activities"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bt_activities"
            android:drawableLeft="@mipmap/ic_activities"
            android:layout_weight=".5" />
    </LinearLayout>

</LinearLayout>

Upvotes: 0

Related Questions