Linear layout's weight not working on phone, working fine in xml preview in android studio

linear layout in android javatpoint
linear layout vs constraint layout
relative layout in android
linear layout and relative layout in android studio
absolute layout in android
android linearlayout spacing
frame layout in android
how to change to linear layout in android studio

I have used layout_weight in LinearLayout in my xml file. The preview is fine, but when i run the app in a phone, the view is different. This is the xml file

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

        <!--<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_dashboard"
            android:textSize="25sp"/>-->

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp">

        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/ivBrandImage"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_centerVertical="true"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:id="@+id/tBrandName"
            android:layout_toRightOf="@+id/ivBrandImage"
            android:text="Email name"
            android:layout_marginLeft="@dimen/margin_left_brand_name"
            android:layout_centerVertical="true"/>

        <android.support.v7.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tBrandName"
            android:layout_toRightOf="@+id/ivBrandImage"
            android:layout_marginLeft="@dimen/margin_left_brand_name"
            android:layout_marginTop="6dp"
            android:id="@+id/switchBrand"/>

    </RelativeLayout>

    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/margin_top_dashboard_card"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="@dimen/margin_left_dashboard"
            android:text="@string/textview_pending_approvals"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/margin_top_dashboard_card"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin_left_dashboard"
            android:text="@string/textview_pending_approvals"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/margin_top_dashboard_card"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin_left_dashboard"
            android:text="@string/textview_pending_approvals"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

</LinearLayout>

please make it for all resolution like hdpi ,xhdip,xxhdpi,xxxhdpi. please select your tested device resolution from here then test it again.

Linear Layout, Linear layout is a simple layout used in android for layout designing. 1 Types Of Linear Layout Orientation; 2 Main Attributes In Linear Layout: 3 Example of  Android LinearLayout is a view group that aligns all children in either vertically or horizontally.

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

    <!--<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_dashboard"
        android:textSize="25sp"/>-->

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="16dp">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:id="@+id/ivBrandImage"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"
                android:layout_centerVertical="true"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:id="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:text="Email name"
                android:layout_marginLeft="@dimen/button_padding"
                android:layout_centerVertical="true"/>

            <android.support.v7.widget.SwitchCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:layout_marginLeft="@dimen/button_padding"
                android:layout_marginTop="6dp"
                android:id="@+id/switchBrand"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/button_padding"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="@dimen/button_padding"
            android:text="fjdkjk"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/button_padding"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/button_padding"
            android:text="jdfkjffjkjk"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/button_padding"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/button_padding"
            android:text="jkgjk"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

</LinearLayout>

import it -

compile 'com.android.support:cardview-v7:21.0.+'

working fine in MI4

Android Linear Layout, In android, LinearLayout is a ViewGroup subclass which is used to render all child View instances one by one either in Horizontal direction or Vertical direction​  LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no… Platform Android Studio Google Play Jetpack Kotlin Docs News

Try the following:

Use android:layout_height="0dp" and android:layout_weight="1" for the first CardView(which contains the Image of Android logo).

Linear Layout Tutorial With Examples In Android, LinearLayout is a ViewGroup that is responsible for holding views in it. It is a layout that arranges its children i.e the various views and layouts linearly (one after  Linear layout is a simple layout used in android for layout designing. In the Linear layout all the elements are displayed in linear fashion means all the childs/elements of a linear layout are displayed according to its orientation. The value for orientation property can be either horizontal or vertical.

Changing the parent layout of xml file from LinearLayut to RelativeLayout solved the issue. Of course, some minor changes were required to achieve the goal. Here is the xml file, which runs fine on the phone too.

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:id="@+id/cvDashboardHeader">

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:id="@+id/ivBrandImage"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"
                android:layout_centerVertical="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:id="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:text="Email name"
                android:layout_marginLeft="@dimen/margin_left_brand_name"
                android:layout_centerVertical="true"/>

            <android.support.v7.widget.SwitchCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:layout_marginLeft="@dimen/margin_left_brand_name"
                android:layout_marginTop="6dp"
                android:id="@+id/switchBrand"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/cvDashboardHeader">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_marginTop="@dimen/margin_top_dashboard_card"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:layout_marginLeft="@dimen/margin_left_dashboard"
                android:text="@string/textview_pending_approvals"
                />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin_top_dashboard_card"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/margin_left_dashboard"
                android:text="@string/textview_pending_approvals"
                android:gravity="center_vertical"
                />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin_top_dashboard_card"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/margin_left_dashboard"
                android:text="@string/textview_pending_approvals"
                android:gravity="center_vertical"
                />

        </android.support.v7.widget.CardView>

    </LinearLayout>

</RelativeLayout>

Android LinearLayout with Examples, LinearLayout – Horizontal. Open “res/layout/main.xml” file, add 3 buttons within LinearLayout , with “horizontal” orientation. In this  LinearLayout(Context, IAttributeSet, Int32, Int32) LinearLayout(Context, IAttributeSet, Int32, Int32) LinearLayout(IntPtr, JniHandleOwnership) LinearLayout(IntPtr, JniHandleOwnership) A constructor used when creating managed representations of JNI objects; called by the runtime.

In case of LinearLayout.It is demo through below code .You can solve your problem. Use below code.

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10sp"
        android:weightSum="3">

        <TextView
            android:id="@+id/text_kpis"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/text_color_dark"
            android:textSize="@dimen/text_size_13" />


        <TextView
            android:id="@+id/text_target"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/txtbox_text_color_darek"
            android:textSize="@dimen/text_size_13" />


        <TextView
            android:id="@+id/text_actual"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/txtbox_text_color_darek"
            android:textSize="@dimen/text_size_13" />
    </LinearLayout>

In case of Relatvie Layout Use Below code.

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="10sp"
        android:textColor="@android:color/white"
        >

        <TextView
            android:id="@+id/tv_kpis"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/kpi"
            android:layout_alignParentLeft="true"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_16" />

        <TextView
            android:id="@+id/tv_target"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/traget"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_16" />


        <TextView
            android:id="@+id/tv_actual"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:text="@string/actual"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_16" />


    </RelativeLayout>

Linear Layout in Android, The simplest solution would be using empty views with weights as separators. <​LinearLayout android:layout_width="match_parent"  The basic behaviour of LinearLayout is to flow its children in either a horizontal or vertical direction, depending on the orientation. Achieving that is really simple in the editor.

14. INTRODUCTION TO LINEAR LAYOUT IN ANDROID STUDIO , Attributes of linear layout. Following are some attributes of linear layout of Android. Id: Unique identifier of layout. Orientation: Attribute to set linear  Android Layout The basic building block for user interface is a View object that is created from the View class and occupies a rectangular area on the screen. Views are the base class for UI components like TextView, Button, EditText etc. The ViewGroup is a subclass of View.

Android LinearLayout example, LinearLayout is a ViewGroup that displays child View elements in a linear direction, either vertically or horizontally. You should be careful about over-using the LinearLayout. If you begin nesting multiple LinearLayout s, you may want to consider using a RelativeLayout instead. Start a new project named HelloLinearLayout.

how to align an element to right in horizontal LinearLayout?, You have to set the weight property of your elements. Create three RelativeLayouts as children to your LinearLayout and set weights 0.15, 0.70, 0.15.

Comments
  • how are you inflating this xml layout?
  • This is a layout of what? Obviously, parental view of your LinearLayout has android:layout_height="wrap_content" property. So, what is the hierarchy of layouts?
  • This is one of the fragments of a navigation drawer. I have created a navigation drawer activity from the studio's inbuilt options. So, the hierarchy is somewhat like, DrawerLayout(activity_main.xml) > CoordinatorLayout(app_bar_main.xml) > CardView (whose code I've pasted in the question). All the three parts are in different xml files. "include" attribute is used to include files in the hierarchy I've mentioned.
  • The layout looks fine in all the resolutions in the studio. But, doesn't work out in the device. I'm using Moto E2 (Lollipop)
  • I've already included compile 'com.android.support:cardview-v7:23.3.0' in the gradle file.
  • That would make the whole card disappear. weight should be greater than 0.