Kaboom
Kaboom

Reputation: 684

Check which item is selected in drawer

Building an android 4.0+ application with a slide drawer navigation menu for my users website.

I am adding the items into the drawer using the following function

 private void addDrawerItems() {
    String[] osArray = { "Home", "Store", "Cart", "Account", "Careers", "About Us", "Contact Us" };
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
        }
    });
}

How can I check and see which item is clicked from the menu so I can reload the page to a different url when it is clicked? Say a user clicks "Home" i want to know so I can send to url1 or if they click "Contact Us" I can send them to url2.

Upvotes: 1

Views: 44

Answers (2)

Adan_SL
Adan_SL

Reputation: 358

You can do two things

1) In the method onItemClick the int position returns the value in the array of the clicked item, for example: 0-> Home, 1->Store, 2->Cart

2) You can get the String of the item clicked with this code.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String item = parent.getItemAtPosition(i).toString();     //returns the string of the item
            if(item.equals("Home"){
                         //do your thing in home}
               }
            });

Upvotes: 1

Kaboom
Kaboom

Reputation: 684

Found it literally right after I asked.

if(id == 0) {
    Toast.makeText(MainActivity.this, "You clicked HOME", Toast.LENGTH_SHORT).show();
}

Upvotes: 0

Related Questions