רפאל חדד
רפאל חדד

Reputation: 27

Custom ToolBar not there

I have decided to make my own Tool bar

So i removed the regular ToolBar :

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="false"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar> //removetollbar

and make my own tool bar

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

then i include in main xml

<include
    layout="@layout/customtoolbar"
    android:id="@+id/custombar" >

</include>

and in the code i set it up :

_CustomToolBar = (android.support.v7.widget.Toolbar) 
findViewById(R.id.custombar);
setSupportActionBar(_CustomToolBar);
android.support.v7.widget.Toolbar _CustomToolBar;

now when the app run the custom toolbar is not there

Please help.

Upvotes: 0

Views: 49

Answers (2)

Varad Mondkar
Varad Mondkar

Reputation: 1561

Didn't get your question clearly, but still I will post how I code to set custom toolbar.

1) My Manifest have NoActionBar theme to remove default toolbar.

2) My custom_toolbar.xml file -

<?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_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="5dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

3) Way I include it in my main.xml

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

4) My Java code -

// In onCreate method
android.support.v7.widget.Toolbar _CustomToolBar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(_CustomToolBar);

And it is working properly.

enter image description here

So Here whats the different between our code.

1) Give your custom toolbar proper id android:id="@+id/customToolbar" instead of giving it to you include.

2) Give height and background color to your custom toolbar so that it will be visible properly

android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"

3) And instead of linking include id to toolbar, link your custom toolbar id

findViewById(R.id.customToolbar);

Upvotes: 0

Ben P.
Ben P.

Reputation: 54204

In your <include> tag, you've set the id of the toolbar to be @+id/custombar, but you're looking it up using R.id.toolbar. Change that line to this:

_CustomToolBar = (android.support.v7.widget.Toolbar) findViewById(R.id.custombar);

Upvotes: 1

Related Questions