Ben
Ben

Reputation: 175

Why is there 2 title bars in my activity when using navigation drawer in Marshmallow device

When I run my app in devices lower than marshmallow devices it shows only one title bar. But when I try it in marshmallow devices the navigation drawer is present in the second title bar.Like this.

sadasasdsad

Can someone Tell me how to fix this?

My MainActivity, activity_main, manifest and style are given below.

Thank You.

MainActivity

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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


try {


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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    setTitle("Case Sheets");

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    myDB = MainActivity.this.openOrCreateDatabase("hoteldb", MODE_PRIVATE, null);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext()));
        try {

            showList();

        }catch (Exception e)
        {
            Toast.makeText(getApplicationContext(),"Please Synch the data",Toast.LENGTH_SHORT).show();
        }



    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(),"Please Login",Toast.LENGTH_LONG).show();
        Intent logout = new Intent(MainActivity.this, LoginPage.class);
        startActivity(logout);

    }



}

activity_main.xml

<?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:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

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



</android.support.design.widget.CoordinatorLayout>

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

Manifest.xml

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

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<application
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
   <!-- android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > -->
    <activity
        android:name=".Oodo.Login.LoginPage"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".Oodo.CaseSheets.MainActivity"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="portrait"></activity>

     </application>

style.xml

<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>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />




<style name="heading_text">
    <item name="android:textColor">#ff000000</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">16sp</item>
    <item name="android:padding">5dp</item>
</style>
<style name="HomeButton">
    <item name="android:layout_gravity">center_vertical</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">center_horizontal</item>
    <item name="android:textSize">@dimen/text_size_medium</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:background">@null</item>
</style>

</resources>

Upvotes: 1

Views: 320

Answers (3)

Ben
Ben

Reputation: 175

I finally fixed it.

What I did was:

1.Deleted the toolbar from the layout

2.Changed my MainActivity like this. refer here

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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


try {


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

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle("Case Sheets");

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,  R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawer.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });
    drawer.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

myDB = MainActivity.this.openOrCreateDatabase("hoteldb", MODE_PRIVATE, null);

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext()));
    try {

        showList();

    }catch (Exception e)
    {
        Toast.makeText(getApplicationContext(),"Please Synch the data",Toast.LENGTH_SHORT).show();
    }



} catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(),"Please Login",Toast.LENGTH_LONG).show();
    Intent logout = new Intent(MainActivity.this, LoginPage.class);
    startActivity(logout);

}



}

And Kept the theme of the activity as Apptheme itself.

It worked on both marshmellow and lolipop devices..

@rafsanahmad007..Thank u for helping.

Upvotes: 2

rafsanahmad007
rafsanahmad007

Reputation: 23881

you are missing this:setSupportActionBar()

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setSupportActionBar(toolbar);

in values/styles.xml:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Apply the theme: for ur activity in manifest

in manifest:

  <activity
    android:name=".Oodo.CaseSheets.MainActivity"
   android:theme="@style/AppTheme.NoActionBar"  //change here

Upvotes: 2

Ravi Theja
Ravi Theja

Reputation: 3401

add this setSupportActionBar(myToolbar); after toolbar initialisation.

Upvotes: 0

Related Questions