Disable click on drawerlayout hamburger icon when show progress bar - android

I has activity with the next layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayoutWidget"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/containerProgress"
android:layout_width="match_parent"
android:clickable="false"
android:layout_height="match_parent"
android:background="#4777">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/widgetRecyclerView"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/windowBackground"
android:scrollbars="vertical" />
</android.support.v4.widget.DrawerLayout>
Container containerProgress(progressBar) is show over all other elements in screen.
OK.
I need when containerProgress is show to was not possible to click any items on screen.
But I can click on drawerlayout hamburger icon.
How I can disable click on hamburger icon while containerProgress is show?

Set empty click listener to toggle
toggle.setOnClickListener(view -> {
// We ignore it
}
And whem you want to enable it again:
toggle.setOnClickListener(null);
OR
You can hide it using
toggle.setDrawerIndicatorEnabled(false);
OR
If under "disable click" you mean "prevent drawer to be opened" you can do that:
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
And when you want to enable it:
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)

Related

How to use ActionBarDrawerToggle to close both left & right navigation drawers?

I have the following layout in my activity.xml
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/store_page_layout"
tools:context=".StorePage">
<include
android:id="#+id/store_page_toolbar"
layout="#layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/store_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/store_page_toolbar">
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="#dimen/container_body_height"
android:layout_weight="1">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:elevation="6dp"
card_view:cardCornerRadius="4dp"
android:background="#drawable/landing_animated_button_background">
</android.support.v7.widget.CardView>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/store_menu_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/black_200"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/store_cart_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginLeft="#dimen/nav_drawer_left_min_margin"
android:background="#color/black_200"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
`
No I went ahead and added ActionBarDrawerToggle on my Toolbar widget, the behavior I wanted from the hamburger icon is if I click on it left drawer open up (Working), I click it again left drawer closes (Working), I open right drawer by dragging from right to left plus the hamburger icon changes to arrow (Working), if I click on arrow icon it closes right drawer as well (Not working)
As you can see, I want the hamburger icon to close both right and left drawers based on which one is open, my approach is to listen to click on arrow icon and decide which drawer is open then close it. I am unable to figure out how to set onClickListener on the hamburger or arrow icon automatically added by ActionBarDrawerToggle class.
This is explained in the documentation
http://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html#setToolbarNavigationClickListener(android.view.View.OnClickListener)
public void setToolbarNavigationClickListener (View.OnClickListener onToolbarNavigationClickListener)
When DrawerToggle is constructed with a Toolbar, it sets the click
listener on the Navigation icon. If you want to listen for clicks on
the Navigation icon when DrawerToggle is disabled
(setDrawerIndicatorEnabled(boolean), you should call this method with
your listener and DrawerToggle will forward click events to that
listener when drawer indicator is disabled.
I finally figured out the solution. Instead of setting setToolbarNavigationClickListener on actionBarDrawerToggle setting setNavigationOnClickListener on Toolbar works perfectly. my code is given below.
toolbar.setNavigationOnClickListener(new toolBarNavigationIconListener());
And OnClickListener is as follows.
private class toolBarNavigationIconListener implements View.OnClickListener {
#Override
public void onClick(View v) {
if(!storeDrawer.isDrawerOpen(Gravity.RIGHT) && !storeDrawer.isDrawerOpen(Gravity.LEFT)) {
storeDrawer.openDrawer(Gravity.LEFT);
} else if(storeDrawer.isDrawerOpen(Gravity.LEFT)) {
storeDrawer.closeDrawer(Gravity.LEFT);
} else if(storeDrawer.isDrawerOpen(Gravity.RIGHT)) {
storeDrawer.closeDrawer(Gravity.RIGHT);
}
}
}

Android: DrawerLayout with content view with two fragments

I'm creating the layout for a tablet and I have a DrawerLayout, which has a Fragment on the left menu (so the drawer) and should have two fragments as main content.
The way I'm doing it is the following:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
<View
android:id="#+id/right_card_group_divider"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#drawable/grey_line_bg"/>
<FrameLayout
android:id="#+id/menu_frame_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"/>
</LinearLayout>
<!-- The navigation drawer -->
<FrameLayout
android:id="#+id/menu_frame"
android:layout_width="300dp"
android:layout_gravity="start"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
Nevertheless, I notice that if I manually hide the "menu_frame_two", the DrawerLayout works perfectly, but if that Fragment isn't hidden, then when opening the DrawerLayout nothing appears on the screen: it gets darker, just like if the drawer has been opened.
Is there any reason why the left drawer menu isn't showing?
Your menu_frame_two FrameLayout seems coming over the menu_frame Navigation Drawer. So navigation drawer is not visible that time
Solution :
Use single FrameLayout and add all elemets of main content screen inside it
<FrameLayout android:id = "#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame_inside"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
<View
android:id="#+id/right_card_group_divider"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#drawable/grey_line_bg"/>
<FrameLayout
android:id="#+id/menu_frame_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"/>
</FrameLayout>
Reason :
As per Documentation there should be one content_frame with height and width equal to match_parent

Navigation Drawer not showing when I hide ADS

I have an android app with a navigation drawer (Sidebar) and ads on the main activity xml layout. I have a broadcast receiver inside of the Activity that listens for changes in internet connectivity. If the internet connection is good then I make the adView visible, but if the internet connection fails, I change the visibility of the adView to GONE.
The problem is when I change the visibility of the adView to GONE then the navigation drawer won't open for some odd reason. Only when I close the navigation drawer with the visibility of the adView set to GONE can I catch a quick glimpse of it closing... but when I open it I cannot see that it is opened. It appears that it has opened, but that it is not visible. If I turn my wifi back on, the adView shows and the problem doesn't exist anymore. It would be nice if I could make the ADS disappear completely if the wifi or data connection turned off.
my xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.packagename"
tools:ignore="MergeRootFrame">
<RelativeLayout
android:id = "#+id/activity_relative"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above = "#+id/adView"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/Blue"
ads:adSize="SMART_BANNER"
ads:adUnitId="xxx-xx-xxxx"/>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/transparent"
android:dividerHeight="0dip"
android:background="#color/LightGrey"/>
</android.support.v4.widget.DrawerLayout>

how to set Drawer Layout icon in right side of the device screen?

I have created drawer layout sample application, it's working fine, my problem is drawer layout working in right to left perfectly but I am trying to move icon left side to right side but it's not working give me your suggestion..!!! This is possible or not?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- The navigation drawer -->
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Maybe it's too late but you can solve this using the default Menu.
Create res/menu/my_right_side_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/btnMyMenu"
android:icon="#drawable/ic_drawer"
android:title="Right Side Menu"
myapp:showAsAction="always"/>
</menu>
Then add your menu in onCreateOptionsMenu() in your Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
int menuToUse = R.menu.my_right_side_menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(menuToUse, menu);
return super.onCreateOptionsMenu(menu);
}
Next, in your ActionBarDrawerToggle handle the click event of your menu item
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if (item != null && item.getItemId() == R.id.btnMyMenu) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
}
return false;
}
};
And finally don't forget to hide your Home button from the ActionBar
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
If you use AppBarLayout and Toolbar do as below
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:layoutDirection="rtl">
<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>
notice: android:layoutDirection="rtl"
This icon represents navigation menu, which by design has to be on left side of the screen. As per the guidelines, we can although have a navigation drawer on right side, but that shall be used to modify the contents (for example filters). For all such purposes you might want to use ActionbarItem, and put up an ActionItem in right corner of the screen. Click on that action item will open or close the right navigation drawer.
But for sure, as per the design, this animated three lined menu icon, which represents navigation shall be on left hand side.
Just for the information, to put the navigation drawer on right side, you have to change the gravity of navigation drawer as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_background" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
android:id="#+id/right_drawer"
android:layout_width="280dp"
android:layout_gravity="end"
android:layout_height="match_parent"
android:orientation="vertical" />
</android.support.v4.widget.DrawerLayout>
Also, in this case you really really want the navigation menu icon, on right either use custom header layouts or a library like ActionBarSherlock to edit it.
I hope this helps!
Here is a loophole-like solution which worked in my case.
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar0, R.string.open_nav, R.string.close_nav);
I made up a toolbar for it.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginRight="40dp"
android:layout_marginEnd="40dp"
android:background="#color/colorPrimary">
<!-- this is your general toolbar-->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right|end|center_vertical"
android:text="Mytitle"/>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar0"
android:layout_width="40dp"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="right|end"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<!-- this is your nav-button toolbar-->
</FrameLayout>
</android.support.design.widget.AppBarLayout>
and set onclicklistener for it:
toolbar0.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
drawer.openDrawer(Gravity.RIGHT);
}
}
});
From Developer's Guide:
Drawer positioning and layout is controlled using the
android:layout_gravity attribute on child views corresponding to which
side of the view you want the drawer to emerge from: left or right.
(Or start/end on platform versions that support layout direction.)
Which means, you can do this by:
<DrawerLayout
android:layout_gravity="right">
</DrawerLayout>
Edit
According to Creating a Navigation Drawer,
The drawer view (the ListView) must specify its horizontal gravity
with the android:layout_gravity attribute. To support right-to-left
(RTL) languages, specify the value with "start" instead of "left" (so
the drawer appears on the right when the layout is RTL).
So you should do:
<DrawerLayout
android:layout_gravity="start">
</DrawerLayout>

ScrollView in content of DrawerLayout prevents the drawer to be opened by swiping

When I put ScrollView into the content of a DrawerLayout, I am nolonger able to open the drawer by swiping from the side.
Activity layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The menu_main content view -->
<FrameLayout
android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView
android:name="com.gumtree.androidapp.DrawerFragment"
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
In Activity's onCreate I add a fragment which has following layout:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_height="160dp"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/headline_text_size"
android:padding="#dimen/detail_text_padding"
android:textIsSelectable="false"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/description_text_size"
android:padding="#dimen/detail_text_padding"
android:textIsSelectable="false"/>
</LinearLayout>
</ScrollView>
Without the ScrollView everything works fine and I am able to open the drawer by swiping from the side. However when I add the ScrollView, it stops working.
The problem here was silly named ID of FrameLayout used as content container of DrawerLayout. I used system ID (android.R.id.content) which caused that the content fragment was put on the top of all other views - even the drawer.
It also caused fragment's layout to overlap the drawer and - related to this question - blocked the drawer from receiving touch events. The touch events were taken by fragment's ScrollView.
Conclusion: DO NOT USE SYSTEM IDs (android.R.*) WHERE IT IS NOT NEEDED.
I just wanted it to look nice and clean.. Silly me :)

Categories

Resources