Navigation Drawer icons (made with GIMP) take too much space - android

I am following the Android tutorial that suggests to use V4 Navigation Drawer that should give me on the left side an hamburger navigation icon.
What happens instead is that if I use one icon I have this result:
If instead I use another similar icon it is even worse and occupies all the space:
I do not understand:
1) How can I use the second icon in a way that is on the left, while the other icons continue to appear, like one would expect from a Navigation Drawer?
2) Why two different icons have such a different behaviour, after all I worked them with gimp giving them 200x200 pixels dimensions
My Main Class:
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
getActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#550000ff")));
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] provaListaDrawer = {"List1", "List2", "List3"};
mTitle = mDrawerTitle = getTitle();
DrawerLayout mDrawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.app_drawer, R.string.drawer_open, R.string.drawer_close)
{
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
Log.d("IVO", "onDrawerClosed");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
Log.d("IVO", "onDrawerOpened");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
}
;
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_view,provaListaDrawer));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
//
// getActionBar().setHomeButtonEnabled(true);
// ActionBar actionBar = getActionBar();
// actionBar.setDisplayShowHomeEnabled(false);
// actionBar.setDisplayShowCustomEnabled(true);
// actionBar.setDisplayShowTitleEnabled(false);
// View customView = getLayoutInflater().inflate(R.layout.activity_main, null);
// actionBar.setCustomView(customView);
// Toolbar parent =(Toolbar) customView.getParent();
// parent.setContentInsetsAbsolute(0,0);
}
//other methods
}
activity_main.xml
<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="center"
>
<!-- The main content view -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_as_list"
android:name="ivano.android.com.xx"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="19dp"
android:background="#drawable/image_background"
android:paddingTop="?android:attr/actionBarSize"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
/>
</android.support.v4.widget.DrawerLayout>

I found the solution,
basically I download the official material design icon and it works as a charm
https://design.google.com/icons/index.html
dimensions 48x48; width 48 pixels; height 48 pixels; bit depth 32;
I have chosen the reorder icon.

Related

Navigation Menu in Support Action Bar not opening

EDIT: So the menu exists but I can only get to it by sliding the menu out, the button on the top left corner doesn't toggle the drawer open.
I'm trying to implement a nav menu but instead of a hamburger button showing that opens my menu, I'm left with a non-functioning back button. I'm assuming it's a little different than the Nav Button tutorial Google has due to using a toolbar as my action bar.
MainActivity.java:
private CharSequence mTitle;
private FragmentTabHost mTabHost;
private Toolbar toolbar;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
FrameLayout frameLayout;
DrawerLayout Drawer; // Declaring DrawerLayout
ActionBarDrawerToggle mDrawerToggle; // Declaring Action Bar Drawer Toggle
String TITLES[];
int ICONS[] = {R.drawable.drawer_back, R.drawable.drawer_settings,R.drawable.drawer_notifications, R.drawable.drawer_feedback};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TITLES = getResources().getStringArray(R.array.menu_array);
mTitle = getTitle();
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<>(this,
R.layout.item_row, TITLES));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,R.string.openDrawer,R.string.closeDrawer){
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
}
main_activity.xml:
<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">
<FrameLayout 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/frame_layout"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<include
android:id="#+id/gm_header"
layout="#layout/gm_header"
></include>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:background="#color/ColorPrimaryDark"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
First, there's two DrawerLayouts declared, one called Drawer and one called mDrawerLayout. I'm going to use mDrawerLayout.
In the Drawer Toggle constructor, you can associate the toggle with the DrawerLayout and the Toolbar by declaring it with mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer){
// rest of code
Then add another couple of lines
If you want the icon to change between a hamburger and an arrow when the drawer opens and closes use this listener:
mDrawerLayout.setDrawerListener(mDrawerToggle);
call syncState to put the button and drawer into the same state(open or closed):
mDrawerToggle.syncState();
You can achieve proper "back" navigation by handling the click events of the menu items. The one you're talking about in Android is referred as the Up button which is known as the home menu item. Add this code to your Activity and you'll be able to go back from the current Activity to the previous one:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Now there are way better ways to handle the UP navigation than this. This approach simply kills the current Activity you see which will end up behaving like a back navigation as you'd like to call it.
Please refer to this section from the Android documentation to understand what's the difference between
Back:
and Up:
Once you understand the difference, you can check out this section to understand how the proper Up navigation should be handled.
Cheers!

toolbar.setNavigationOnClickListener not working

toolbar.setNavigationOnClickListener this function is not working have no clue why.
activity_main.xml Layout
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".login.LoginActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
container_main.xml Layout
<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:fitsSystemWindows="true">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
Java Activity class
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// enabling action bar app icon and behaving it as toggle button
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
setSupportActionBar(toolbar);
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
toolbar,
R.string.app_name,
R.string.app_name
){
public void onDrawerClosed(View view) {
//getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("COMECOU", "COMECOU");
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
/**
* Displaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
I forgot to say that if I slide I can see the menu is just the buttons is not logging neither being triggered.
UPDATE
Please see my answer if you want to know how to solve it.
Another thing to remember is that
calling
toolbar.setNavigationOnClickListener()
before
setSupportActionBar(toolbar);
won't work.
I see that you are using the ActionBarDrawerToggle and hence setNavigationOnClickListener() is the wrong one. The right one is setToolbarNavigationClickListener(). This will make it behave like a toggle button.
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TOU AQUI", "TOU AQUI");
}
});
You should use the ActionBarDrawerToggle#setToolbarNavigationClickListener in your case.
From the Android Documentation:
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 found the mistake we can't place toolbar outside of the DrawerLayout
here is how it looks like:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2196F3"
android:minHeight="?attr/actionBarSize"/>
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
In my case - ScrollView has handled all touches. I have added android:layout_marginTop="?android:attr/actionBarSize" after Toolbar and it has worked. Hope I helped someone )
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/common_toolbar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?android:attr/actionBarSize"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
... ...
The root problem is in ActionBarDrawerToggle. That's how constructor looks like:
ActionBarDrawerToggle(Activity activity, Toolbar toolbar, DrawerLayout drawerLayout, DrawerArrowDrawable slider, #StringRes int openDrawerContentDescRes, #StringRes int closeDrawerContentDescRes) {
//...
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mDrawerIndicatorEnabled) {
toggle();
} else if (mToolbarNavigationClickListener != null) {
mToolbarNavigationClickListener.onClick(v);
}
}
});
}
And I can't really understand the logic:
toolbar.setNavigationOnClickListener sets click listener for hamburger icon
when user click on hamburger:
2.1 if hamburger visible then open/hide drawer
2.2 if hamburger is not visible then call click listener.
So for most cases hamburger icon should be visible, hence in all these cases setNavigationOnClickListener doesn't work and useless.
Probably it should look like this:
ActionBarDrawerToggle(Activity activity, Toolbar toolbar, DrawerLayout drawerLayout, DrawerArrowDrawable slider, #StringRes int openDrawerContentDescRes, #StringRes int closeDrawerContentDescRes) {
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
if (mToolbarNavigationClickListener != null) {
mToolbarNavigationClickListener.onClick(v);
}
}
});
}
A workaround solution could be overriding toolbar.setNavigationOnClickListener and calling toggle there. Though the method is package private:
package android.support.v7.app
import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.Toolbar
import android.view.View
object SupportUtils {
fun setHamburgerListener(listener: () -> Unit,
drawerToggle: ActionBarDrawerToggle,
drawerLayout: DrawerLayout,
toolbar: Toolbar) {
drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
override fun onDrawerStateChanged(newState: Int) {
drawerToggle.onDrawerStateChanged(newState)
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
drawerToggle.onDrawerSlide(drawerView, slideOffset)
}
override fun onDrawerClosed(drawerView: View) {
drawerToggle.onDrawerClosed(drawerView)
}
override fun onDrawerOpened(drawerView: View) {
drawerToggle.onDrawerOpened(drawerView)
}
})
toolbar.setNavigationOnClickListener {
listener()
drawerToggle.toggle()
}
}
}
private fun setUpToolbarWithHomeAsUp() {
setSupportActionBar(toolbar)
val actionBar = supportActionBar
actionBar?.let{
actionBar.setHomeButtonEnabled(true)
actionBar.setDisplayShowTitleEnabled(false)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
//What ever you want
return true
}
}
return super.onOptionsItemSelected(item)
}

Navigation Drawer is not opening when sliding from left

As the page on Android Developers says
The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar.
But strangely the navigation drawer on my activity does not respond to sliding action. It toggles only on touching the icon on the action bar. Below is my implementation of the navigation drawer
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
// Set up the drawer.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
drawerLayout);
Is there any possible explanation for this? What I doubt is my activity by default has the layout of one of its fragments. So is that the reason?
Edit: The layout file of my activity
<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"
tools:context=".MainPage">
<!-- The main ocntent view -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer-->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:name="com.example.android.handsfree.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
Your ´NavigationDrawerFragment´ already creates a drawer Toggle for you inside its `setUp´ method.
You should not create a new one inside your ´MainPage´ Activity.
Notes:
You can use the android.support.v7.app.ActionBarDrawerToggle instead of the v4 one inside the NavigationDrawerFragment.
Update:
The problem seems solved now. There were 2 Issues:
The OP created a second drawer toggle in it's MainPage - Activity but there was already one created inside the NavigationDrawerFragment's setUp method, which gets called by the MainPage in order to set up the drawer. (This is basicly outsourc[ecoding]ing some of the drawer stuff to the drawer fragment.)
The OP locked the drawer inside onCreateOptionsMenu by calling a method which sets the DrawerLockMode to LOCK_MODE_LOCKED_CLOSED. He never reverted this change.
You should use:
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
<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/white" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/drawer"
android:choiceMode="singleChoice" />
on your activity
drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
listData = (ListView) findViewById(R.id.listview_drawer);
drawlayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
Add these fields in your activity class:
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
Then, inside your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
ic_drawer is a .png image which located in drawable resources. You can search this image on Google.
API level to be used is 11+.
Hope it helps for you.
1- replace the tag by :
<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"
tools:context=".MainPage">
<!-- The main ocntent view -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer-->
<ListView android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent" />
</android.support.v4.widget.DrawerLayout>
2- use this code in the onCreate function u can use the v4 ActionBarDrawerToggle or the v7 her i use the v4:
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer,
R.string.app_name,R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

ListVIew is not a sliding drawer

Hi guys i'm new to android and i'm in a learning phase. I followed tutorial on Navigation drawer from androidhive and made that code backward compatible for 2.3.3.
but i'm getting error as follows:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignment.drawersam/com.assignment.drawersam.MainDrawerActivity}: java.lang.IllegalArgumentException: View android.widget.ListView{4170e148 VFED.VC. ......I. 0,0-0,0 #7f05003e app:id/list_slidermenu} is not a sliding drawer
I'm not able to understand what the error is and how to rectify it.
any help would be appreciated.
code in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItem = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItem.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItem.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItem);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
code in activity_main_drawer.xml:
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainDrawerActivity" >
<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" >
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="#369"
android:dividerHeight="1dp"
android:gravity="start"
android:listSelector="#drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
As mentioned in the documentation, DrawerLayout must be the root view of the layout.
Remove the LinearLayout and let the DrawerLayout be the root view.
I've worked on a version compatible with earlier versions of android (Api level 7 and above) you need android support library v7 also to make it work. Here's the complete code: Sliding menu based on androidhive code for api level 7 and above

Android - Navigation Drawer - ScrollView No Scrolling

i have a problem with a navigation drawer.
I cant scroll inside the navigation drawer ... but i have enough items in my listview.
I've checked my xml's but havent find any cause for the problem.
Here is my "main" XML where the ListView is defined:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com /apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Hauptmenue_extended" >
<ListView
android:id="#+id/left_drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#fff" />
</android.support.v4.widget.DrawerLayout>
And here is my code:
mDrawerLayout = (DrawerLayout) findViewById(R.id.fragView);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Get the titles ...
drawerTitles = getResources().getStringArray(
R.array.drawerTitles_array);
// Get the sub titles ...
drawerSubtitles = getResources().getStringArray(
R.array.drawerSubtitles_array);
// Set the icons ...
drawerIcons = new int[] { R.drawable.icon_buergermeldung,
R.drawable.icon_sprechblase,
R.drawable.icon_action_settings_dark, R.drawable.icon_user_chat, R.drawable.icon_haus, R.drawable.icon_contact, R.drawable.icon_calendar, R.drawable.icon_search, R.drawable.icon_zaehlerstand };
// Create new MenuListAdapter
MenuListAdapter mMenuAdapter = new MenuListAdapter(this,
drawerTitles, drawerSubtitles, drawerIcons);
mDrawerList.setAdapter(mMenuAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Add Navigation Drawer to Action Bar
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(R.string.app_name);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
Can anyone help me?
Try using the XML code from the NavigationDrawer instructions page. A few differences stand out to me. For example, set the ListView height to match_parent instead of wrap_content (weird, I know) and make sure you include the FrameLayout for the content.
<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">
<!-- 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/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Categories

Resources