Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding Relative layout into Linear layout

I have a LinearLayout inside DrawerLayout named "container". At the run, time I am trying to add a RelativeLayout inside the "container". It causes RelativeLayout alignment does not work properly i.e. progress coming over logo image.

Relative Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:keepScreenOn="true">
    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/logo" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingStart="8dp"
        android:paddingEnd="5dp">


        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="1dp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_network_error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:gravity="center"
            android:text="@string/no_network"
            android:textColor="#E10000"
            android:textSize="30sp"
            android:visibility="visible" />

    </LinearLayout>

    <TextView
        android:id="@+id/tv_software_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:paddingRight="20dp"
        android:paddingBottom="20dp"
        android:text="Version"
        android:textColor="@android:color/darker_gray" />
</RelativeLayout>

DrawerLayout having container

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">
    <LinearLayout
        android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@color/white"
        android:fitsSystemWindows="false"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

Injecting layout at Runtime

protected View getRootView(View view) {
        View sliderLayout = LayoutInflater.from(this).inflate(R.layout.slider_layout, null);
        LinearLayout layout = (LinearLayout) sliderLayout.findViewById(R.id.contentPanel);
        layout.addView(view);
        return sliderLayout;
    }
like image 613
Bansal_Sneha Avatar asked May 09 '19 15:05

Bansal_Sneha


People also ask

Can we use constraint layout inside linear layout?

With the help of ConstraintLayout, we can position our UI components in any sort of order whether it may be horizontal or vertical. But in the case of Linear Layout, we can only arrange our UI components either in a horizontal or in a vertical manner.

Which is better relative layout or linear layout?

LinearLayout is less used as compared to RelativeLayout. RelativeLayout is used more in applications. We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.

How do I change a relative layout to constraint layout?

Open your layout in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click the layout and click Convert layout to ConstraintLayout.

Why is relative layout better than linear layout?

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout .


1 Answers

I am not sure where getRootView() resides in your code. If you clarify, I can provide better solution.

However, I created a project with navigation drawer activity, defined the RelativeLayout to be added dynamically in layout_to_be_added.xml, and did the following experiment in onCreate() of MainActivity:

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

    ...

    final LinearLayout contentPanel = findViewById(R.id.contentPanel);
    contentPanel.post(new Runnable() {
        @Override
        public void run() {
            View layoutToBeAdded = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_to_be_added, null);
            contentPanel.addView(layoutToBeAdded);
            // contentPanel.invalidate();
        }
    });
}

This resulted in the problem you mentioned, progress bar and text is not below the centered logo, as seen here:

Probably the layout params are not correct....

It seems null root causes wrong calculations or evaluations, therefore I updated inflate() line as follows:

View layoutToBeAdded = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_to_be_added, contentPanel, false);

Here we provide container as root but we don't attach slider layout to it. This way the root is only used to calculate correct LayoutParams and we get the expected result as seen below:

Correct result

like image 199
Mehmed Avatar answered Oct 14 '22 14:10

Mehmed