0

I am trying to place a button overlapping two layouts. The Layouts must have the layout_weight as shown in the image below, I've been struggling on it for a while, .. I succeeded with the below code .. but only for api 22 (lollipop) & 23 (MarshMallow).. Problem occurs in API 19 (Kitkat) & below .. The Lower layout seem to cover the button .. ie, button is half visible from the top.

Please help me to achieve like in image with all android version support .. TIA !

enter image description here

Code which worked on API 22 & 23 but not in 19 and Below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.automovill.automovill.testing">


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

    <LinearLayout
        android:id="@+id/LL1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".55"
        android:background="#2961a7"
        android:orientation="vertical">

    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="-25dp"
        android:background="#ffd016"
        android:padding="10dp"
        android:text="TESTING"
        android:textColor="#000"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/LL2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-25dp"
        android:layout_weight=".45"
        android:background="#163d6d">

    </RelativeLayout>

</LinearLayout>

VipiN Negi
  • 2,994
  • 3
  • 24
  • 47

3 Answers3

0

To be honest, I think you'll have to perform all kinds of crazy gymnastics to get the standard layouts do this. LinearLayout is not the right tool because the expectation is that the elements are all adjacent to each other (you may have been exploiting a bug before).

You can do standard layout gymnastics, or you can go ahead a write your own ViewGroup to manage the layout positions of the child view to conform to your needs. You'll end up having to compute pixels and all that. Probably no way to avoid these manual computations since the usual layout typically expect view adjacency rather than overlap.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Maybe try it with adding the following code to your button's layout:

android:layout_marginBottom="-25dp"
Strider
  • 4,452
  • 3
  • 24
  • 35
  • adding android:layout_marginBottom="-25dp" will again put the button on its original location without margins ie, top of the lower layout, .. as it already have android:layout_marginTop="-25dp – VipiN Negi Feb 10 '16 at 08:54
0

I would try to move the button at the top of the layout file. There is an implicit rule in RelativeLayout about the z ordering of the views. The further down the view the higher is the z value.

Community
  • 1
  • 1
Janusz
  • 187,060
  • 113
  • 301
  • 369
  • correct, if I place the button following the z order, yet pushing the button with -ve margin top push the button below the top layout in any API level. I can't even place the button in the main Parent, as it has to be Linear Layout with the weightsum. – VipiN Negi Feb 10 '16 at 08:51
  • Sorry I don't really understand your comment. – Janusz Feb 10 '16 at 12:52
  • I meant, I've tried by following the z order also. It will not work for any API .. unless its a Relative Layout. If you have something in mind plz help me with the codes .. – VipiN Negi Feb 10 '16 at 14:29
  • Ah now I see the button is not inside a RelativeLayout. Sorry then my answer is not helpful at all. – Janusz Feb 11 '16 at 08:56