Relative layout with wrap content

relativelayout
relativelayout measure
constraint layout wrap content
relative layout horizontal
relative layout to bottom of
relativelayout padding
how to change constraintlayout to relativelayout
android relativelayout align bottom

I want to customize AlertDialog to have a fixed header and a footer with scrollable RecyclerView. So I tried to do it with RelativeLayout but if there are many items in RecyclerView then the footer hides below RecyclerView. And I add fixed height to the RecyclerView then if there are fewer items then there is extra space between RecyclerView and footer.

With RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
    android:gravity="center"
android:layout_centerVertical="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/main"
        android:orientation="vertical">
        <TextView
           android:text="Add Additional Information"
           android:id="@+id/additionalInfoTitle"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textSize="16dp"
           android:gravity="center"
           android:textColor="@color/white"
           android:textStyle="bold"
           android:background="@drawable/popupTitle"
           android:padding="@dimen/_10sdp"/>
       <android.support.v7.widget.RecyclerView
           android:id="@+id/additionalDataList"
           android:scrollbars="vertical"
           android:layout_alignParentTop="true"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>

        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/drlBottomMenu"
        android:layout_height="wrap_content"
        android:background="@color/offWhite"
        android:orientation="vertical"
        android:layout_below="@+id/main">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Continue"
            android:layout_gravity="center"
            android:id="@+id/continueButton"
            style="@style/buttonStyle"
            android:backgroundTint="@color/colorPrimary"
            android:layout_weight="0.5"
            android:textAllCaps="false"
            android:padding="12dp"
            android:layout_marginTop="@dimen/_12sdp"
            android:layout_marginLeft="@dimen/_20sdp"
            android:layout_marginRight="@dimen/_20sdp"
            android:textColor="@color/white"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:padding="12dp"
            android:layout_gravity="center"
            android:id="@+id/cancelButton"
            android:textAllCaps="false"
            style="@style/buttonStyle"
            android:backgroundTint="@color/gray"
            android:layout_weight="0.5"
            android:textColor="@color/white"
            android:layout_marginBottom="@dimen/_12sdp"
            android:layout_marginLeft="@dimen/_20sdp"
            android:layout_marginRight="@dimen/_20sdp"
        />
    </LinearLayout>
</RelativeLayout>

With LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/roundedCorner">
  <TextView
        android:text="Add Additional Information"
        android:id="@+id/additionalInfoTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="16dp"
        android:gravity="center"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:background="@drawable/popupTitle"
        android:padding="@dimen/_10sdp"/>
  <android.support.v7.widget.RecyclerView
    android:id="@+id/additionalDataList"
    android:scrollbars="vertical"
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"/>
  <Button
        android:text="Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/continueButton"
        style="@style/buttonStyle"
        android:backgroundTint="@color/colorPrimary"
        android:layout_marginTop="@dimen/_12sdp"
        android:layout_marginLeft="@dimen/_20sdp"
        android:layout_marginRight="@dimen/_20sdp"
    />
     <Button
        android:text="Cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cancelButton"
        style="@style/buttonStyle"
        android:backgroundTint="@color/gray"
        android:layout_marginBottom="@dimen/_12sdp"
        android:layout_marginLeft="@dimen/_20sdp"
        android:layout_marginRight="@dimen/_20sdp"
    />
</LinearLayout>

Can anyone please help me in designing this. Desired output should be:

EDIT After using constraintlayout I am getting following result:

Try this

Activity code

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    RecyclerView additionalDataList;
    ArrayList<String> arrayList = new ArrayList<>();
    DataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.test);

        Window window = dialog.getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);

        additionalDataList = dialog.findViewById(R.id.additionalDataList);
        additionalDataList.setLayoutManager(new LinearLayoutManager(this));
        additionalDataList.setHasFixedSize(true);

        addDataToList();

        adapter = new DataAdapter(this, arrayList);
        additionalDataList.setAdapter(adapter);

        dialog.show();
    }

    private void addDataToList() {
        for (int i = 0; i < 50; i++) {
            arrayList.add("Item :" + i);
        }
    }
}

layout.test

<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:id="@+id/additionalInfoTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:padding="10dp"
        android:text="Add Additional Information"
        android:textColor="@android:color/white"
        android:textSize="16dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/additionalDataList"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/additionalDataList"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/drlBottomMenu"
        app:layout_constraintEnd_toEndOf="@id/drlBottomMenu"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/additionalInfoTitle" />


    <LinearLayout
        android:id="@+id/drlBottomMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/additionalDataList">

        <Button
            android:id="@+id/continueButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="12dp"
            android:layout_marginRight="20dp"
            android:layout_weight="0.5"
            android:padding="12dp"
            android:text="Continue"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            app:backgroundTint="@color/colorPrimary" />

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="12dp"
            android:layout_marginRight="20dp"
            android:layout_weight="0.5"
            android:padding="12dp"
            android:text="Cancel"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            app:backgroundTint="@android:color/darker_gray" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

OUTPUT

output when list has few items

output when list has more items

match_parent like view inside wrap_content RelativeLayout · GitHub, Match parent view inside wrap content RelativeLayout. <?xml version="1.0" <​LinearLayout xmlns:android="http://schemas.android.com/apk/res/android". I want to customize AlertDialog to have a fixed header and a footer with scrollable RecyclerView. So I tried to do it with RelativeLayout but if there are many items in RecyclerView then the footer

Try using MaterialDialog or AlertDialog, and then create the layout with constrainlayout + guidelines. It's way more easy.

Relative Layout is Dead. Meet Constraint Layout, match_parent — Match parent will be the same size as the ConstraintLayout. It will still use margins and padding. wrap_content — Wrap content  These are just a few examples. All layout attributes are documented at RelativeLayout.LayoutParams. The value for each layout property is either a boolean to enable a layout position relative to the parent RelativeLayout or an ID that references another view in the layout against which the view should be positioned.

<android.support.v7.widget.RecyclerView
    android:id="@+id/additionalDataList"
    android:scrollbars="vertical"
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
 android:layout_marginBottom="height of bottom header"<-------------add this line
/>

or you can use constraintlayout and set height to 0dp so recylerview can take up availabe space

one more option is to use linear layout and set it to wrap content and dynamically inflate views

RelativeLayout, We will also explore some of the specific layout controls available for organising the screen content namely – Android LinearLayout and Android RelativeLayout. android:layout_width=wrap_content tells the view to size itself to the dimensions required by its content. android:layout_width= match_parent tells the view to become as big as its parent view. View Identification

Try using Constraint inside constraint like

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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.support.constraint.ConstraintLayout
        android:id="@+id/main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/additionalInfoTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:background="@android:color/darker_gray"
            android:gravity="center"
            android:padding="10dp"
            android:text="Add Additional Information"
            android:textColor="@android:color/white"
            android:textSize="16dp"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/additionalDataList"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_alignParentTop="true"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_weight="8"
            android:scrollbars="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/additionalInfoTitle" />

        <Button
            android:id="@+id/continueButton"
            style="@style/buttonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:backgroundTint="@color/colorPrimary"
            android:text="Login"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/additionalDataList" />

        <Button
            android:id="@+id/cancelButton"
            style="@style/buttonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:backgroundTint="@color/gray"
            android:text="Cancel"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/continueButton" />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

Android Layout - LinearLayout, RelativeLayout, A wiki engine is a type of content management system, but it differs from most other android:text="This is a Wrap text"/> </RelativeLayout>. @Jjohnny didn't the outcome produced blank space beneath the relative layout.I am also stuck in a problem where me using relative layout within stack layout but if I position any element below relative layout as stacklayout child,I am getting blank space between the relative layout and the other child element.Am I doing anything wrong or missing anything?

What is difference between FillParent and wrap content in android?, android wrap-content example1 just as big as it's parent “ RelativeLayout “, but button's height is still big enough to enclose it's content only. Relative Layout In Android With Example. The Relative Layout is very flexible layout used in android for custom layout designing. It gives us the flexibility to position our component/view based on the relative or sibling component’s position.

Android wrap_content and fill_parent example, Android Layout Params - Wrap-Content , Match Parent and Fill_Parent - Learn android:text="My Button" android:background="#008080" /> </RelativeLayout>. Set button align to right side of android screen inside relative layout. Inside relative layout application developer can easily set views alignments with particular given attributes. So here is the complete step by step tutorial for Align view to right side of relativelayout android using xml.

XML- Layout Params, RelativeLayout is also a Layout that you might have used in the past, and What happens when you set the container to Wrap Content on the  Modify the default content of res/layout/activity_main.xml file to include few widgets in Relative layout. 3: Define required constants in res/values/strings.xml file: 4: Run the application to launch Android emulator and verify the result of the changes done in the application.

Comments
  • If there are fewer items then too the popup takes up the almost entire screen. I want it to wrap as per the number of items.
  • @Arti check updated answer try using ConstraintLayout
  • it is a better solution for your query @Arti
  • Above doesn't work it takes the full screen with empty space in between.
  • @Arti i have tested the above and its working for me can u share your code for alert dialog