6
 public void onPopup(View view)
{
    final PopupMenu menu=new PopupMenu(this,view);
    menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu());


    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
    {
        public boolean onMenuItemClick(MenuItem item)
        {

            Toast toast=Toast.makeText(MainActivity.this,
                    item.getTitle()+"Selected",Toast.LENGTH_SHORT);
            //Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
            //startActivity(intent2);

            //startActivity(new Intent(MainActivity.this,YourSpotActivity.class));

    toast.show();
    return true;
        }
    });
    menu.show();

}

When i click any one of the list item then it will start another activity. How can i do that by modify an above code. explain me please. I have use four car model in the menu. when i choose any one of that car then it will go to particular activity.

Rameshbabu
  • 611
  • 2
  • 7
  • 21

2 Answers2

3

You need to use switch as below

  switch (item.getItemId()) {
    case R.id.menuitem1:
    Toast.makeText(getApplicationContext(), "StartActiviy 1", Toast.LENGTH_SHORT).show();
        // start activity 1
       return true;
    case R.id.menuitem2:
    Toast.makeText(getApplicationContext(), "StartActiviy 2", Toast.LENGTH_SHORT).show();
       // start activity 2
       return true;
     default:
      //default intent
       return true;
     }

http://developer.android.com/reference/android/widget/PopupMenu.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • tel me the procedure for changing the list item font size and background. – Rameshbabu Jul 27 '13 at 07:38
  • http://stackoverflow.com/questions/16878662/how-to-set-the-background-of-android-popupmenu-to-white. – Raghunandan Jul 27 '13 at 07:40
  • http://stackoverflow.com/questions/12636101/how-to-style-popupmenu. one more here. – Raghunandan Jul 27 '13 at 07:43
  • i had an error when i am including menu. show an error it requires api level 13 current is 8. but i want api level 8 only. how can it possible. – Rameshbabu Jul 27 '13 at 07:45
  • i want your help frequently bro. i am developing android project. I am new to android. So kindly give me your mail id. I will send my doubts and code to you. Please. – Rameshbabu Jul 27 '13 at 07:47
  • no i want minimum api level 8. because basic android phone also work on it.. help me . – Rameshbabu Jul 27 '13 at 07:48
  • you can ask your doubts here with the relevant details i am not an expert. i can only answer a few question that i am aware off. there more highly reputated people on SO. So why not post it here. it will be visible to large pool of developers. – Raghunandan Jul 27 '13 at 07:48
  • @Rameshababu no you can't use popup menu below api 11 http://developer.android.com/reference/android/widget/PopupMenu.html. clearly stated **Added in API level 11** – Raghunandan Jul 27 '13 at 07:49
  • k but i saw it. It works in one application name say taxi. it works well. that's why i am asking bro. – Rameshbabu Jul 27 '13 at 07:53
  • i have not seen that application i stick to the docs and the docs states added in api level 11 so it won't be available below 11. try google search or ask a new question. probably you will get a workaround for this. – Raghunandan Jul 27 '13 at 07:55
2

You can use switch statement as below inside onMenuItemClick:

 switch (item.getItemId()) {
        case R.id.menuitem1:
            //calling intent ( activity1 )
        case R.id.menuitem2:
          //calling intent ( activity 2)
         default:
          //default intent
   }
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61