2

I have created an app that has a navigation drawer, it works fine, but the only problem is that I see two toolbars. One in which there is Actionbartoggle button and other which is default. Now the tutorial I have seen has used a toolbar in the main content of drawer layout and obviously navigation contents are coming from menu. Now the problem is I think we have to pass a toolbar reference in the constructor of Actionbardrawertoggle, and after I passed it properly it works fine, but I see two toolbars/actionbars. Basically my question is how do I get the toggle button on the main action bar and not get second tool bar created? I have tried using

<style name="MyTheme" parent="android:Theme.Material.Light.NoActionBar">

It helps me to hide the upper actionbar but I don't think it's the good way to do it. I am uploading my screenshot of output and code:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Page Content -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    </LinearLayout>

    <!-- Navigation View -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"

        app:itemTextColor="#5DADE2"

        app:menu="@menu/drawer_items"/>

</android.support.v4.widget.DrawerLayout>

Mainactivity:

 import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.support.annotation.NonNull;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.Window;
    import android.widget.Toast;    
    import java.util.HashMap;    
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;    
    import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
    SessionManager session;

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_area);   

        setupToolbarMenu();
        setupNavigationDrawerMenu();    

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.lg:

                session.logoutUser();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    } private void setupToolbarMenu() {

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

        mToolbar.setTitle("Home Page");
    }

    private void setupNavigationDrawerMenu() {

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);

        drawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout,mToolbar,
                R.string.drawer_open,
                R.string.drawer_close);

        mDrawerLayout.addDrawerListener(drawerToggle);

        drawerToggle.syncState();

    }  @Override // Called when Any Navigation Item is Clicked
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        closeDrawer();

        switch (menuItem.getItemId()) {

            case R.id.home:
                // Put your Item specific Code here
                break;

            case R.id.invoice:
                Intent intent = new Intent(this,details.class);
                startActivity(intent);
                // Put your item specific Code here
                break;
        }

        return true;
    }

    // Close the Drawer
    private void closeDrawer() {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }

    // Open the Drawer
    private void showDrawer() {
        mDrawerLayout.openDrawer(GravityCompat.START);
    }


    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
            closeDrawer();
        else{
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure?")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                }).setNegativeButton("no", null).show();}
    }     
}

my styles.xml file:

<resources>
        <!-- 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/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>     
    </resources>

I know this might not be of some use, but here's my manifest file too:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".details" />
        <activity android:name=".Login"
            android:label="Login"/>

        <activity android:name=".MainActivity" />
        <activity android:name=".recycler" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

And here's the output I am getting: enter image description here

Scath
  • 3,777
  • 10
  • 29
  • 40
Robin10
  • 77
  • 1
  • 8
  • Please check next links : https://stackoverflow.com/questions/24307919/action-bar-drawer-toggle-custom-icon https://stackoverflow.com/questions/29558303/change-icon-of-navigation-drawer – Ahmed Aboelyazeed Aug 02 '17 at 18:35

1 Answers1

1

Try to follow this example: https://developer.android.com/training/appbar/setting-up.html

There you will find this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
    }

the thing you are missing is: setSupportActionBar(myToolbar);

Leandro Ocampo
  • 1,894
  • 18
  • 38
  • 1
    ok so adding android:theme="@style/Theme.AppCompat.Light.NoActionBar" and setSupportActionBar(mToolbar); played a part. Now i am able to see my drawer in the The main actionbar where app name was written. Thx :) i am accepting your answer. Just one more thing , now i see the overflow menu in dark color when i click on it, how can i give it the white color back while maintaining the drawer in that main action bar? – Robin10 Aug 02 '17 at 18:53
  • I haven't said that you should use android:theme="@style/Theme.AppCompat.Light.NoActionBar"... use the one you had before: Theme.AppCompat.Light.DarkActionBar – Leandro Ocampo Aug 02 '17 at 18:56
  • @Robin10 ... maybe if you remove this line: app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" from your toolbar .. – Leandro Ocampo Aug 02 '17 at 18:59
  • and i am not able to see the action bar anywhere when i go to other acticity now, actualy i want action bar to be seen in all activities, so that later i can add tab layout too in it , so is there any other way than using Theme.AppCompat.Light.NoActionBar" in styles? i have mentioned that in my question too. or i should put this theme in just that activity which have drawer? – Robin10 Aug 02 '17 at 19:00
  • Each activity has to have its own toolbar @Robin10. If you want a toolbar for another activity you have to put a toolbar there too. – Leandro Ocampo Aug 02 '17 at 19:02
  • oh ok , got it, so ill create separate toolbar xml file and will include it in all the layouts and then reference it in the activites, ill do that , just one more thing , now when i have replaced the noactionbar theme with the old one t.Light.DarkActionBar , the app is crashing now and not able to start. Should i use noactionbar back? – Robin10 Aug 02 '17 at 19:07
  • Yup!! go back to that.. my mistake =P – Leandro Ocampo Aug 02 '17 at 19:09
  • In order not to copy the same layout everywhere you can define your toolbar inside a new layout and use tag in every activity that should use that toolbar. – Leandro Ocampo Aug 02 '17 at 19:11
  • yes offcourse thats what i was talking about when i said "include it in all layouts" ;) , thanks mate anyway :) So basically what i have to do is put in manifest noactionbar, but in toolbar the light.darkactionbar right? – Robin10 Aug 02 '17 at 19:13
  • @Robin10 Yes... thy this in your toolbar: android:theme="@style/ThemeOverlay.AppCompat.Dark". For more information: https://stackoverflow.com/questions/27238433/when-should-one-use-theme-appcompat-vs-themeoverlay-appcompat – Leandro Ocampo Aug 02 '17 at 19:17