Question

res/menu/mainmenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:logo="@drawable/ic_action_settings"
    android:showAsAction="ifRoom|withText"
    android:title="@string/action_settings"/>

<item
    android:id="@+id/action_logout"
    android:logo="@drawable/logout"
    android:showAsAction="ifRoom|withText"
    android:title="@string/action_logout"/>


</menu>

MainMenu.java public class MainMenu extends ActionBarActivity {

    Button userinfo, requestservice, makepayment, trackparcel, checkcard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        // Show the Up button in the action bar.
        // setupActionBar();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);

        userinfo = (Button) findViewById(R.id.userinfo);
        requestservice = (Button) findViewById(R.id.requestservice);
        makepayment = (Button) findViewById(R.id.makepayment);
        trackparcel = (Button) findViewById(R.id.usertrackbutton);
        checkcard = (Button) findViewById(R.id.checkcard);

        userinfo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, UserInfo.class);
                startActivity(intent);
            }
        });
        requestservice.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, RequestService.class);
                startActivity(intent);

            }
        });
        makepayment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, Payment.class);
                startActivity(intent);

            }
        });
        trackparcel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, TrackParcel.class);
                startActivity(intent);
            }
        });
        checkcard.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, CheckCard.class);
                startActivity(intent);
            }
        });

    }

    public void onBackPressed() {
        new AlertDialog.Builder(MainMenu.this)
                .setTitle("Logout")
                .setMessage("Would you like to logout?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Intent intent = new Intent(MainMenu.this,
                                        Login.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                SharedPreferences sp = PreferenceManager
                                        .getDefaultSharedPreferences(MainMenu.this);
                                Editor edit = sp.edit();
                                edit.clear();
                                edit.commit();
                                startActivity(intent);
                                finish();

                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // user doesn't want to logout
                    }
                }).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;

        case R.id.action_logout: {
            new AlertDialog.Builder(MainMenu.this)
                    .setTitle("Logout")
                    .setMessage("Would you like to logout?")
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    Intent intent = new Intent(MainMenu.this,
                                            Login.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    SharedPreferences sp = PreferenceManager
                                            .getDefaultSharedPreferences(MainMenu.this);
                                    Editor edit = sp.edit();
                                    edit.clear();
                                    edit.commit();
                                    startActivity(intent);
                                    finish();

                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // user doesn't want to logout
                                }
                            }).show();
        }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

What I want to ask is that why my action items won't appear in my ActionBar eventhough I already wrote android:showAsAction="ifRoom|withText" or android:showAsAction="always" I don't know why it behave like this.

Was it helpful?

Solution

You are extending ActionbarActivity suggests you use Actionbar from the support library.

Change to

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:logo="@drawable/ic_action_settings"
    app:showAsAction="ifRoom|withText"
    android:title="@string/action_settings"/>

<item
    android:id="@+id/action_logout"
    android:logo="@drawable/logout"
    app:showAsAction="ifRoom|withText"
    android:title="@string/action_logout"/>


</menu> 

Quoting docs

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

OTHER TIPS

I have done the silly mistake here.. This is the correct code of what I have changed

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        yourapp:showAsAction="always"
        android:title="@string/action_settings"/>

    <item
        android:id="@+id/action_logout"
        android:icon="@drawable/logout"
        yourapp:showAsAction="always"
        android:title="@string/action_logout"/>

</menu>

Change android:logo="@drawable/ic_action_settings" to android:icon="@drawable/ic_action_settings"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top