Navigation Menu in Support Action Bar not opening - android

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!

Related

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);

Navigation drawer: text is hidden below actionbar?

I added navigation drawer to my application. Everything works fine, but now I am trying to add a simple textView to menu, and am not succeeding. The problem is the fact that text is hidden underneath actionBar itself. Even 50dp margin from top isn't enough.
Do you have any tips on how to fix this issue?
My main activity:
<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 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:fitsSystemWindows="true"
android:clipToPadding="false"/>
<!-- The navigation drawer -->
<FrameLayout
android:id="#+id/left_drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/dark_brown">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="40dp"
android:textColor="#ffffff"
android:id="#+id/textView"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"/>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
And my main activities class:
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private DatabaseHandler database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Handle application side menu
sideMenu();
// Set tint for android bar (only for KitKat version)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.action_bar_bg);
}
// Create system objects
database = new DatabaseHandler(this);
final Statistics statistic = new Statistics(database);
// Create main fragment and point app to it
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new MainFragment(database, statistic))
.commit();
}
}
private void sideMenu() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, // host Activity
mDrawerLayout, // DrawerLayout object
R.drawable.ic_drawer, // nav drawer icon to replace 'Up' caret
R.string.drawer_open, // "open drawer" description
R.string.drawer_close // "close drawer" description
) {
// Called when a drawer has settled in a completely closed state.
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(R.string.drawer_close);
}
// Called when a drawer has settled in a completely open state.
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(R.string.drawer_open);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#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);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
#Override
protected void onDestroy() {
super.onDestroy();
database.closeDatabase();
}
}
You want to assign fitsSystemWindows and clipToPadding to the Navigation Drawer fragment (most probably the ListView)
Something like this -
<ListView 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:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
android:fitsSystemWindows="true"
android:clipToPadding="false"
/>
But in your case, you are using the FrameLayout on the activity_main itself to populate the Drawer contents. So you have to apply these two properties to the FrameLayout
<FrameLayout
android:id="#+id/left_drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:fitsSystemWindows="true"
android:clipToPadding="false"
android:background="#color/dark_brown">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="40dp"
android:textColor="#ffffff"
android:id="#+id/textView"
android:gravity="center_vertical"/>
</FrameLayout>
Also, notice, you don't need following to your TextView anymore.
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"

NavigationDrawer and ViewPager in the same Activity in Android

I am trying to create a NavigationDrawer as well as having a ViewPager with sliding tabs in an activity using ActionBarSherlock. I have the tabs and I have created layouts for the view pager and the drawer layout. I know that what I have is that the viewpager layout and drawerlayout are in seperate files and they should probably be in the same file to find the view. Although I do not know how to do this with the code that I have.
I have done a lot of research as to how to do this and the standard pattern is to not do this, although as many people have pointed out, Google Play Music does this. I want my application to look like that and I know that there is a workaround as people have suggested. I am just unsure on how the workaround works with ActionBarSherlock and the code that I currently have implemented. Please can anyone help me with this. Thank you in advance.
Here is my activity code:
public class SlidingTabsActivity extends SherlockFragmentActivity
{
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
private ActionBar actionBarTabs;
/* Navigation drawer */
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
/* Navigation Drawer */
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListView = (ListView) findViewById(R.id.left_drawer);
System.out.println("Drawer Layout test:" + drawerLayout.getId());
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.navigation_drawer_list_item, R.array.navigation_drawer_list));
drawerListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
}
});
actionBarTabs = getSupportActionBar();
actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBarTabs.setDisplayHomeAsUpEnabled(true);
actionBarTabs.setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView)
{
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(drawerToggle);
tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view
/* Adds fragments to the tabs adapter */
tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null);
}
Here is my DrawerLayout layout xml code:
<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/content_frame"
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="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
And here is my ViewPager layout xml code:
<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"
tools:context=".SlidingTabsActivity" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pager" />
</RelativeLayout>
Firstly, your Activity has both Navigation Drawer and ViewPager, but I saw you put the layout descriptions in different xml files. But, you must put only one xml file for your Activity.
Considering this, the key point now is: "how to declare DrawerLayout and ViewPager in the same xml?". If you put in wrong order it won't work (I had this same problem, that's why I'm alerting you). So, the answer is to declare the DrawerLayout as root and inside it, the ViewPager, and later, after closing the ViewPager "tag", you declare the ListView of DrawerLayout.
An example:
<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.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<ListView android:id="#+id/drawer_list"
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>
I hope the answer helps you.

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>

DrawerLayout is hidden below the GridView

I try to implement the latest drawer layout from support library 13.
With the following code, the drawer is always showing below the gridview. Even I try to call bringToFront() still not working. Can help to find what's wrong? Thanks.
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">
<FrameLayout
android:id="#+id/content_frame"
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="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
member_home_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<GridView
android:id="#+id/member_home_thumbnail_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" >
</GridView>
</LinearLayout>
The main activity:
public class BaseRootActivity extends BaseActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mMainMenus;
public ListView getDrawerListView() {
return this.mDrawerList;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainMenus = this.getResources().getStringArray(R.array.main_menu_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) this.findViewById(R.id.left_drawer);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mMainMenus));
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("actionbar title");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("drawer title");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
...
in main fragment
public class MemberHomeFragment extends Fragment implements OnItemClickListener {
private GridView mGridListView;
private UserThumbnailAdapter memberAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.member_home_layout, container, false);
mGridListView = (GridView) rootView.findViewById(R.id.member_home_thumbnail_grid);
mGridListView.setOnItemClickListener(this);
memberAdapter = new UserThumbnailAdapter(this.getActivity(), null);
mGridListView.setAdapter(memberAdapter);
this.startLoading(); // load thumbnails
return rootView;
}
So this isn't a problem with the way you are using the drawer layout as I mentioned earlier. It's a problem with the way you are using fragments. The way the FragmentManager works is you create a fragment transaction, and you tell the fragment transaction a set piece of work and then you commit it, so that it all happens at once.
When you add, remove, or in your case Replace a fragment you have to tell the FragmentManager where to put your fragment, and you were giving it the wrong location. You were telling it to put your fragment inside android.R.id.content, a place that ALL of the content on the screen lives inside. You only want to add a fragment to one your own containers. The fact that the ID you specified (android.R.id.content) begins with 'android', is a big giveaway that it is the android systems, and not yours.
You instead want to put it in the location inside of your navigation drawer that you specified, which is R.id.content_frame. You can see how you specified that in your xml above, which I will copy here for you:
<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/content_frame"
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="0dp"
android:background="#111"/>
I cannot understand but here is the issue:
As #spierce7 pointed out, the way I call fragment replacement is not correct (but why?)
My code is:
getFragmentManager().beginTransaction().replace(android.R.id.content, new MemberHomeFragment()).commit();
and when I changed to:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new MemberHomeFragment()).commit();
It works.

Categories

Resources