0

I am trying to add a nav bar to my activity using a guide developer.

However, it doesn't see to want to work. I have a bunch of additional buttons too but I have cut it down so its easier to read here.

I have tried a bunch of different YouTube tutorials all of which use the same basic idea of toolbar.

Clarification,

I believe the toolbar is being pushed behind the constraint layout or as when i go into design view i can see it behind the button at the top of the page.

.java file

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

.xml file

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout  
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:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="******">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

 <android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:id="@+id/nav_view"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu"/>


</android.support.v4.widget.DrawerLayout>
  • Is the action bar showing up instead of toolbar OR some other layout issue ? – Kaveri Mar 03 '19 at 15:33
  • Currently with that, i have removed the action bar by creating a new style called 'AppTheme.NoActionBar' and within android manifest have called this. 'android:theme="@style/AppTheme.NoActionBar"' So no action bar is shown – Imran Mohammed Mar 03 '19 at 15:38

1 Answers1

0

In your Activity.java import android.support.v7.widget.Toolbar instead of
android.widget.Toolbar:

    import android.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.support.v7.widget.Toolbar;

    public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getName();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Stack Overflow");
        setSupportActionBar(toolbar);

Review this question

Dr Mido
  • 2,414
  • 4
  • 32
  • 72