6

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; } 
2
  • Can you use the layout inspector to view the actual layout when the app is running? If this doesn't help: the inspector should generate a file of the retrieved layout, which you can show us to help further Commented May 17, 2019 at 15:26
  • 1
    Since you put the logo ImageView before ProgressBar in the RelativeLayout, it is desirable to see the ProgressBar over the logo. Commented May 18, 2019 at 12:06

2 Answers 2

3
+50

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

Sign up to request clarification or add additional context in comments.

6 Comments

from where did you get object "sliderLayout" at first line of method?The view "contentPanel" is inside view of sliderLayout.
I deleted it, it was blind shot since I don't know where getRootView is and how your whole implementation is. The solution holds for my example though.
getRootView was called before setContentView(); as you have already done.
You cannot override getRootView in an Activity, so I wonder where you override it.
It is not overrided method but its created by me which will get called in setContentView () of Parent .
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.