Search code examples
androidandroid-layoutandroid-linearlayoutandroid-relativelayout

Android Layout- Constraint between the RelativeLayout and LinearLayout that has the same parent


I have a layout file in my Android application like below;

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/myPage"
        android:theme="@style/login"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

           <ImageView
                android:id="@+id/myimageview"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                android:src="@drawable/mypicture"/>
            <com.myapp.RobotoTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/myimageview"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="@string/mystring"
                android:textColor="#fff"
                android:textSize="12sp"
                app:rt_fontWeight="light" />

        </RelativeLayout>

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_alignParentBottom="true"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:orientation="vertical">

<android.support.design.widget.TextInputLayout
            android:id="@+id/password_text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            app:theme="@style/loginTheme">
              <android.support.v7.widget.AppCompatEditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:hint="@string/password"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout> 
        </LinearLayout>
    </RelativeLayout>

I want to put space between the RelativeLayout that contains the RobotoTextView and the LinearLayout under this layout.They are in the same level in my layout file.Also my app has to be run on different screen resolutions.

It must be something like a constraint between the RobotoTextView and the LinearLayout under this text view.

I tried android:layout_marginTop in the LinearLayout but it did not work. RobotoTextView overlapped with the text view under the LinearLayout


Solution

  • Add this attribute to the bottom layout that gives it an id:

    android:id="@+id/bottom"
    

    and these to the top layout:

    android:layout_marginBottom="10dp"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/bottom"
    

    you can change 10dp to anything you like.
    Also remove

    android:layout_weight="1"
    

    since it applies only to views inside LinearLayout