Why is the app name displaying on Toolbar?

1.5k Views Asked by At

please check the image that shows the displayed outputI have created a fragment class and in its xml layout i have added a toolbar with some text in it. But due to some reason app name is also displayed there. I have made other pages as well with the same code but the name is displayed only on this one.

Here is my xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar1"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <TextView
        android:text="Recharges"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_weight="1" />

</android.support.v7.widget.Toolbar>


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/logo"
    android:id="@+id/imageView3" />

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:rowCount="5">



    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="25dp"
        android:layout_marginRight="10dp"
        android:layout_row="1"
        android:layout_column="0"
        app:srcCompat="@drawable/mobile"
        android:id="@+id/imageView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:gravity="center"
        android:layout_column="1"
        android:layout_marginTop="30dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="Mobile"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="25dp"
        android:layout_row="2"
        android:layout_column="0"
        app:srcCompat="@drawable/tv"
        android:id="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="1"
        android:layout_marginTop="30dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="DTH"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="25dp"
        android:layout_row="3"
        android:layout_column="0"
        app:srcCompat="@drawable/data"
        android:id="@+id/imageView8" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="1"
        android:layout_marginTop="30dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="DataCard"/>

        />




</GridLayout>

3

There are 3 best solutions below

0
On BEST ANSWER

In your code -- android:theme="@style/ThemeOverlay.AppCompat.ActionBar"

Change above line to:

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

Then the title will be shown in white colour.

Replace toolbar code with this:

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar1"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

In Java:

final Toolbar toolbar = (Toolbar) view.findViewById(R.id.my_toolbar1);
toolbar.setTitle("Recharges");
0
On

To save time instead of adding some code in each activity oncreate() you can add a label to each activity in the AndroidManifest.xml and it will be added automatically to the toolbar. Ex:

<activity
        android:name="com.example.Activity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"/>
0
On

try this:

in your fragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout resource that'll be returned
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    mToolbar = (Toolbar) rootView.findViewById(R.id.my_toolbar1);
    if (mToolbar != null) {
       ((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
    }
    mToolbar.setTitle("Text Title");

    return rootView;
}