androidnavigation-drawermaterialdrawer

Toolbar is cutting from top. How to show full Toolbar?


Screenshot

I am adding material drawer Material Drawer and made a method addNavigationDrawer to add Navigation Drawer and calling the method after I get the details of user from Firebase Firestore Database(You can find it in Auth Listener under OnCreate Method) But On First time opening of this activity , action bar is cutting as you can see in the image But when I just minimize it and open it again, its working absolutely fine. Please suggest what to do?

Here is the full code of Activity

public class Main2Activity extends AppCompatActivity {

ProfileInformationDialog profileInformationDialog;

FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener;
FirebaseFirestore db;
DocumentReference documentReference;

Button button;
GoogleApiClient googleApiClient;

/*@BindView(R.id.toolbar)
Toolbar toolbar;*/

ProgressDialog progressDialog;

String user_name,shop_name;
Toolbar toolbar;

Drawer result;

AccountHeader headerResult;


@Override
public void onStart()
{
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    googleApiClient.connect();
    super.onStart();
    firebaseAuth.addAuthStateListener(authStateListener);
}



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

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

    toolbar.setTitle("Hello");

    setSupportActionBar(toolbar);

    //DrawerUtil.getDrawer(Main2Activity.this,toolbar);

    //addNavigationDrawer();
    progressDialog = new ProgressDialog(this);
    db = FirebaseFirestore.getInstance();
    profileInformationDialog = new ProfileInformationDialog(this);

    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);

    ButterKnife.bind(this);

    button = (Button) findViewById(R.id.log_out);
    firebaseAuth = FirebaseAuth.getInstance();

    authStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            final FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser != null) {
                progressDialog.setMessage("Loading...");
                progressDialog.show();
                progressDialog.setCancelable(false);

                //User is signed in
                documentReference = db.collection("Users").document(firebaseUser.getUid());

                documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot)
                    {
                        if (documentSnapshot != null && documentSnapshot.exists()) {
                            progressDialog.hide();
                            User user1 = documentSnapshot.toObject(User.class);
                            user_name = user1.getName();
                            shop_name = user1.getShop_name();
                            addNavigationDrawer();
                            //DrawerUtil.getDrawer(Main2Activity.this,toolbar);
                            headerResult.updateProfile(new ProfileDrawerItem().withIcon(R.drawable.logout_icon256).withName(user_name));
                            Toast.makeText(Main2Activity.this, "Document Exists", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            progressDialog.hide();
                            Toast.makeText(Main2Activity.this, "No Such Document", Toast.LENGTH_SHORT).show();
                            profileInformationDialog.show();
                            profileInformationDialog.setCancelable(false);
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e)
                    {
                        progressDialog.hide();
                        Toast.makeText(Main2Activity.this, "Exception " + e, Toast.LENGTH_SHORT).show();
                    }
                });

                Log.d("TAG", "onAuthStateChanged:signed_in:" + firebaseUser.getUid());
            } else {
                // User is signed out
                Log.d("TAG", "onAuthStateChanged:signed_out");
            }
        }
    };


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(@NonNull Status status)
                        {
                            //Toast.makeText(getApplicationContext(),"Google Logged Out",Toast.LENGTH_SHORT).show();
                        }
                    });

            firebaseAuth.signOut();
            LoginManager.getInstance().logOut();

            startActivity(new Intent(Main2Activity.this,SignInActivity.class));
            finish();
        }
    });


}

private void addNavigationDrawer()
{
    headerResult = new AccountHeaderBuilder()
            .withActivity(this)
            .withHeaderBackground(R.drawable.curve_shape)
            .withSelectionListEnabledForSingleProfile(true)
            .addProfiles(
                    new ProfileDrawerItem().withName(user_name).withIcon(R.drawable.logout_icon256)
            )
            .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                @Override
                public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile)
                {
                    Toast.makeText(Main2Activity.this, profile.getName() + "", Toast.LENGTH_SHORT).show();
                    return false;
                }
            })
            .build();


    PrimaryDrawerItem makeBillItem = new PrimaryDrawerItem().withIdentifier(1)
            .withName("Make new Bill");

    SecondaryDrawerItem logoutItem = new SecondaryDrawerItem().withIdentifier(2)
            .withName("Log Out").withIcon(R.drawable.logout_icon256);

    result = new DrawerBuilder()
            .withAccountHeader(headerResult)
            .withActivity(this)
            .withToolbar(toolbar)
            .withTranslucentStatusBar(false)
            .withDisplayBelowStatusBar(false)
            .withActionBarDrawerToggle(true)
            .withActionBarDrawerToggleAnimated(true)
            .withCloseOnClick(true)
            .withSelectedItem(-1)
            .addDrawerItems(
                    makeBillItem,
                    new DividerDrawerItem(),
                    logoutItem
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    Toast.makeText(Main2Activity.this, position + " = position", Toast.LENGTH_SHORT).show();
                    return true;
                }
            })
            .build();

}


@Override
public void onStop() {
    super.onStop();
    if (firebaseAuth != null) {
        firebaseAuth.removeAuthStateListener(authStateListener);
    }
}

}


Solution

  • step 1 add this style to value/style

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

    step 2 add this style to value/style-v21

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    

    step 3 and change your layout

    <?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:id="@+id/drawer_layout"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:openDrawer="start"> 
    
           <android.support.design.widget.CoordinatorLayout 
                  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"
                 tools:context="com.visky.railway.sec.ui.activity.MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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>
    
        <RelativeLayout
             android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
           -----------here your layout content---------------
    
        </RelativeLayout>
     </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" />
    
    </android.support.v4.widget.DrawerLayout>
    

    step 4 add theme to manifest.xml

       <activity
          android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar"/>