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
Related
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 :)
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.
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" />
In my application I use DrawerLayout to show same help information. So I put it over actuall activity. Now I want to add TalckBack function and I cannot make that the any of view in DrawerLayout get the yellow rectangle (which marks focus in talkback mode).
The Drawerlayout view is loaded on demand by inflantign it.
drawerLayout = (DrawerLayout) ((Activity) mContext).findViewById(R.id.drawer);
drawerHelp = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.help, drawerLayout, false);
What I try:
in xml add:
android:focusable="true"
android:focusableInTouchMode="true"
android:importantForAccessibility="yes"
In code:
requestChildFocus()
requestFocus()
requestChildFocusFromTouch()
but still no luck.
So how to force the rectangle mark in talkback mode for ImageButton?
I solve my problem by adding
android:importantForAccessibility="no"
to all element in drawer layout and to other not nedeed elements in help layout.
I have a problem, I would like to put in my Layout the Drawer Layout and the FlyOutContainer but in the error Log it shows an error:that DrawerLayout cannot be cast to FlyOutContainer what can I do to fix this problem. I need the Drawer Layout to go to the MainActivity.
Hope you can help me and sorry for my bad english.
The code in that demo inflates a layout and casts the root view of the Layout to FlyOutContainer. If you change your layout xml so that the root is now DrawerLayout, then that code no longer runs correctly and you are probably getting ClassCastException. You should do it this way instead (in onCreate()):
setContentView(R.layout.your_activity_layout);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_id);
FlyOutContainer flyOutContainner = (FlyOutContainer) findViewById(R.id.fly_out_container_id);
Of course, if you don't actually need a reference to these views/layouts, you can just stop after setContentView(...)