android add expand/collapse in navigation drawer menu - android

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.

Related

combine navigation up button with drawer navigation hamburger button inside toolbar

I am struggling in combining both up button to navigate back through my fragments and the overflow menu "hamburger" button that can open up the navigation drawer menu..
any suggestion how to make it work and have the menu icon on the right and the up button on the left like in the following picture
?
MainActivity.onCreate part:
setSupportActionBar(binding.toolbar)
.apply {
title = null
}
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
my Toolbar xml :
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
style="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/toolbarColor"
app:navigationIcon="#drawable/ic_baseline_menu_24" />
I tried adding
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration.Builder(R.id.foundLostFragment)
NavigationUI.setupActionBarWithNavController(
this,
navController,
appBarConfiguration.build()
)
but it just makes the up button act like the hamburger button so it opens the drawer menu and I cannot manage to have both of the icons displayed on the toolbar..
What I have done to fix my issue was creating a second menu for the toolbar itself with 1 menu item and overrided onOptionsItemSelected() to apply that when the toolbar item clicked I just open the drawer menu so in that way I have both up button and both menu item that opens my drawer layout menu :)

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

Menu icons toolbar on Android 5.0 (Lollipop)

I can't load the menu items in the toolbar. I'm using the Drawer Navigation, and I can't even show the hamburger icon.
I'm using getSuportActionBar, my activity extends from ActionBarActivity, I added the toolbar xml into my activity xml.
SOLUTION
I found my solution here, I just added a LinearLayout as a parent in the toolbar
Appcompat Toolbar Not Showing With Navigation Drawer
You can set your favorite icon and add a listener.
mToolbar.setNavigationIcon(R.drawable.ic_drawer);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDrawerLayout.openDrawer(Gravity.START);
}
});
I found my solution here, I just added a LinearLayout as a parent in the toolbar
Appcompat Toolbar Not Showing With Navigation Drawer

Action bar - custom image and text near UP button

I have a login screen with two options in my app - "login" and "create account". I want to implement the following thing, for example, on login screen :
I've got one activity and another, by tapping on "UP" button i want to return back. I have the following constructor of desired activity:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bl__login_form_view_controller);
getActionBar().setDisplayHomeAsUpEnabled(true);
//----------------------added source - no effect-----------------------
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setIcon(R.drawable.circle) ;
getActionBar().setTitle("A title");
//---------------------------------------------------------------------
setTitle ("Вход") ;
}
And i have UP button. How can i add circle image and some text? All tutorials say, that i can set app icon in AppManifest, but it would change app icon on main screen. And how can i add text to back button? I don't wanna implement any navigation logic or set parent activity for whole app, because it's a login screen, and the main menu with the navigation logic will be shown only after authorization. Thanks in advance!
setTitle will set the title and setIcon will set the icon to the ActionBar.
getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);
Reference :Adding Up Action and http://developer.android.com/guide/topics/ui/actionbar.html
What about:
getActionBar().setIcon(R.drawable.circle);
use getActionBar().setIcon(R.drawable.youricon) for setting the icon and getActionBar().setTitle("A title"); for setting the text next to the icon.
For the icon:
getActionBar().setIcon(R.drawable.youricon);
For the title:
getActionBar().setTitle("A title");
you can use custom action bar using your own layout
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
final ViewGroup actionBarLayout = (ViewGroup)getLayoutInflater().inflate(R.layout.yourXML, null);
actionBarLayout.findViewById(R.id.buttonId).setOnClickListener(this);
actionBar.setCustomView(actionBarLayout);
its work for me. so i hope its work for you.
The accepted answer serves the problem well. I'm just adding some additional information of using a custom action bar layout.
getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);
// You can use setLogo instead like
// getActionBar().setIcon(R.drawable.youricon);
// In case of support action bar, if its not showing properly, you need to add display options
//getActionBar().setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);
Now here's a code segment describing how to use a custom layout in android actionbar.
// Make a layout named 'custom_actionbar' and simply add elements you want to show in your actionbar.
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
.inflate(R.layout.custom_actionbar, null);
// Now for support action bar, get the action bar
actionBar = getSupportActionBar();
// Set necessary attributes
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setElevation(0);
// Set the custom layout in actionbar here
actionBar.setCustomView(actionBarLayout);
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
// Now handle the elements of the custom layout here
Button b1 = (Button) findViewById(R.id.button1);
// Set actions for Button 1 here
b1.setText("Button 1");
// Another element in custom actionbar
ImageView iv1 = (ImageView) findViewById(R.id.myImage);
iv1.setBackgroundResource(R.drawable.myImagePlaceHolder);
Place the codes in onCrate and check yourself.

New NavigationDrawer on tablets with fixed left layout

I could not find a way to do a tablet multi-pane layout easily with NavigationDrawer. Play Music app does that.
I have used LOCK_MODE_LOCKED_OPENED but it opens the drawer on top of the content as expected and it cannot be closed. Therefore the content is not completely visible.
Do we have to do it manually?
The only way we found is to do it manually, we created a simple linear layout for tablet and check the view instance in activity:
View layout = findViewById(R.id.navigation_layout);
if (layout instanceof DrawerLayout) {
drawerLayout = (DrawerLayout) layout;
// initialization of drawer toggle etc.
...
} else {
// linear layout
getSupportActionBar().setHomeButtonEnabled(false);
}

Categories

Resources