fighter-ii
fighter-ii

Reputation: 557

NavigationDrawer menuItem setChecked doesnt work

i created a BaseActivity with a NavigationDrawer from the Android support design library.

public abstract class BaseActivity extends ActionBarActivity {  

final static int MENUITEMS = 5;

private Toolbar toolbar;                              // Declaring the Toolbar Object

NavigationView navigationView;
private DrawerLayout drawerLayout;

Context basecontext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(getLayoutId());

/* Assinging the toolbar object ot the view
and setting the the Action bar to our toolbar
 */
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initializing NavigationView

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {


            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) {
                menuItem.setChecked(false);

            } else {
                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {


                    //Replacing the main content with ContentFragment Which is our Inbox View;
                    case R.id.menu_start:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(BaseActivity.this, Start.class);
                        startActivity(intent);
                        // For rest of the options we just show a toast on click
                        //TODO

                    case R.id.menu_all_entries:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    case R.id.menu_missed_entries:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    case R.id.menu_diagram:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    case R.id.menu_export:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    default:
                        Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();

                }
            }

            //Closing drawer on item click
            drawerLayout.closeDrawers();
            //String localClassName = getLocalClassName();
            //Check to see which item was being clicked and perform appropriate action
            return true;

        }
    });

This works just fine.

Now i have a "Sub"-Activity (nothing that is represented as a menu item).

In this Activity:

public class AddEntryPage1 extends BaseActivity {


@Override
protected int getLayoutId(){
    return R.layout.activity_add_entry_page1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
    for(int i=0;i<MENUITEMS;i++){
        navigationView.getMenu().getItem(0).setChecked(false);
    }
}
}

i tried to set all my menuitems to unchecked, but nothing happens.

When i evaluate the expression

navigationView.getMenu().getItem(0).isChecked()

it is always true (before and after the for-loop/getItem(0).setChecked(false)) .

I also get no error on this.

Seems like i have no writing access? Is it a problem that i have initiated my drawer.xml with a selected item? When i click an other menu item the selected item changes as expected. Or does checkableBehavior="single" means, that one item must be checked?

<group android:checkableBehavior="single">

    <item
        android:id="@+id/menu_start"
        android:checked="true"
        android:icon="@drawable/ic_action_home"
        android:title="Start" />

    <item
        android:id="@+id/menu_all_entries"
        android:checked="false"
        android:icon="@drawable/ic_action_view_list"
        android:title="Alle Einträge" />

    <item
        android:id="@+id/menu_missed_entries"
        android:checked="false"
        android:icon="@drawable/ic_action_view_list"
        android:title="Verpasste Einträge" />

    <item
        android:id="@+id/menu_diagram"
        android:checked="false"
        android:icon="@drawable/ic_editor_insert_chart"
        android:title="Diagramm" />


    <item
        android:id="@+id/menu_export"
        android:checked="false"
        android:icon="@drawable/ic_social_share"
        android:title="Export" />

</group>

Thanks for helping me out!

Upvotes: 2

Views: 5951

Answers (4)

fighter-ii
fighter-ii

Reputation: 557

For anyone who might have the same issue:

I reported the issue to Google: https://code.google.com/p/android/issues/detail?id=184089

Upvotes: 3

RogerParis
RogerParis

Reputation: 1559

How about:

try to delete the:

 android:checked="false"

and the code i have if it helps is:

 navigationView = binding.navigationView;
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            drawerLayout.closeDrawers();
            int itemId = menuItem.getItemId();
            navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
            return true;
        }
    });

Upvotes: 0

fighter-ii
fighter-ii

Reputation: 557

I found the reason:

Strangely

navigationView.getMenu().getItem(1).setChecked(false);

activates/checks the entry

and

.setChecked(true) 

also activates/checks it.

Can anybody reproduce this? Might that be a bug?

Upvotes: 0

Patel Hiren
Patel Hiren

Reputation: 305

//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) {
         // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
    }
}

Upvotes: 0

Related Questions