hashik
hashik

Reputation: 3

Toolbar won't display in Activity

I've set my application theme to be NoActionBar in AndroidManifest.xml because I only want a toolbar in my activity and a few fragments, so I'm willing to set it manually for those.

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I made a view for my toolbar tb.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<TextView
    android:id="@+id/tvTitle"
    android:text="Main"
    android:textColor="@color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
    android:layout_gravity="center" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ivBell"
    android:background="@drawable/bell"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true" />
</android.support.v7.widget.Toolbar>

and here's how I set it in the activity:

 Toolbar toolbar = (Toolbar) getLayoutInflater().inflate(R.layout.main_toolbar, null);
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayShowTitleEnabled(false);

But it doesn't show up when I launch my app - any ideas what the problem could be?

Upvotes: 0

Views: 234

Answers (1)

Viraj Patel
Viraj Patel

Reputation: 2403

Try below code:

In your style.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorAccent</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:splitMotionEvents">false</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

In your AndroidMenifest.xml

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

In your Activity.java or BaseActivity.java file add following method and call it in onCreate or you can directly put method's code in Activity.java's onCreate:

public void setUpToolbar(String strTitle) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            title = (TextView) toolbar.findViewById(R.id.tvTitle);
            title.setText(strTitle);
        }
    }

Note: Don't forget to include tb.xml file in your activity's .xml file

In your Fragment.java or BaseFragment.java file add following method and call it in onCreateView or you can directly put method's code in Fragment.java's onCreateView:

public void setupToolBarWithBackArrow(Toolbar toolbar, @Nullable String Title) {
        ActionBar actionBar;
        // If you are using BaseActivity.java then keep below code as it is otherwise replace BaseActivity with Your Activity's name
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        actionBar = ((BaseActivity) getActivity()).getSupportActionBar();

        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_back); // Set null if you don't want to show back arrow
        }
        // Remove this click listener if you set null in setHomeAsUpIndicator(...)
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.onBackPressed();
            }
        });
        title = (TextView) toolbar.findViewById(R.id.title);
        title.setText(Title != null ? Title : "");
    }

Note: Don't forget to include tb.xml file in your fragment's .xml file

Upvotes: 2

Related Questions