Reputation: 1119
I am currently trying to implement the new NavigationDrawer as per the Google IO 2013 guidelines. I am using ActionbarSherlock. The code is working well. The only issue is that I am not able to toggle the "up" caret next to my app icon when the navigation drawer is pulled out. I am pasting my entire code below. Kindly help.
public class MainActivity extends SherlockFragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
new Thread(new Runnable() {
@Override
public void run() {
prefs = getPreferences(MODE_PRIVATE);
opened = prefs.getBoolean(OPENED_KEY, false);
if(opened == false)
{
mDrawerLayout.openDrawer(mDrawerList);
}
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(getMenuItem(item))) {
return true;
}
switch (item.getItemId()) {
case R.id.action_websearch:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private android.view.MenuItem getMenuItem(final MenuItem item) {
return new android.view.MenuItem() {
@Override
public int getItemId() {
return item.getItemId();
}
public boolean isEnabled() {
return true;
}
@Override
public boolean collapseActionView() {
return false;
}
@Override
public boolean expandActionView() {
return false;
}
@Override
public ActionProvider getActionProvider() {
return null;
}
@Override
public View getActionView() {
return null;
}
@Override
public char getAlphabeticShortcut() {
return 0;
}
@Override
public int getGroupId() {
return 0;
}
@Override
public Drawable getIcon() {
return null;
}
@Override
public Intent getIntent() {
return null;
}
@Override
public ContextMenuInfo getMenuInfo() {
return null;
}
@Override
public char getNumericShortcut() {
return 0;
}
@Override
public int getOrder() {
return 0;
}
@Override
public SubMenu getSubMenu() {
return null;
}
@Override
public CharSequence getTitle() {
return null;
}
@Override
public CharSequence getTitleCondensed() {
return null;
}
@Override
public boolean hasSubMenu() {
return false;
}
@Override
public boolean isActionViewExpanded() {
return false;
}
@Override
public boolean isCheckable() {
return false;
}
@Override
public boolean isChecked() {
return false;
}
@Override
public boolean isVisible() {
return false;
}
@Override
public android.view.MenuItem setActionProvider(ActionProvider actionProvider) {
return null;
}
@Override
public android.view.MenuItem setActionView(View view) {
return null;
}
@Override
public android.view.MenuItem setActionView(int resId) {
return null;
}
@Override
public android.view.MenuItem setAlphabeticShortcut(char alphaChar) {
return null;
}
@Override
public android.view.MenuItem setCheckable(boolean checkable) {
return null;
}
@Override
public android.view.MenuItem setChecked(boolean checked) {
return null;
}
@Override
public android.view.MenuItem setEnabled(boolean enabled) {
return null;
}
@Override
public android.view.MenuItem setIcon(Drawable icon) {
return null;
}
@Override
public android.view.MenuItem setIcon(int iconRes) {
return null;
}
@Override
public android.view.MenuItem setIntent(Intent intent) {
return null;
}
@Override
public android.view.MenuItem setNumericShortcut(char numericChar) {
return null;
}
@Override
public android.view.MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
return null;
}
@Override
public android.view.MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
return null;
}
@Override
public android.view.MenuItem setShortcut(char numericChar, char alphaChar) {
return null;
}
@Override
public void setShowAsAction(int actionEnum) {
}
@Override
public android.view.MenuItem setShowAsActionFlags(int actionEnum) {
return null;
}
@Override
public android.view.MenuItem setTitle(CharSequence title) {
return null;
}
@Override
public android.view.MenuItem setTitle(int title) {
return null;
}
@Override
public android.view.MenuItem setTitleCondensed(CharSequence title) {
return null;
}
@Override
public android.view.MenuItem setVisible(boolean visible) {
return null;
}
};
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* 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);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends SherlockFragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
// change int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
int imageId = getResources().getIdentifier("tempmap", "drawable", getActivity().getPackageName());
// change ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
// change getActivity().setTitle(planet);
getActivity().setTitle("MapView");
return rootView;
}
}
}
Upvotes: 7
Views: 6082
Reputation: 62559
try to call syncState on the toggle drawer like this:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//your ActionBarDrawerToggle is below
mDrawerToggle.syncState();
}
or just right after you instantiate it.
Upvotes: 11
Reputation: 611
If you don't need the 3 lines to move (like in gmail app), you could just add:
<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
in your activity's theme. I prefer this to using yet another library in my app.
ic_drawer can be downloaded from here: http://developer.android.com/training/implementing-navigation/nav-drawer.html
Upvotes: 11
Reputation: 312
I have the caret removed in 4.x devices, but not for 2.x devices. The repository is here if you need it: https://github.com/bcrider/NavigationDrawerSherlocked
It's simply the sample that Google supplied that I modified using ActionBarSherlock as you have done as well. I fixed an issue with a method (onOptionsItemSelected) in the sample that originally prevented the tapped icon from doing anything after ActionBarSherlock was integrated into the app.
Hope this helps!
Upvotes: 0
Reputation: 88
I had the same problem and did some research on that topic. It does not seem to be easy to get the caret working correctly on older SDKs. Long story short, there's a guy who has a running example implementation that's based on reflection:
https://github.com/nicolasjafelle/SherlockNavigationDrawer
I tested it and it works perfectly for me, even on Android 2.x !
Upvotes: 3