How to keep one layout over another layout in android

include layout android programmatically
how to access include layout in android
android add view on top of another view programmatically
android add layout to another layout programmatically
include xml in another xml android
android merge layout
android layout examples
frame layout in android

This is my xml I want to keep one layout over another layout.

I have progress Bar and try again Textview in same place I am doing visible and invisible Textview and Progressbar programatically, so I want keep Progressbar over try again button, but I am unable to do this.

I have searched from google that we can do using Frame layout but its not working.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tesco_blue"
    android:paddingBottom="@dimen/margin_18"
    android:paddingLeft="@dimen/margin_18"
    android:paddingRight="@dimen/margin_12"
    android:orientation="vertical"
    android:paddingTop="@dimen/margin_18">
    <LinearLayout
        android:id="@+id/securebarcode_error_layout_variant_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:layout_marginLeft="@dimen/margin_56"
        android:layout_marginRight="@dimen/margin_56"
        android:visibility="visible"
        tools:visibility="visible">

        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_40" />

        <TextView
            android:id="@+id/errortitle"
            style="@style/Typeface.Body.Bold.White"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:lineSpacingExtra="4sp"
            tools:text="@string/pwp_network_error" />


        <TextView
            android:id="@+id/errordesc"
            style="@style/Typeface.Body.White"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:layout_marginTop="@dimen/margin_12"
            android:lineSpacingExtra="8sp"
            tools:text="@string/pwp_network_error_description" />
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            >
        <ProgressBar
            android:id="@+id/pwp_progressbarlayout_variant_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"


            android:visibility="visible"
            android:paddingLeft="@dimen/cl_48"
            android:paddingRight="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingBottom="@dimen/padding_12"
            android:indeterminateDrawable="@drawable/custom_progress_bar"/>
        </FrameLayout>

        <TextView
            android:id="@+id/try_again"
            style="@style/Typeface.Body.Bold.HighlightBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/error_button_background"
            android:gravity="center"
            android:paddingLeft="@dimen/cl_48"
            android:paddingRight="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingBottom="@dimen/padding_12"
            android:text="@string/Tryagain"
            android:textColor="@color/fdv_pending_state_color" />


        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_40" />


    </LinearLayout>
</LinearLayout>

Update your Layout as below - Relative Layout can do the trick

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tesco_blue"
    android:paddingBottom="@dimen/margin_18"
    android:paddingLeft="@dimen/margin_18"
    android:paddingRight="@dimen/margin_12"
    android:orientation="vertical"
    android:paddingTop="@dimen/margin_18">


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


        <LinearLayout
            android:id="@+id/securebarcode_error_layout_variant_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:layout_marginLeft="@dimen/margin_56"
            android:layout_marginRight="@dimen/margin_56"
            android:visibility="visible"
            tools:visibility="visible">

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin_40" />

            <TextView
                android:id="@+id/errortitle"
                style="@style/Typeface.Body.Bold.White"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:lineSpacingExtra="4sp"
                tools:text="@string/pwp_network_error" />


            <TextView
                android:id="@+id/errordesc"
                style="@style/Typeface.Body.White"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center"
                android:layout_marginTop="@dimen/margin_12"
                android:lineSpacingExtra="8sp"
                tools:text="@string/pwp_network_error_description" />


            <TextView
                android:id="@+id/try_again"
                style="@style/Typeface.Body.Bold.HighlightBlue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/error_button_background"
                android:gravity="center"
                android:paddingLeft="@dimen/cl_48"
                android:paddingRight="@dimen/cl_48"
                android:paddingTop="@dimen/padding_12"
                android:paddingBottom="@dimen/padding_12"
                android:text="@string/Tryagain"
                android:textColor="@color/fdv_pending_state_color" />


            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin_40" />


        </LinearLayout>

            <ProgressBar
                android:id="@+id/pwp_progressbarlayout_variant_a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_centerInParent="true"
                android:visibility="visible"
                android:indeterminateDrawable="@drawable/custom_progress_bar"/>


    </RelativeLayout>

</LinearLayout>

Re-using layouts with <include/>, Overview · Keep the device awake · Schedule repeating alarms If you already know the layout that you want to re-use, create a new XML file and define the layout. Note: The tools:showIn attribute in the XML above is a special attribute in your view hierarchy when including one layout within another. Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the View, you can do it…

  1. You can simply use constraint layout in your case as basically its from the family of frame layout , you can implement the code below.just take your root layout as ConstraintLayout

    <TextView
        android:id="@+id/try_again"
        style="@style/Typeface.Body.Bold.HighlightBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@drawable/error_button_background"
        android:gravity="center"
        android:paddingLeft="@dimen/cl_48"
        android:paddingRight="@dimen/cl_48"
        android:paddingTop="@dimen/padding_12"
        android:paddingBottom="@dimen/padding_12"
        android:text="@string/Tryagain"
        android:textColor="@color/fdv_pending_state_color" />
    
    <ProgressBar
        android:id="@+id/pwp_progressbarlayout_variant_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/try_again"
        app:layout_constraintBottom_toBottomOf="@id/try_again"
        app:layout_constraintLeft_toLeftOf="@id/try_again"
        app:layout_constraintRight_toRightOf="@id/try_again"
        android:visibility="visible"
        android:paddingLeft="@dimen/cl_48"
        android:paddingRight="@dimen/cl_48"
        android:paddingTop="@dimen/padding_12"
        android:paddingBottom="@dimen/padding_12"
        android:indeterminateDrawable="@drawable/custom_progress_bar"/>
    

Relative Layout, be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout… 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. In your XML layout, dependencies against other views in the layout can be declared in any order.

You have to move your try_again TextView to inside of FrameLayout so the your Progressbar will show on TextView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tesco_blue"
android:orientation="vertical"
android:paddingLeft="@dimen/margin_18"
android:paddingTop="@dimen/margin_18"
android:paddingRight="@dimen/margin_12"
android:paddingBottom="@dimen/margin_18">

<LinearLayout
    android:id="@+id/securebarcode_error_layout_variant_a"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/margin_56"
    android:layout_marginRight="@dimen/margin_56"
    android:orientation="vertical"
    android:visibility="visible"
    tools:visibility="visible">

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_40" />

    <TextView
        android:id="@+id/errortitle"
        style="@style/Typeface.Body.Bold.White"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:lineSpacingExtra="4sp"
        tools:text="@string/pwp_network_error" />


    <TextView
        android:id="@+id/errordesc"
        style="@style/Typeface.Body.White"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/margin_12"
        android:gravity="center"
        android:lineSpacingExtra="8sp"
        tools:text="@string/pwp_network_error_description" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">

        <ProgressBar
            android:id="@+id/pwp_progressbarlayout_variant_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminateDrawable="@drawable/custom_progress_bar"
            android:paddingLeft="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingRight="@dimen/cl_48"
            android:paddingBottom="@dimen/padding_12"
            android:visibility="visible" />


        <TextView
            android:id="@+id/try_again"
            style="@style/Typeface.Body.Bold.HighlightBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/error_button_background"
            android:gravity="center"
            android:paddingLeft="@dimen/cl_48"
            android:paddingTop="@dimen/padding_12"
            android:paddingRight="@dimen/cl_48"
            android:paddingBottom="@dimen/padding_12"
            android:text="@string/Tryagain"
            android:textColor="@color/fdv_pending_state_color" />
    </FrameLayout>


    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_40" />

Here it is the image what you want.

How to place layouts one over another in android using , LayoutParams) to add an additional content view to the screen. Here I will create an xml with a textView and a Edittext and on another layout i Will  In my project there is one button--> on click Go to second Activity And Stream a Video, i.e. I am using 2 xml files too, I want to Play the Video on the same page Where The button is placed, The Video Should be in Overlay,

Constructing View Layouts, That means it is perfectly legal to pass a layout such as LinearLayout as an Keep in mind that each subclass of ViewGroup , such as LinearLayout , has its own Constraints define the relation between two different view elements, and walk through this Google codelab to understand how to use ConstraintLayout works. Browse other questions tagged android android-layout android-linearlayout or ask your own question. The Overflow Blog Podcast 241: New tools for new times

Re-using Layouts with <include/>, Although Android offers a variety of widgets to provide small and re-usable elements of your application that are common across multiple layouts can be extracted, If you already know the layout that you want to re-use, create a new XML file view groups in your view hierarchy when including one layout within another. The view itself might be a single view (like TextView) or a complex layout hierarchy (nested layouts, since all layouts are views themselves). After calling setContentView your activity knows what its content looks like and you can use (FrameLayout) findViewById(R.id.root_view) to retrieve any view int this hierarchy (General pattern (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view) ).

How to make sense of the many Android layouts, Before we go into detailing the different layouts, we'll first go over the view object It represents an object which is the container for other views. Keeping the process above in mind, you should try to keep the layout of your  The Android framework gives you the flexibility to use either or both of these methods to build your app's UI. For example, you can declare your app's default layouts in XML, and then modify the layout at runtime. Tip: To debug your layout at runtime, use the Layout Inspector tool.

Comments
  • use FrameLayout its there for a reason.
  • You need to explain what "its not working" means: stackoverflow.com/help/how-to-ask
  • @KaranMer is right. You can also use Relative Layout if you just want to play with ProgressBar
  • Sorry </LinearLayout> closing tag is missing in the end of the layout.