Lucas Romier Lucas Romier - 4 months ago 20
Android Question

addView pushes text below drawerlayout

I am totally new to the android app development, and I have a problem:

I am adding content to my main LinearLayout when pressing a button on my DrawerLayout:

private void switchTo(String text) {
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
final TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setText("New text: " + text);
content.addView(textView, 0);
}


But this adds the text on top of my LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_layout"
android:gravity="bottom">

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:background="#e6e6e6"
android:layout_height="match_parent">

<ListView
android:id="@+id/navList"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#cccccc"/>
</android.support.v4.widget.DrawerLayout>




How can I manage to add the text after the DrawerLayout, because adding the text right now pushes the DrawerLayout down (If it is unclear, I would like the DrawerLayout to stay on top of the page in height, while rendering text "next to it")

Answer

The DrawerLayout works like this

To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.

Check this information regarding DrawerLayouts

Your layout file should have 2 views :
1 - Main content (when then drawer is closed)
2 - The drawer itself

I would have your layout like this then

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view : here you define the layout of your activity -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <!-- The main content view : here you define the layout of your activity -->
    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 

        <!-- The navigation drawer -->
        <ListView
            android:id="@+id/navList"
            android:layout_width="250dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="#cccccc"/>

    <LinearLayout/>

</android.support.v4.widget.DrawerLayout>

Additionally, when you specify :

 content.addView(textView, 0);

You are strictly saying that you want to add your textView to the first position of your content. This adds your textView before every other causing the textView to be before the drawerLayout Check this reference regarding ViewGroups and adding views

So, the modified code would be

..
private LinearLayout contentFrame;
..
contentFrame = (LinearLayout) findViewById(R.id.content_frame);
...

private void switchTo(String text) {
    final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(params);
    textView.setText("New text: " + text);
    contentFrame.addView(textView);
}