Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar - findViewbyID returning null

Why am I unable to find my Toolbar in my layout?

setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
}

After that, toolbar is still null.

activity_man.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    tools.context=".PlayerActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
        : (goes on)

EDIT:

toolbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"/>

Thanks!

like image 886
diiiz_ Avatar asked Dec 14 '14 12:12

diiiz_


1 Answers

Your code is working fine you may not have disable default actionbar:

Change the following in styles.xml to remove action bar

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

    <item name="windowActionBar">false</item>

</style>

Bring toolbar to front using toolbar.bringToFront();

like image 82
kunal.c Avatar answered Oct 04 '22 22:10

kunal.c