-1

So I have an XML for my activity, and I want to put another view on top of whatever this XML has. The new view covers the entire screen (match_parent for both width and height). I use LayoutInflater.inflate(int,View) to inflate the new view into my root view. The new view is in front of everything but the last button. Even calling bringToFront or bringChildToFront doesn't do anything. If I wrap this button in another LinearLayout, it will solve the problem, but I don't want to do that. This only happens with Button.

Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".Activities.LoginActivity"
    android:background="@drawable/background"
    android:id="@+id/root_view">

    <LinearLayout
        android:id="@+id/middle_views"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:gravity="center">

        <EditText
            android:id="@+id/user_name_edit_text"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:padding="10dp"
            android:layout_margin="@dimen/small_dp"
            android:background="@drawable/grey_edit_text_background"
            android:inputType="textEmailAddress"
            android:hint="@string/email_label"
            android:fontFamily="@font/bariol"
            android:textSize="@dimen/medium_sp"/>

        <EditText
            android:id="@+id/password_edit_text"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:padding="10dp"
            android:layout_marginBottom="@dimen/big_dp_1"
            android:background="@drawable/grey_edit_text_background"
            android:inputType="textPassword"
            android:hint="@string/password_label"
            android:fontFamily="@font/bariol"
            android:textSize="@dimen/medium_sp"/>

        <Button
            android:onClick="onSignInClick"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:background="@drawable/orange_gradient_button"
            android:text="@string/login_label"
            android:textColor="#fff"
            android:fontFamily="@font/bariol"
            android:textStyle="bold"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/middle_views"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_margin="@dimen/medium_dp">

        <ImageView
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:src="@drawable/banner"
            android:layout_marginBottom="@dimen/big_dp_3"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/middle_views"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginTop="@dimen/small_dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/or_label"
            android:fontFamily="@font/bariol"
            android:textSize="@dimen/big_sp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:background="@drawable/facebook_icon"
                android:layout_margin="@dimen/medium_dp"/>

            <Button
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:background="@drawable/twitter_icon"
                android:layout_margin="@dimen/medium_dp"/>

            <Button
                android:layout_width="45dp"
                android:layout_height="45sp"
                android:background="@drawable/google_icon"
                android:layout_margin="@dimen/medium_dp"/>

        </LinearLayout>

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:paddingStart="@dimen/big_dp_1"
        android:paddingEnd="@dimen/big_dp_1"
        android:layout_margin="@dimen/big_dp_2"
        android:background="@drawable/white_button"
        android:text="@string/create_new_account_label"
        android:textSize="@dimen/small_sp"
        android:textAllCaps="false"
        android:textStyle="normal"
        android:fontFamily="@font/bariol"/>

</RelativeLayout>

New view XML (InfoDialog is a subclass of RelativeLayout):

<?xml version="1.0" encoding="utf-8"?>
<com.sseo242.airua.CustomViews.InfoDialog
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/semiBlack"
    android:focusable="true"
    android:clickable="true">

    <LinearLayout
        android:id="@+id/dialog_board"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@drawable/dialog_background">

        <TextView
            android:id="@+id/info_dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/medium_dp"
            android:text="title"
            android:textStyle="bold"
            android:fontFamily="@font/bariol"
            android:textSize="@dimen/large_sp"
            android:textColor="#003f66"/>

        <TextView
            android:id="@+id/info_dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/big_dp_1"
            android:layout_marginStart="@dimen/big_dp_2"
            android:layout_marginEnd="@dimen/big_dp_2"
            android:gravity="center"
            android:text="Content"
            android:textColor="#333"
            android:fontFamily="@font/bariol"
            android:textSize="@dimen/big_sp"/>

        <Button
            android:id="@+id/info_dialog_ok_button"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:layout_marginBottom="@dimen/big_dp_1"
            android:background="@drawable/orange_gradient_button"
            android:text="@string/ok_button"
            android:textColor="#fff"
            android:fontFamily="@font/bariol"
            android:textStyle="bold"
            android:textSize="@dimen/big_sp"/>

    </LinearLayout>

</com.sseo242.airua.CustomViews.InfoDialog>

Method I use to show the new view:

public void showInfoDialog(BaseActivity activity, String title, String message,
                               @Nullable DialogOnClickHandler handler){
        LayoutInflater inflater = activity.getLayoutInflater();
        ViewGroup rootView = activity.getRootView();
        int index = rootView.getChildCount();
        InfoDialog dialog = (InfoDialog)((ViewGroup) inflater.inflate(R.layout.dialog_info, rootView))
                .getChildAt(index);
        rootView.bringChildToFront(dialog);
        dialog.setContent(title, message, handler);
    }
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
Son Nguyen
  • 1,124
  • 2
  • 10
  • 24
  • you need to make a container layout like linear or relative then inflate your view in that container. – Umair May 23 '18 at 05:05
  • any reason why it still puts the button in front of the newly inflated view? I don't really want to create a dummy view as a container view. – Son Nguyen May 23 '18 at 05:07
  • @SonNguyen the reason your button still shows up is that you are inflating your your layout inside your parent layout i.e relativeLayout. and giving layout_alignParentBottom will make sure it is displayed there and your inflated layout will take rest of the place. – Umair May 23 '18 at 05:14
  • @pskink yes, it can show/dismiss – Son Nguyen May 23 '18 at 05:17
  • I'm not using Android dialog. I'm making a custom one. – Son Nguyen May 23 '18 at 14:18

1 Answers1

0

I would suggest you to use FrameLayout instead of RelativeLayout. You can easily overlap two views using that. An example for this can be found here :-

How to layer views

Place one view over another

Ganesh K
  • 126
  • 14