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;
}
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.
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.
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.
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 .
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:
.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With