-1

I'm explaining my problem. First of all, I wanted to let you know that I am a beginner in coding ;) I am currently creating a calculation application. The application is functional and error-free. I would now like to refine the home screen (activity_main.xml) by adding a text. In this same home screen (activity_main.xml), I added a DrawerLayout. However, I have been stuck for several days on a problem.

When a TextView is added to this Drawerlayout, I am confronted with two problems (depending on the positioning of the textview in the code): either the text applies to all fragments whereas it should only be applied to activity_main.xml, or the TextView is not displayed at all.

I would like to have your opinion on my problem and at the same time, find a solution;)

It's an example of my problem :

Desired result Result

Thank you in advance for your answer !

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context="fr.mathieuch.aide_ventil2.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/open"/>

    <!-- 1 | MainActivity RootView -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Toolbar -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/activity_main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:theme="@style/ToolBarStyle"/>


        <!-- FrameLayout for our fragments -->
        <FrameLayout
            android:id="@+id/activity_main_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>

    <!-- 2 | NavigationView -->

    <android.support.design.widget.NavigationView
        android:id="@+id/activity_main_nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/activity_main_nav_header"
        app:menu="@menu/activity_main_menu_drawer" />



</android.support.v4.widget.DrawerLayout>
Matof35
  • 1
  • 1
  • 1
    why is your textView's layout_height and width = match_parent? – Praveen G Mar 25 '19 at 10:24
  • Indeed, it's a stupid mistake :( – Matof35 Mar 25 '19 at 10:28
  • I just updated it – Matof35 Mar 25 '19 at 10:30
  • Firstly, it doesn't really matter what you set for the width and height on the `TextView`, where it's at currently. Any non-drawer child of `DrawerLayout` is laid out to match in both directions, regardless of what the specified dimensions are. Secondly, where you have it now, it will appear behind, underneath, your `Fragment`s. If you were to move it to after the `LinearLayout`, it would be in front of them, on top. I'm not exactly sure what you're trying to accomplish, but in both of those cases, transacting `Fragment`s in and out of that `FrameLayout` will not affect the `TextView` at all. – Mike M. Mar 25 '19 at 10:41
  • It's always going to stay exactly where you put it. Please better explain exactly what you mean by "it should only be applied to activity_main.xml". – Mike M. Mar 25 '19 at 10:41
  • Thank you for your answer! I would like to add a textview on the MainActivity, i.e. it only appears on the MainActivity and not on the other fragments. – Matof35 Mar 25 '19 at 11:01

1 Answers1

0

You should put white background to your fragment's xml root. And check.

Sohel S9
  • 246
  • 1
  • 15