chrise
chrise

Reputation: 4253

android toolbar sometimes showing activity names and sometimes not

My app has different behaviour for the toolbar. In one case, it shows the name of the activity, in others not. I cant spot a difference in xmls that would explain it. Any idea why they behave so differently?

Activity without Text

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:fitsSystemWindows="true"
tools:context=".ScreenMenu">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.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" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_screen_menu" />

</android.support.design.widget.CoordinatorLayout>

Content xml for act without text

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.ce.activizor.ScreenActivities"
tools:showIn="@layout/activity_screen_menu"
android:background="@color/background">

<LinearLayout

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:weightSum="5">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:weightSum="2">

        <Button
            style="@style/AppTheme.Button_prim_sec"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="Calendar"
            android:id="@+id/btn_screen_menu_cal"
            android:layout_weight="1" />

        <Button
            style="@style/AppTheme.Button_prim_sec"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="What's on"
            android:id="@+id/btn_screen_menu_whatson"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout

        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:weightSum="4">

        <Button
            style="@style/AppTheme.Button_prim_sec"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="Friends"
            android:id="@+id/btn_screen_menu_friends"
            android:layout_weight="1" />

        <Button
            style="@style/AppTheme.Button_prim_sec"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="Notifications"
            android:id="@+id/btn_screen_menu_notifications"
            android:layout_weight="1" />

        <Button
            style="@style/AppTheme.Button_prim_sec"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="Activities"
            android:id="@+id/btn_screen_menu_act"
            android:layout_weight="1" />

        <Button
            style="@style/AppTheme.Button_prim_sec"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="Add event"
            android:id="@+id/btn_screen_menu_addevent"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

And for the activity that shows the title

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:fitsSystemWindows="true"
tools:context=".ScreenActivities">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.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" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_screen_activities" />

</android.support.design.widget.CoordinatorLayout>

Content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.ce.activizor.ScreenActivities"
tools:showIn="@layout/activity_screen_activities"
android:background="@color/background">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/et_activity_new_activity"
    android:hint="new activity"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true" />

<Button
    style="@style/AppTheme.Button_prim_sec"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add activity"
    android:id="@+id/btn_activity_new_activity"
    android:layout_below="@+id/et_activity_new_activity"
    android:layout_alignParentStart="true" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lv_activity_all_activities"
    android:layout_below="@+id/btn_activity_new_activity"
    android:layout_alignParentStart="true" />

</RelativeLayout>

enter image description here This one displays the activity name on the phone (see right pic)

enter image description here

Also, one problem I had with the activity that does not show the text was that I had to add a relative layout as first layer as otherwise, the toolbar would be covering part of the linear layout.is there a better way to achieve this?

Upvotes: 0

Views: 59

Answers (1)

prateek
prateek

Reputation: 508

  1. First check for android:label tag for activites in manifest file. For some, it might be mentioned and for some it might be missing.

  2. To add a title, use

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); setSupportActionBar(mActionBarToolbar); getSupportActionBar().setTitle("My title");

  1. To remove a title, use getSupportActionBar().setDisplayShowTitleEnabled(false);

Upvotes: 1

Related Questions