I have read the documentation for NavigationDrawer and tried to create it in my own application. But there is a trouble: the ListView that should be used as menu just floats over the main content and I can't perform any actions with it (e.g. close by swipe). What is the problem?
<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" >
<LinearLayout
android:id="#+id/mainPageLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
android:id="#+id/mainScreenViewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/navigationDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:gravity="start" >
</ListView>
</android.support.v4.widget.DrawerLayout>
And the drawer initialization:
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
Thanks.
Here is a rough sample of codes that work for me, they're a combination of things from android developers site and StylingAndroid blog:
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layoutxml_id);
// must initialize mDrawerLayout and mDrawerToggle in main thread
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) {
// do something
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// do something
}
};
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if(item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
return super.onOptionsItemSelected(item);
}
Related
I am trying to add DrawerLayout to my app. Here is my 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">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:listSelector="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
And activity code:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setupNavigationDrawer();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
private void setupNavigationDrawer() {
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.hello_world,
R.string.hello_world
) {
public void onDrawerClosed(View view) {
//Snackbar.make(view, R.string.drawer_close, Snackbar.LENGTH_SHORT).show();
}
public void onDrawerOpened(View drawerView) {
//Snackbar.make(drawerView, R.string.drawer_open, Snackbar.LENGTH_SHORT).show();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
I can swipe from the left side and see my drawer menu, but I don't see any ActionBar button to the left of activity title that toggles drawer menu. How can I add something like a "hamburger" button to my activity?
Have you tried to put following statement:
//calling sync state is necessary or else your hamburger icon won't show up
mDrawerToggle.syncState();
I have set following ic_menu (attached png image so may you not able to see) drawable as indicator... It works for me perfectly.
/**
* Setting of Actionbar
*/
private void setupToolbar() {
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
}
See following function:
/**
* In case if you require to handle drawer open and close states
*/
private void setupActionBarDrawerToggle() {
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
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) {
//Snackbar.make(view, R.string.drawer_close, Snackbar.LENGTH_SHORT).show();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
//Snackbar.make(drawerView, R.string.drawer_open, Snackbar.LENGTH_SHORT).show();
}
};
//Setting the actionbarToggle to drawer layout
mDrawerLayout.setDrawerListener(mDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
mDrawerToggle.syncState();
}
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
Drawer.setDrawerListener(mDrawerToggle);// use this line to set Drawer toggle
You would be better off using NavigationView, from the support package. Much easier to use, less code, etc.
http://android-developers.blogspot.no/2015/05/android-design-support-library.html
http://antonioleiva.com/navigation-view/
https://developer.android.com/reference/android/support/design/widget/NavigationView.html
I begin a new project, just copy/paste code from an old project where everything is ok. In this one toggle button doesn't change states(it is only arrow and is not working when I am pressing it) like:
to
open/close drawer is working fine using swipe touch
Have to notice that I am using only one Activity that have 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/base_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/base_container_for_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent">
//elements for screen
</RelativeLayout>
<!--that is just for drawer-->
<ListView
android:id="#+id/navigationDrawerList"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#color/color_red_style" />
in my activity:
//declaring vars:
private ListView mDrawerList;
protected DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
in onCreate() :
dataList = new ArrayList<DrawerItem>();
mDrawerLayout = (DrawerLayout) findViewById(R.id.base_layout);
mDrawerList = (ListView) findViewById(R.id.navigationDrawerList);
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
addDrawerItems() is working ok
protected void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer, R.string.close_drawer) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(getResources().getString(R.string.bar_menu));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle("produse");
// creates call to onPrepareOptionsMenu()
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
and of course I put handle toggle in onOptionsItemSelected(MenuItem item)
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
So what's the problem,I just can't realize... Any ideas will be appreciated.
omg I forgot to sync button toggle state :)
I had to add this in my MainActivity() :
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
Im using Android Studio 1.2
The icon drawer in actionbar is give me an error when I click then but is work fine If I open it sliding with the hand from the left to the rigth.
this is my layout where I have my drawer list from the left; the list of options is in listView "mimenu"
<RelativeLayout xmlns:android="xxxxxx"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:background="#ffffff"
tools:context="xxxx">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="#+id/listaxx"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
>
<ListView
android:id="#+id/mimenu"
android:layout_width="match_parent"
android:layout_below="#+id/profileBox"
android:choiceMode="singleChoice"
android:layout_height="match_parent"
android:background="#ffffff"
android:divider="#eee"
android:dividerHeight="1dp"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
In my class java
public class ListaPat extends Activity {
public ArrayList<Pat> lstPat= new ArrayList<Pat>();
private Pat_Adaptador adaptador;
private LinearLayout linearLayout;
ArrayAdapter<CharSequence> navigationDrawerAdapter;
ListView leftDrawerList;
String[] leftSliderData = new String[]{"test1","test2"};
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_patx);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
leftDrawerList = (ListView)findViewById(R.id.mimenu);
navigationDrawerAdapter= new ArrayAdapter<CharSequence>( ListaPat .this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
RellenarNoticias();
InicializarLista();
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
mDrawerLayout.closeDrawer(leftDrawerList);
}
else {
mDrawerLayout.openDrawer(leftDrawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to extend either ActionbarActivity, or AppCompatActivity, and use a Toolbar widget to be in the beginning of your Activity. In your activities on create, do the following:
public class YourActivity extends ActionBarActivity{
...
...
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//Set toggle with toolbar now!
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
And for your Toolbar that lives in your activity xml (the very first element for your layout!)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#color/primary_color"
android:title="#string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="CreateSomeStyleHereIfYouNeed"
app:popupTheme="#android:style/Theme.Holo.Light"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</android.support.v7.widget.Toolbar>
I fix the problem changing this function
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
mDrawerLayout.closeDrawer(leftDrawerList);
}
else {
mDrawerLayout.openDrawer(leftDrawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
New function that fix the problem
#Override public boolean onOptionsItemSelected(MenuItem item) {if (mDrawerToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }
Im beginner in Android app developement. I have implement custom action bar in Android app.I have add 2 images on custom action baar see below xml code
custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/black_pattern" >
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="#null"
android:src="#android:drawable/ic_menu_rotate" />
</RelativeLayout>
And set this xml file in actionbar view
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Refresh Clicked!",
Toast.LENGTH_LONG).show();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
}
But right now i want to open and close the navigation drawer on imageButton click which included in custom.xml layout and add in action baar. I implement navigation drawer also , but i don't understand how to show and hide navigation drawer on imageButton click event.Can someone help how to do this
Im trying this way
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = "test";
mPlanetTitles = new String[]{"one", "two", "three"};
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
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) {
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return 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
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
if you have the navigation drawer implemented you can try:
NavigationDrawerFragment mNavigationDrawerFragment;
DrawerLayout mDrawerLayout;
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout);
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Refresh Clicked!",
Toast.LENGTH_LONG).show();
if(mDrawerLayout.isDrawerOpen(Gravity.END))
mDrawerLayout.closeDrawer(Gravity.START);
else
mDrawerLayout.openDrawer(Gravity.END);
}
});
Create a drawer 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">
<!-- List of Actions (pages) -->
<ListView android:layout_height="match_parent"
android:layout_width="280dp"
android:id="#+id/navList"
android:background="#ffffffff"
android:choiceMode="singleChoice"
android:layout_below="#+id/profileBox"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
private DrawerLayout drawer;
// DrawerLayout
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) findViewById(R.id.navList);
Add the drawer content to the listview
and use
drawer.openDrawer(Gravity.LEFT);
drawer.openDrawer(Gravity.LEFT);
To open and close the drwaer on imge click
I've been following http://developer.android.com/training/implementing-navigation/nav-drawer.html to set up a navigation drawer in my app. Once an item in the drawer has been selected, the current fragment should be removed and replaced with a new ListFragment. However, both remain and are drawn on top of each other.
mainpage.xml:
<?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="#android:color/background_light">
<!-- The main content view -->
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/mainListFragment"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:name="com.example.SpeakEasy.MainPageListFragment"
android:layout_alignParentLeft="true"
android:layout_margin="2dp">
</fragment>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="0dp"
android:background="#android:color/background_light">
</ListView>
</android.support.v4.widget.DrawerLayout>
MainPage.java:
public class MainPage extends SherlockFragmentActivity {
public static AmazonClientManager clientManager = null;
protected UiLifecycleHelper uiHelper;
private String[] categoryTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
clientManager = new AmazonClientManager(getSharedPreferences("speakeasySDB", Context.MODE_PRIVATE));
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
setTitle("Main Feed");
setContentView(R.layout.mainpage);
categoryTitles = getResources().getStringArray(R.array.navigationDrawerCategories);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_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);
getSupportActionBar().setTitle(getTitle());
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(getTitle());
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.activity_list_item, android.R.id.text1, categoryTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
/**
* Swaps fragments in the main content view
*/
private void selectItem(int position) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = null;
// Create a new fragment and specify the planet to show based on position
switch (position) {
case 0:
Toast.makeText(this, "Following not yet implemented", Toast.LENGTH_SHORT).show();
break;
case 1:
fragment = new HomePageListFragment();
fragmentTransaction.replace(R.id.mainListFragment, fragment).commit(); //R.id.content_frame didn't work either
break;
}
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
getSupportActionBar().setTitle(categoryTitles[position] + "Quotes");
mDrawerLayout.closeDrawer(mDrawerList);
}
}
Any ideas what could be happening?
I see that you have fragment declared in the layout file but you are trying to use it in a dynamic way. Replace the fragment with FrameLayout/LinearLayout.
Then you can do transaction.replace(id,fragment,tag).commit();
Hope this helps.