How to hide drawer once the applilication starts? - android

I am developing an android application with a navigation drawer. Everything works fine. except that when I start my application the drawer appears automatically. Here what I tried to fix this issue:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
mDrawerLayout);
// as a solution I added this if-statement
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawers();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
But it is not working! . How to hide it once the applilication starts ?

mDrawerLayout.closeDrawer(GravityCompat.START);

Hi u can use following code:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);

Related

Staying in navigation drawer when clicking item from it

I have a navigaiton drawer. When i click on a certain item from the nav drawer, the nav drawer is closed and is redirected to main page. I want navigation drawer to refresh after clicking an item in it.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.refresh_drawer) {
----CODE----
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
What can i do to make nav drawer refresh and not redirect to main page? somebody please help me
It closes because you used:
drawer.closeDrawer(GravityCompat.START);
In onNavigationItemSelected() method so, removing this will help.
Also:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Define this inside onCreate() method and make it private by adding it in above of onCreate() like this to have access whole the Activity:
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
}
This is the recommended way to define such widgets inside an Activity when Activity creates.

Navigation drawer is not opening when set app:elevation="0dp" in AppBarLayout

I want to implement Navigation drawer it's working well but when i set app:elevation="0dp" in AppBarLayout at that time the navigation drawer is not opening. i have tried folowing code.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(GravityCompat.START);
LOGD("Clicked:::","Drawer");
}
});
and also using toggle
drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.app_name, R.string.app_name);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
but it's not working, i stuck from many days, pls help to solve it!!!

How to change the humberger icon in toolbar?

I am using mikepenz drawer library but I want to change default humburger icon and back arrow icon with my own drawable icon.
I have tried many times but I am unable to change the icon with my own icon .
Can anyone help me ?
new DrawerBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withToolbar(toolbar)
.addDrawerItems(
//pass your items here
)
.build();
CODE TO SHOW THE HUMBURGER ICON:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
following is the code I found many times but i tried this also but it did not work
Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
And when I am searching I also come to know that you can not change the icon if you passing the toolbar in drawer builder so can anyone tell me what can I do?
I haven't tried it with that library but, try the following:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
As per this link, you need to remove the withToolbar() from the DrawerBuilder and then you will have to handle open/close completely on your own.
For that you can do some thing like that
protected void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
...
}
Also you had to add a toolbar navigation click listener to listen for click events on the custom drawer icon.
protected void onCreate(Bundle savedInstanceState) {
...
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
...
}
You can update the icon dynamically whenever required as
toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);
Hope this will help you.
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
String Drawer_Open,Drawer_Close;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set it button icon
getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
//set it makes button Clickble
getSuppotActionBar().setHomeButtonEnabled(true);
//set your own icon by using this code
getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
drawerLayout.serDrawerListener(actionBarDrawerToggle);
}
}
Again Do you have any quires get Consult me here.....,hope you got solution to your problem...
Try this by modify following:
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
to
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
this disable library default icon then change the icon...
getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);

Icon animation on the new NavigationView

I am trying to switch to the new NavigationView for my Drawer.
When i did (and its working fine), I lost the animation of the Menu icon to the Back icon when the Drawer is sliding.
How can i have that on the NavigationView?
Here is my code:
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setNavigationIcon(R.drawable.ic_menu_white);
setSupportActionBar(toolbar);
mDrawer = (NavigationView) findViewById(R.id.navigation_drawer);
if (mDrawer != null) {
mDrawer.setNavigationItemSelectedListener(this);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Thanks in advance!
After doing a lot research, i solved it by simply adding the following:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
Hope it helps.

Android setText always crash

I'm using Android Studio to Create a Default Navigation Drawer Activity.
And I just do "setText", my app always crashed.
Here is my code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
TextView t1=(TextView)findViewById(R.id.textView);
t1.setText("Test");
}
You should check is there any R.id.textView in your activity_main.xml. If your TextView is nested inside another view, you should do something like this:
TextView t1=(TextView) view.findViewById(R.id.textView);
There is also a possibility, this TextView is inside your fragment and your fragment doesn't exists when you are trying to find this TextView by id.

Categories

Resources