Failed to change the Up cursor next to the application icon in the ActionBar in my navigation box

I am currently trying to implement a new NavigationDrawer in accordance with the rules of Google IO 2013. I am using ActionbarSherlock. The code works well. The only problem is that I cannot switch the “up” carriage next to my application icon when the navigation drawer is pulled out. I embed all my code below. Please 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; } } } 
+7
source share
4 answers

If you do not need 3 lines to move (for example, in the gmail application), you can simply add:

 <item name="homeAsUpIndicator">@drawable/ic_drawer</item> <item name="android:homeAsUpIndicator">@drawable/ic_drawer</item> 

in your activity topic. I prefer this to use another library in my application.

ic_drawer can be downloaded here: http://developer.android.com/training/implementing-navigation/nav-drawer.html

+11
source

try calling syncState in the switch box, for example:

  @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); //your ActionBarDrawerToggle is below mDrawerToggle.syncState(); } 

or immediately after its creation.

+11
source

I had the same problem and did some research on this topic. It doesn't seem easy to get the carriage to work correctly on older SDKs. In short, there is a guy who has a reflection example of execution:

https://github.com/nicolasjafelle/SherlockNavigationDrawer

I tested it and it works great for me, even on Android 2.x!

+3
source

I have a carriage removed in 4.x devices but not for 2.x devices. The repository is here if you need it: https://github.com/bcrider/NavigationDrawerSherlocked

This is just a sample that Google provided to me, which I modified using ActionBarSherlock, as you did. I fixed the problem with the method (onOptionsItemSelected) in the sample, which initially did not allow the icon with the button pressed to do something after the ActionBarSherlock was included in the application.

Hope this helps!

0
source

All Articles