0

I'm using the below code in onCreate to display a dropdown menu in the actionbar. The code works fine, it shows the dropdown menu and performs the tasks that i've added in onNavigationItemSelected. But his happens only once i.e if i select Menu Item 2 for the first time the Toast shows "Selected" message and the asynctask is executed. When i reselect Menu Item 2 nothing happens! Why this problem occurs and how can i get rid of this it?

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        final LinearLayout mainLayout = (LinearLayout) this
                .findViewById(R.id.linearLayout1);

        ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

            @Override
            public boolean onNavigationItemSelected(int itemPosition,
                    long itemId) {

                if (actions[itemPosition].toString().equals("exit")) {

                    System.exit(0);
                }
                if (actions[itemPosition].toString().equals(
                        "Menu Item 2")) {

                    Toast.makeText(getApplicationContext(),"Selected" ,Toast.LENGTH_LONG).show();
                    code="2";
                    //call to an asynctask

                }
                return false;
            }
        };

    getActionBar().setListNavigationCallbacks(adapter, navigationListener);
Hitesh
  • 5
  • 4

1 Answers1

0

you have need to read these links then your problem will solve out your strategy of calling menu items are not correct so follow these links

http://developer.android.com/reference/android/view/MenuItem.html
http://developer.android.com/guide/topics/ui/menus.html
http://www.androidhive.info/2011/09/how-to-create-android-menus/
Handling a Menu Item Click Event - Android
Creating an Options Menu in Android

These codes are more useful to make drop down item selection items

code are above :- go to Project and res folder then menu/then your .xml file for menu items

  <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
      android:icon="@drawable/menu_addnew"
      android:title="new_game"
      android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
      android:icon="@drawable/menu_addnew"
      android:title="help" />
 </menu>


  and then in java file you have use this code

   public class AndroidMenusActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

// Initiating Menu XML file (menu.xml)
  @Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.layout.menu, menu);
    return true;
}
 @Override
public boolean onOptionsItemSelected(MenuItem item)
{

    switch (item.getItemId())
    {
    case R.id.new_game:
        // Single menu item is selected do something
        // Ex: launching new activity/screen or show alert message
        Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
        return true;

    case R.id.help:
        Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
Community
  • 1
  • 1
Amitsharma
  • 1,577
  • 1
  • 17
  • 29
  • This is link only answer and link only answers are discouraged – Raghunandan Apr 02 '14 at 04:30
  • it mean you want :- we make codes for you this will make helpful codes for your problem if you read these links carefully then you can implement easily – Amitsharma Apr 02 '14 at 04:32
  • i am addind some other codes for you ok please wait – Amitsharma Apr 02 '14 at 04:32
  • http://meta.stackexchange.com/questions/144566/new-policy-on-link-only-answers. I am not saying this. Pls read the new policy of the site.. – Raghunandan Apr 02 '14 at 04:33
  • try to understand a thing nobody have time to expend time free of cost so if you have bit help then you have to read with his own.if you work then it make you more confident that's by...ok i have update codes – Amitsharma Apr 02 '14 at 04:42
  • i understand. But its the site's new policy so post link's as comments and not as answers. If you don't time have time to expand then why post an answer instead of just posting link as a comment – Raghunandan Apr 02 '14 at 04:44
  • @Raghunandan ok if you are satisfy with answer vote up k...thank u for update me new policy of stack overflow .... – Amitsharma Apr 02 '14 at 05:07