My question is very clear that i want to change the Up icon When NavigationDrawer open and closed. So please tell me how to Change the upp icon in anction bar. I have tried so much but i m not able to do. ii have used custom layout to set the title. Thanks in advance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.center_action_bar_text);
actionBar.setTitle("dvds");
View view = actionBar.getCustomView();
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText("zfdgfdg");
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
---------------------------------
drawerListView.setAdapter(drawerListAdapter);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(HomeActivity.this,
drawerLayout, R.drawable.abc_ic_clear_search_api_holo_light, R.string.drawer_open,
R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
Add this method to your code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
i have other one answer
just add this method:-
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//boolean drawerOpen1 = mDrawerLayout.isDrawerOpen(this.slider);
//menu.findItem(R.id.sliding).setVisible(!drawerOpen);
if(drawerOpen)
{
menu.findItem(R.id.sliding).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
}
else {
menu.findItem(R.id.sliding).setIcon(R.drawable.menuicon);
}
//menu.findItem(R.id.sliding).setIcon(R.drawable.ic_launcher);
//menu.findItem(R.id.action_settings).setVisible(!drawerOpen1);
return super.onPrepareOptionsMenu(menu);
}
for that method call you need to use this tow method
invalidateOptionsMenu();
// calling onPrepareOptionsMenu() to hide action bar icons
when menu is open
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
//mDrawerLayout.openDrawer(Gravity.END);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
when menu is close
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
//menu.findItem(R.id.sliding).;
//mDrawerLayout.closeDrawer(Gravity.END);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
Related
I've been trying to create a navigation drawer for an existing app. I've found a few tutorials for this, but most of them (including the official Android guide) appear to be for the v4 ActionBarDrawerToggle library, which has been deprecated. I'm trying to use the v7 library instead, but my ActionBarDrawerToggle doesn't seem to do what the documentation says it should do.
Edit: Modified my code as per the answer below. The hamburger icon now switches back and forth correctly, but when the user taps the hardware back button to go back to the main fragment of my app, the hamburger icon disappears entirely. Why does this happen?
private void addDrawerItems() {
String[] itemArray = {"About", "Nearby", "Settings", "Feedback",};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ContributionsActivity", "Item " + position + " selected");
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.title_activity_contributions);
setContentView(R.layout.activity_contributions);
//Set up navigation drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.drawer_list);
addDrawerItems();
setupDrawer();
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// enabling drawer toggle by clicking on the app icon.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case android.R.id.home:
if (mediaDetails.isVisible()) {
getSupportFragmentManager().popBackStack();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
You're missing sync state, add it and everything should be fine.
I'm using the v7.widget.Toolbar in my app, but I'm getting some funky functionality. I have my main activity and fragments that are placed over it. When there are no fragments on the backStack, the hamburger button shows and the menu works correctly. When I add a fragment to the backStack, the up caret shows correctly, however when I click the up caret, the nav menu opens instead of the fragment being popped off the stack.
Now if there's a real answer, I'll take it, but at this point I will take a hackish solution. I tried adding a listener so I knew when the action bar button was hit, but that just made it so the fragment popped, the page went back, but the nav menu still opened. onOptionsItemSelected is not being called (due to the way I implemented the Drawer Toggle, but doing it the "correct" way gave me way more problems, such as no nav menu showing on the main page at all).
To sum it up for clarity: The up caret is opening the nav menu, instead of going back.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContent = findViewById(R.id.drawer_content);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new DrawerListItemAdapter(DRAWER_ITEMS, getApplicationContext()));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
Log.d("Main", "Open Menu");
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
ActionBar ab = getSupportActionBar();
if(ab != null){
ab.setDisplayHomeAsUpEnabled(canback);
}
if(!canback){
//App can crash as mDrawerToggle will be null when app launches
try{
mDrawerToggle.syncState();
}catch (Exception e){
e.printStackTrace();
}
}
Log.d("Main", "shouldDisplayHomeUp");
}
#Override
public boolean onSupportNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
Log.d("Main", "Up carat pressed");
getSupportFragmentManager().popBackStack();
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);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("Main", "Menu item clicked: " + Integer.toString(item.getItemId()));
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
The solution that worked for me
A combination of Alex' answer (including his comment in the answer) below and this hacky answer.
You can use setToolbarNavigationClickListener() - it sets the listener that handles clicks when drawer indicator is disabled
drawerToggle.setToolbarNavigationClickListener((View view) -> {
getSupportFragmentManager().popBackStack();
});
I have an activity with a navigation drawer that works as it should. The only problem is that the hamburger menu doesn't animate after the first fragment replacement.
public class Main extends AppCompatActivity {
// lots of attributes
#Override
protected void onCreate(Bundle in) {
super.onCreate(in);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
setupDrawer();
}
public void onEventMainThread(LoadedLEsEvent event) {
setupDrawer();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
void setupDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, navDrawer, R.string.drawer_open, R.string.drawer_closed) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
drawerToggle.setDrawerIndicatorEnabled(true);
navDrawer.setDrawerListener(drawerToggle);
menuListAdapter = new MenuListAdapter(this, R.layout.drawer_item, someList);
menuListView.setAdapter(menuListAdapter);
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Replaces the frame layout with a fragment
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
So basically it works fine until I replace the FrameLayout with a fragment. From here, the drawer still works but the Hamburger menu doesn't animate at all.
So the problem was that I initialized drawerToggle each time I called setupDrawer(). Moving drawerToggle = new ActionBarDrawerToggle(...) {...}; to onCreate() did the trick.
I am creating simple app having DrawerLayout navigation. In which one menu item is My Profile. In My profile Screen there is button which open Change Password screen in same Fragment. if i open DrawerLayout 's menu and close it without clicking, then My Profile Screen loads again.
Following is my code
public class HomeActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ArrayList<SideMenuEntity> listSideMenuItems;
private SideMenuAdapter adapterSideMenu;
Menu menu;
Fragment fragment;
AsyncTaskHelper loadFragmentTask;
int position, old_position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
listSideMenuItems = new ArrayList<>();
for(String str : Constant.SIDE_MENU_ITEMS)
{
listSideMenuItems.add(new SideMenuEntity(str));
}
// setting the nav drawer list adapter
adapterSideMenu = new SideMenuAdapter(getApplicationContext(),listSideMenuItems);
mDrawerList.setAdapter(adapterSideMenu);
mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
displayView(position);
}
});
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.mipmap.ic_launcher,R.string.app_name, R.string.app_name)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
if (fragment != null) {
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
}
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
old_position = -1;
displayView(-1);
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_home_setting, menu);
_setActionBarHomeVisible(false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId())
{
case R.id.title_bar_home:
displayView(0);
return true;
case R.id.title_bar_setting:
_setFragmentContainer(new SettingFragment(), "Setting");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
*
*/
protected void displayView(int p)
{
position = p >= 0 ? p : 0;
fragment = null;
switch (position)
{
case Constant.SIDE_MENU_ITEM_DAHSHBOARD:
fragment = new DashboardFragment();
break;
case Constant.SIDE_MENU_ITEM_MY_PROFILE:
fragment = new MyProfileFragment();
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**-------------- private functions ---------------------*/
public void _setFragmentContainer(Fragment fragment, String title)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
setTitle(title);
}
}
Try adding the super constructor:
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
if (fragment != null) {
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
}
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
i solve the problem my self own. actually whenever DrawerLayout closed, onDrawerClosed event fired, then i just null the fragment variable after _setFragmentContainer() function
if (fragment != null)
{
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
fragment = null;
}
Now DrawerLayout Menu closed, due to fragment null , it don't load fragment again.
I have a problem with the NavigationDrawer icon, I want it to be visible within the ActionBar. It is needed to show the user, that he can open the drawer in my app by swiping from the left edge of the screen.
I use this code before without any problems, but in this application it isn't working well, I don't know what the problem can be. Please help me.
Here is my code:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
ArrayList<Integer> smIcon_adrs = new ArrayList<Integer>();
smIcon_adrs.add(R.drawable.sm_font);
smIcon_adrs.add(R.drawable.sm_size);
smIcon_adrs.add(R.drawable.sm_count);
smIcon_adrs.add(R.drawable.sm_about);
ArrayAdapter<Integer> sm_adapter = new smIcoAdapter(getBaseContext(),
smIcon_adrs);
mDrawerList.setAdapter(sm_adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.hello_world, R.string.app_name) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle("دعا عهد");
supportInvalidateOptionsMenu();
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("امکانات");
supportInvalidateOptionsMenu();
super.onDrawerOpened(drawerView);
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerLayout.setDrawerListener(mDrawerToggle);
and here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_play:
if (mp.isPlaying()) {
item.setIcon(R.drawable.actn_play);
mp.pause();
} else {
updateProgressBar();
mp.start();
item.setIcon(R.drawable.actn_stop);
}
break;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onPrepareOptionsMenu(Menu menu) {
mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
Here is someone with a similar problem, you might test the solution posted there: https://stackoverflow.com/a/23332975/1738838.
According to that you are missing this method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}