-1

I'm trying to set android:layout_weight="1" but it does not do anything.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:background="#fbd0d5"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.raza.helloworld.MainActivity"
    tools:showIn="@layout/activity_main"
    android:weightSum="1">


    <TextView

        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Minnions"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Happy"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Birthday"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />
</LinearLayout>

What's wrong with this code?

BSMP
  • 4,596
  • 8
  • 33
  • 44
Raza Khan
  • 19
  • 4

6 Answers6

0

Changes the weightsum to 3 in parent linear Layout

android:weightSum="3"
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

just remove weigtsum and showIn

<?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:orientation="vertical"
    android:layout_height="match_parent"
    android:background="#fbd0d5"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <TextView

        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Minnions"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Happy"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Birthday"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />
</LinearLayout>
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
0

Remove android:weightSum="1" from root layout

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:background="#fbd0d5"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >


    <TextView

        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Minnions"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Happy"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fef65b"
        android:text="Birthday"
        android:textColor="#f44242"
        android:textSize="60sp"
        android:textStyle="bold" />
</LinearLayout>
Komal12
  • 3,340
  • 4
  • 16
  • 25
0

You need to change the weightsum of your LinearLayout to equal 3.

android:weightSum="3"

This other thread povides a bit more detail about weight sum but the gist of it is:

The child elements layout_weight should equal to the parent's weight_sum.

Troy Poulter
  • 677
  • 1
  • 8
  • 29
0

All your views have the same weight!. So none of their height will be changed.change weightSum to be equal to the sum of weight of its children and set different weights for child views. It'll Change then.

Like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fbd0d5"
android:orientation="vertical"
android:weightSum="10"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:background="#fef65b"
    android:text="Minnions"
    android:textColor="#f44242"
    android:textSize="60sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:background="#fef65b"
    android:text="Happy"
    android:textColor="#f44242"
    android:textSize="60sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:background="#fef65b"
    android:text="Birthday"
    android:textColor="#f44242"
    android:textSize="60sp"
    android:textStyle="bold" />
</LinearLayout>
Adithya
  • 1,688
  • 1
  • 10
  • 18
0
<?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">


<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:background="#fef65b"
    android:gravity="center"
    android:text="Hai"
    android:textColor="#f44242"
    android:textSize="60sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:background="#fef65b"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#f44242"
    android:textSize="60sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:background="#fef65b"
    android:gravity="center"
    android:text="How r u"
    android:textColor="#f44242"
    android:textSize="60sp"
    android:textStyle="bold" />
</LinearLayout>
Raj
  • 477
  • 5
  • 20