0

I am trying to implement a screen which looks similar to this one:

enter image description here

I am able to implement the above. I have used the following logic :

FrameLayout
  |- Linear Layout
    |- Button (Purple Colour, Layout Width = 1)
    |- Button (Blue Colour, Layout Width = 1)
  |- Linear Layout (Layout_Gravity = center)
    |- Button (Button Text)

Source Code

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/accent_material_light"
        android:orientation="vertical"
        android:padding="0dp"
        android:weightSum="100">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="40"
            android:background="@android:color/holo_purple" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="60"
            android:background="@android:color/holo_blue_light" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@color/abc_search_url_text_pressed">

        <Button
            android:id="@+id/button2"
            android:layout_height="48dp"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="match_parent" />
    </LinearLayout>


</FrameLayout>

Question :-

How should I place the Button on the border of two views if the two views (purple and blue views) have to take 40% and 60% of the screen respectively. I cannot use layout_gravity to center in that case. How to solve this problem ?

It looks something like this :-

enter image description here

How to fix this problem ?

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
  • For me, I would replace `FrameLayout` with `RelativeLayout`, remove the 2nd `LinearLayout` and add ` android:layout_below="@+id/imageButton"` to the button and may give some top margin too. – isamirkhaan1 May 04 '18 at 15:42
  • 1
    Does this answer your question? [Android: Placing ImageView on overlap between layouts](https://stackoverflow.com/questions/25127468/android-placing-imageview-on-overlap-between-layouts) – AaA Sep 07 '21 at 07:49

2 Answers2

2

For better performance, it's necessary to reduce hierarchy. Here, instead of using two/three level hierarchy, you can use the only constraint layout as root layout.

In constraint layout takes button which has constraint

 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

this will set button in centre, After of that set two imageview

imageView1:

 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintBottom_toTopOf="id:button"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

imageView2:

 app:layout_constraintTop_toBottomOf="id:button"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

Multi-level heirarchy takes long time to load UI. So, try to reduce it.

Pang
  • 9,564
  • 146
  • 81
  • 122
Meet Patel
  • 234
  • 1
  • 10
  • Thanks @Meet Patel for answering the question. I will try it out and accept. One more question - How do you ensure that button comes on top of the two linear layouts and not below them ? – Sachin Jain May 05 '18 at 02:44
  • In constarint layout we define constraint of each views. Here, I assign button's 4 diffrent constraint which try to render it left,right,top and bottom. eventually button will takes center position.In Imageview I give bottom constrainsts of button which will place image center in remainning space. – Meet Patel May 05 '18 at 03:06
  • Could you please show an screenshot which shows with explained layout you have achieved the layout requested in OP question (their first screenshot)? With your explanation there will be a gap exactly size of button in middle of view. – AaA Sep 07 '21 at 07:46
0

Easier solution can be ConstraintsLayout. below is the solution with combination of Linear and relative Layout.

<RelativeLayout 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="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark"
        android:gravity="center">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_purple"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:gravity="center">

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>

</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Install" />
</RelativeLayout>
ADM
  • 20,406
  • 11
  • 52
  • 83