-1

I would like to simply divide a Linear Layout to 3 piece. I did something as in the code below. If I change the weight of second inner layout anything else than 2, it works. But if I change it to 2(two), the second inner layout doesn't appear in the design ?

<LinearLayout
android:id="@+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    >
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:gravity="center"
    >
    <ImageView
        android:id="@+id/HomePageIcon"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingHorizontal="33dp"
    android:paddingBottom="30dp"
    >
</LinearLayout>

Kara
  • 6,115
  • 16
  • 50
  • 57
Tugberk
  • 41
  • 1
  • 7

5 Answers5

1

Use it like below height set to 0dp when orientation is vertical.

<LinearLayout
    android:id="@+id/Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    android:orientation="vertical"
    android:gravity="center"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:layout_weight="1"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@color/background_dark"
        android:gravity="center"
        >
        <ImageView
            android:id="@+id/HomePageIcon"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:adjustViewBounds="true" />
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical"
        android:paddingHorizontal="33dp"
        android:paddingBottom="30dp"
        >
    </LinearLayout>
ADM
  • 20,406
  • 11
  • 52
  • 83
  • One more question, is it better to use Constraint Layouts in such cases ? – Tugberk Jan 31 '18 at 05:29
  • For dividing in `Weight` `LinearLayout` is better solution . – ADM Jan 31 '18 at 05:32
  • When I develop an interface, the locations of buttons and padding between them changes dramatically when I change the screen size, even I use dp values. To prevent this can I use Constraint Layout, or do you have any other advice. Thanks in advance. – Tugberk Jan 31 '18 at 05:36
0

Use the Constraint Layout and forget the Linear and Relative layout.

vishalpa
  • 99
  • 1
  • 4
  • What is the advantage of using Constraint Layout instead of Linear or Relative layout. – Tugberk Jan 31 '18 at 05:13
  • @Tugberk Constraint layout helps to have flat view hierarchy which intern improves performance while layout is rendered. It has hell lot of advantages. Please check out this link https://codelabs.developers.google.com/codelabs/constraint-layout/ – vishalpa May 17 '18 at 07:19
0

Pls try this,

<LinearLayout
    android:id="@+id/Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    android:orientation="vertical"
    android:gravity="center"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        >
        <ImageView
            android:id="@+id/HomePageIcon"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:adjustViewBounds="true" />
    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:paddingHorizontal="33dp"
        android:paddingBottom="30dp"
        >
    </LinearLayout>
Subin Babu
  • 1,515
  • 2
  • 24
  • 50
0

As you are using layout_weight that means your inner layouts will take the height as much as is available to it. So, in that case there is no need of using layout_height = "wrap_content" just change it to android:layout_height="0dp"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:weightSum="4">

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:gravity="center">

    <ImageView
        android:id="@+id/HomePageIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:paddingBottom="30dp"
    android:paddingHorizontal="33dp"></LinearLayout>

</LinearLayout>
Zeero0
  • 2,602
  • 1
  • 21
  • 36
0

When you are using weight than use 0dp to android:layout_width or android:layout_height which you want for affect using weight. And its good to use android:weightSum in parent layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical"
    android:weightSum="4">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center">

        <ImageView
            android:id="@+id/HomePageIcon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingBottom="30dp"
        android:paddingHorizontal="33dp"></LinearLayout>
Upendra Shah
  • 2,218
  • 17
  • 27