How to set navigation bar toggle when collapsible toolbar is collapsed? - android

It works great when the collapsible toolbar is expanded but when I scroll up to make it collapsed into just the toolbar, the toolbar only shows my app name. I want it to also show the hamburger toggle.
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
}

What's happening is that your Toolbar hasn't been told to draw an icon to the left of the title at all. When your Toolbar is expanded, the CollapsingToolbarLayout is then telling the Toolbar to have the icon.
So, if you want your Toolbar to have an icon to the left of the title, you can do this:
getSupportActionBar().setDisplayShowHomeEnabled(true);
You normally don't need to do this because the Activity will tell the ActionBar to show the icon already if the Activity has a parent Activity. Therefore I suspect in your case we're talking about your launcher activity.
Hope this helps.

I found the solution. I had set my toolbar within my collapsable toolbar to parallax when it should have been pinned.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />

Related

android add expand/collapse in navigation drawer menu

Hey guys this is my navigation drawer menu items.
NavigationView nav = (NavigationView) findViewById(R.id.nav_view);
final DrawerLayout drawerlayout = (DrawerLayout)
findViewById(R.id.drawer_layout);
Menu menu = navigationView.getMenu();
SubMenu sub = menu.addSubMenu("Menu 1");
sub.add("Menu 1.1");
sub.add("Menu 1.2");
sub.add("Menu 1.3");
drawerlaayout.closeDrawers();
This how i create menu items.now i Want to add Expand/collapse option in each menu items.Menu 1,Menu 2,Menu 3.
you could try these by doing r&d
1.try whether we can insert our custom layout for menu. if so, change the visibility of submenu.
2.direct drawable insert of icon into the menu.
if nothing helps try,
1.try with expandable recyclerview big nerd's library. obviously this will work.
2.click in the menu header.

setDrawerLockMode not working in android?

I want to lock swipe left-right and right-left of DrawerLayout.
DrawerLayout drawerLayout;
onCreate:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Click button to open drawerLayout:
drawerLayout.openDrawer(Gravity.LEFT);
and close:
drawerLayout.closeDrawers();
I try setDrawerLockMode in onCreate, onResume, onStart but not working, it still can open, close by swipe it.
Edit 2:
It work with:
android:layout_gravity="start"
and not work with:
android:layout_gravity="start|bottom"
Any helps. Thanks.
Add gravity value too when using setDrawerLockMode();
Do this :
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
This should work

Android - making the menu icons aligning themselves to the left instead of right

I am using a Toolbar with:
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
Is there a way of aligning the icons on the left, next to the app icon instead of on the right?

How to implement the Toolbar in the default Navigation Drawer Activity?

I am using Android Studio 1.0.1. I created a new Navigation Drawer Activity that is by default provided by this IDE. It created 2 Java files -
HomePage.javathat extends android.support.v7.app.AndroidBarActivity
NavigationDrawerFragment that extends Fragment
Along with that it provided me with 3 layout files -
activity_home_page.xml
fragment_home_page.xml
fragment_navigation_drawer.xml
Now what I wanted to do was hide the Action Bar and instead get the Toolbar as I wanted to do some detailed designing. For that I took the following steps -
Created a new style in styles.xml
true
false
Changed the theme of the application in AndroidManifest.xml
android:theme="#style/AppTheme"
Created a toolbar layout and included it in activity_home_page.xml
Added a Toolbar object to HomePage.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
But unfortunately, my app force closes whenever I launch HomePage activity. Any suggestions?
Logcat
activity_home_page.xml
HomePage.java
NavigationDrawerFragment.java
custom_toolbar.xml
The problem:
In your fragment you have:
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
...
toolbar.getContext()
However when you call findViewById the activity has not yet called setContentView. That means toolbar was not yet inflated and toolbar is set to null. Therefore toolbar.getContext() throws an NPE.
Fix:
Instead of toolbar.getContext() simply call getActivity(), to get the context.
If your fragment really does need the toolbar, you should call it after your activity's onCreate. A good place for that is onActivityCreated:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
}

ProgressBar in Toolbar (as Actionbar) is not shown (visible) on Android lollipop (5.0) using AppCompat v21

My ProgressBar isn't shown in Android Lollipop (5.0), works fine with Android 4.4. Any suggestions?
Here's my code which sets a Toolbar in onCreate:
super.onCreate...
setContentView...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
progressBar.setVisibility(View.VISIBLE);

Categories

Resources