Drawer toggle not working as expected (icon remains the same) - android

I'm trying to implement an ActionBarDrawerToggle into my app but I can't make it. I've achieved to show the toggle in my toolbar, but the icon is the same always.
This is toggle's icon when drawer is closed:
http://i.stack.imgur.com/HTcom.png
And this is when drawer is opened:
http://i.stack.imgur.com/dX0Z2.png
As you can see, it doesn't change from ic_drawer to back arrow. ic_drawer is never shown.
So, here's my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initializeDrawer();
populateDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void initializeDrawer() {
tagTitles = getResources().getStringArray(R.array.item_names);
icons = getResources().getStringArray(R.array.item_icons);
dwLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dwList = (ListView) findViewById(R.id.drawer_list);
dwList.setOnItemClickListener(new DrawerItemClickListener());
dwToggle = new ActionBarDrawerToggle(this, dwLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle("pepe");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("pop");
invalidateOptionsMenu();
}
};
dwLayout.setDrawerListener(dwToggle);
}
private void populateDrawer() {
ArrayList<DrawerItem> dwItems = new ArrayList<>();
for (int i = 0; i < tagTitles.length; i++) {
//ignore this; population is not properly working
dwItems.add(new DrawerItem(tagTitles[i], Resources.getSystem().getIdentifier("abc_ic_menu_copy_mtrl_am_alpha.png", "drawable", getBaseContext().getPackageResourcePath())));
}
dwList.setAdapter(new NavigationDrawerAdapter(this, dwItems));
}
#Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
dwToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (dwToggle.onOptionsItemSelected(item)) {
return true;
}
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
dwToggle.onConfigurationChanged(newConfig);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
Thanks!

implement following method with DrawerLayout instace, in your case instance is dwLayout. implement following after this line
dwLayout.setDrawerListener(dwToggle);
add this
drawerLayout.post(new Runnable() {
#Override
public void run() {
// To display hamburger icon in toolbar
drawerToggle.syncState();
}
});
or you can do this as well
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

Related

How to access a drawer layout in all the activities of my android app?

I am developing a quiz app which consists of drawer layout in all the activities.The contents in the drawer is same and i want it in all the activities.By searching google i found a solution that declaring drawer in one activity and inherit in all the activities,it works well too.I am attaching the code below.
Now the problem is that i need Tab in one of by activity so i don't know how to inherit 2 activity in here[which is not possible].
Can any one help me out with this problem?
Simply saying,
I have a option that i can extend only i class what to do if i want two classes?
DrawActivity.java
public void onCreateDrawer() {
// R.id.drawer_layout should be in every activity with exactly the same id.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(DrawActivity.this,drawerLayout,R.drawable.ic_drawer2,R.string.app_name,R.string.app_name)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(R.string.app_name);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//getActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged","").toString().equals("logged")) {
layers = getResources().getStringArray(R.array.layers_array1);
flag=0;
}
else {
layers = getResources().getStringArray(R.array.layers_array);
flag=1;
}
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,layers));
drawerList.setSelector(R.color.darkgreen);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
selectItemFromDrawer(pos);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

Moving the Drawer Toggle From Left To Right in Action Bar

In my Activity I have a drawer list that pops from the right but I'm not able to move the drawer toggle from the left to the right side. Please if somebody can help me solve this issue.I just want to move the toggle to the right of the action bar.
package com.parse.starter;
import com.parse.ParseUser;
public class UserDrawer extends AppCompatActivity {
//Declaring Variables
private ListView DrawerList;
private ArrayAdapter<String> Adapter;
private ActionBarDrawerToggle DrawerToggle;
private DrawerLayout DrawerLayout;
private String ActivityTitle;
final ParseUser currentUser = ParseUser.getCurrentUser();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_drawer);
DrawerList = (ListView) findViewById(R.id.navList);
DrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Button Sample = (Button) findViewById(R.id.button);
}
//Method To Add Items To The List View
private void addDrawerItems() {
String[] DArray = {"Job List", "Notifications", "Messages", "Log Out"};
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DArray);
DrawerList.setAdapter(Adapter);
DrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent i0 = new Intent(UserDrawer.this, Set_Info.class);
startActivity(i0);
} else if (position == 1) {
//Intent i1 = new Intent(Drawer1.this, AddPatient.class);
//startActivity(i1);
} else if (position == 2) {
//Intent i2 = new Intent(Drawer1.this, Notifications.class);
//startActivity(i2);
} else if (position == 3) {
//Intent i3 = new Intent(Drawer1.this, Message_Log.class);
//startActivity(i3);
} else if (position == 4) {
Intent i4 = new Intent(UserDrawer.this, MainActivity.class);
startActivity(i4);
Toast.makeText(getApplicationContext(), "You are Logged Out", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
private void setupDrawer() {
DrawerToggle = new ActionBarDrawerToggle(this, DrawerLayout,
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);
getSupportActionBar().setTitle("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(ActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
DrawerToggle.setDrawerIndicatorEnabled(true);
DrawerLayout.setDrawerListener(DrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null && item.getItemId()==android.R.id.home){
if(DrawerLayout.isDrawerOpen(Gravity.RIGHT)){
//Notice the Gravity.Right
DrawerLayout.closeDrawer(Gravity.RIGHT);
}else{
DrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
DrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
DrawerToggle.onConfigurationChanged(newConfig);
}
#SuppressWarnings("ResourceType")
public void SampleClick(View view) {
try {
Intent i = new Intent(UserDrawer.this,Set_Info.class);
startActivity(i);
} catch (Exception e) {
}
}
}

Motion event has invalid pointer count 0 >> On opening/ closing/ selecting in Navigation Drawer

I get this error if I do anything with my navigation drawer.
Motion event has invalid pointer count 0; value must be between 1 and 16.
This error doesn't crash my activity. But just floods my log. Below is the activity. The adapter is custom adapter having image and text in a row.
public class FirstActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ListView navList;
private NavAdapter navAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
navList = (ListView)findViewById(R.id.navigation_list);
navAdapter = new NavAdapter(this);
navList.setAdapter(navAdapter);
navList.setOnItemClickListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opendrawer, R.string.closedrawer);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
navList.setItemChecked(position, true);
String menuItem = (((TextView)view.findViewById(R.id.drawerTV)).getText().toString());
getSupportActionBar().setTitle(menuItem);
}
}

Drawer toggle not animating after Fragment replacement

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.

How do I change my button image with navigation drawer opening and closing

I have a navigation button for my navigation fragment which turns active and opens a navigation drawer menu upon click:
Now when I click it, it turns active as follows:
However, I want to associate it with a navigation drawer such a way that, even if I do not click the button and slide open the navigation drawer, the button turns active when the navigation drawer menu is open and when closed by sliding back in from right to left, the button turns red/inactive. The code which I am trying to work with is as follows:
private boolean mIsNavigationOpen = false;
private DrawerLayout drawerLayout;
private NavigationPanelFragment dlDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
public boolean isNavigationOpen() {
return mIsNavigationOpen;
}
//----------Code for Navigation open logo button active/inactive instances
#SuppressWarnings("deprecation")
public void setNavigationOpen(final boolean isNavigationOpen) {
this.mIsNavigationOpen = isNavigationOpen;
final ImageButton mainButton = (ImageButton) findViewById(R.id.button_main);
if(isNavigationOpen) {
mainButton.setBackgroundResource(R.drawable.bg_helios_active);
} else {
mainButton.setBackgroundDrawable(null);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//----------Code for Navigation Drawer setup
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close){
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
// invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
// invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
private void setupOnClickListenerForMainButton() {
final ImageButton mainButton = (ImageButton) findViewById(R.id.button_main);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
toggleNavigationPanel();
}
});
}
#Override
public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggleNavigationPanel();
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
public void onNewsClicked(final View view) {
if(mIsNavigationOpen) {
toggleNavigationPanel();
}
if (isFragmentVisible(NewsFragment.TAG_NEWS_FRAGMENT)) {
return;
}
FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
mActiveFragment = NewsFragment.newInstance(getSupportFragmentManager());
updateActionBarTitle();
//For swipe action close drawer on button click
drawerLayout.closeDrawer(R.id.drawer);
}
public void onListsClicked(final View view) {
if(mIsNavigationOpen) {
toggleNavigationPanel();
}
if (isFragmentVisible(ListsContainerFragment.TAG_LIST_CONTAINER_FRAGMENT)) {
return;
}
FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
mActiveFragment = ListsContainerFragment.newInstance(getSupportFragmentManager());
updateActionBarTitle();
//For swipe action close drawer on button click
drawerLayout.closeDrawer(R.id.drawer);
}
private void toggleNavigationPanel() {
//final FragmentStackManager manager = FragmentStackManager.getInstance();
if (mIsNavigationOpen) {
//NavigationPanelFragment.removeInstance(getSupportFragmentManager());
updateActionBarTitle();
drawerLayout.closeDrawer(R.id.drawer);
} else {
drawerLayout.openDrawer(R.id.drawer);
final TextView title = (TextView) findViewById(R.id.main_title);
title.setText(getString(R.string.title_applications));
//NavigationPanelFragment.newInstance(getSupportFragmentManager(), manager.getTopTitle());
}
setNavigationOpen(!mIsNavigationOpen);
}
You might want to concentrate on the main_button and togglenavigationpanel. I added the condition if(drawerlayout.isdraweropen(R.id.drawer)){closedrawerlayout...}
but it didn't do the trick. I was wondering if anyone has any idea regarding the same?
Thanks!
When you've set up the drawer a listener has been added which has callbacks for when the drawer is in either final state, so what you need to do is reset the drawable inside these callbacks. I've shown what I think your final code should look like, minus some cleaning up you need to do, hope it helps
private DrawerLayout drawerLayout;
private NavigationPanelFragment dlDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ImageButton mDrawerButton; //Store it! - findViewById is *Expensive*
private TextView mTitle;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mTitle = (TextView) findViewById(R.id.main_title);
mDrawerButton = (ImageButton) findViewById(R.id.button_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
mDrawerButton.setImageResource(R.drawable.bg_helios_inactive);
}
public void onDrawerOpened(View drawerView) {
mDrawerButton.setImageResource(R.drawable.bg_helios_active);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
setupOnClickListenerForMainButton(;
}
#Override //Note: I'd imagine this should be in onResume ...
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
private void setupOnClickListenerForMainButton() {
mDrawerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
toggleNavigationPanel();
}
});
}
...
private void toggleNavigationPanel() {
//if ( drawerLayout.isOpen() ) { //I dont know what methods these objects have
if ( dlDrawer.isOpen() ) { //but one of these must be sensibly storing its own state
updateActionBarTitle();
drawerLayout.closeDrawer(R.id.drawer);
} else {
drawerLayout.openDrawer(R.id.drawer);
mTitle.setText(getString(R.string.title_applications));
}
}

Categories

Resources