• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Adding a TextView to a DrawerLayout

Greum

Well-Known Member
I'm creating an app with a DrawerLayout which shows a blank screen until the menu is displayed. I wanted to add a welcome message on the initial screen.

I tried adding a LinearLayout containing a TextView to the DrawerLayout but the TextView wasn't displayed. Ditto when I tried a TextView by itself.

I guess I could create a new Fragment and load that when the app starts, but that doesn't sound like the best way to approach it.

Is there a way to add a message on the initial screen?

This is my activity_main

Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>
 
i guess you mean splash screen?

No, not a splash screen. It's the first screen that's displayed which has just a hamburger menu, the app name and the 3 dots menu. Click on the hamburger and the drawer opens. That one.
 
No. Below is the screen that the user sees when the app starts. It is kinda "content free", not very helpful and not very welcoming. What I want to do is to add a textview or three to my activity_main (in post #1). Does that make sense?

Screenshot 2019-07-26 08.31.23.png
 
Thanks, but you've missed the point that my activity_main is a DrawerLayout not a ConstraintLayout. It is the fact that it is a DrawerLayout that is giving me trouble...
 
I'm sorry, which code would you like? I've posted my activity_main and as I've explained I've tried adding a TextView, and I've tried adding a LinearLayout containing a TextView but when I run the app the TextView does not get displayed.
 
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </com.google.android.material.appbar.AppBarLayout>
    <include layout="@layout/content_main" />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
 
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">
    <FrameLayout
        android:id="@+id/fragmentHolder"
        android:layout_width="0dp"
        android:layout_height="495dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
 
Code:
apply plugin: 'com.android.application'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "webdesignpaciano.com.agedatabase"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:design:28.0.0'
}
 
Put TextView inside FrameLayout

Java:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
                                                   android:layout_height="match_parent"
                                                   app:layout_behavior="@string/appbar_scrolling_view_behavior"
                                                   tools:context=".MainActivity"
                                                   tools:showIn="@layout/app_bar_main">

    <FrameLayout
            android:id="@+id/fragmentHolder"
            android:layout_width="match_parent"
            android:layout_height="495dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
       
        <TextView
                android:id="@+id/messageTextView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center"
                android:textSize="20sp"
                android:text="Welcome"/>

    </FrameLayout>
   
</androidx.constraintlayout.widget.ConstraintLayout>
 
The only problem with this is that the welcome message is shown on all fragments, the input, delete etc screens.
 
Back
Top Bottom