Sneha Bansal
Sneha Bansal

Reputation: 941

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;
    }

Upvotes: 6

Views: 673

Answers (2)

Sneha Bansal
Sneha Bansal

Reputation: 941

I have changed below the line

contentPanel.addView(view);

to

contentPanel.addView(view,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

It worked for me.

Upvotes: 0

Mehmed
Mehmed

Reputation: 3040

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

Upvotes: 3

Related Questions