-1

Idk why but my custom toolbar is not showing in different activities! In my main activity

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#fff"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

While in my main_class:

toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

...

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == android.R.id.home)
        {
            onBackPressed();
            return true;
        }
        else
        {
            return super.onOptionsItemSelected(item);
        }
    }

and in my manifest:

 <activity
            android:name=".Main"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.dell.x.Main" />
        </activity>

My Styles:

<style name="MyMaterialTheme.Base"  parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="windowNoTitle">true</item>
      <item name="windowActionBar">false</item>
</style>

However my toolbar is not showing in any other activity except the main!I want to show the back button with a custom title.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
ora
  • 79
  • 9

3 Answers3

1

You have made false in your windowActionBar

Try this

<style name="MyMaterialTheme.Base"  parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">true</item>

and

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        tools:ignore="MissingConstraints">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_news"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:titleTextColor="@android:color/white"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    </android.support.design.widget.AppBarLayout>

Hope this helps.

nimi0112
  • 2,065
  • 1
  • 18
  • 32
1

Basically the thing is that the toolbar_layout if you don't want to use Coordinated Layout with the default toolbar! You create this:

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:background="@color/white"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_height="?attr/actionBarSize"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_height="wrap_content">

    <ImageView />
    <TextView/>

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

And then you extend it to whatever layout you want! If want to add a back button you simply do this:

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back));

and in your main layout you add

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == android.R.id.home)
        {
            onBackPressed();
            return true;
        }
        else
        {
            return super.onOptionsItemSelected(item);
        }
    }

I think that works perfectly fine!

0

Within the XML layout file for your Main Activity you will have something like this

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
    ...
    other controls
    ...
</android.support.design.widget.CoordinatorLayout>

You need that Toolbar xml element in your other activities layout XML files.

CodeChimp
  • 4,745
  • 6
  • 45
  • 61