0

What went wrong on my code that Its not able to show my any Android ActionBar icons. Below is my code: Correct where its getting missed.

My style.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Customize your theme here. -->



</style>

My themes.xml: I have used custom theme. I think from here I need to change to show icons

 <?xml version="1.0" encoding="utf-8"?>

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>

    <item name="android:actionMenuTextColor">@style/MyActionBarTitleText</item>

    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>

</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/PrimaryColor</item>
</style>


<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
    parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:background">@color/ActionBarTitleText</item>
</style>



<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
    parent="@android:style/Widget.Holo.ActionBar.TabText">
    <item name="android:textColor">@color/ActionBarTitleText</item>
</style>

MainActivity:

//ActionBar creating/adding icon in Menu
@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_activity_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

//ActionBar Menu icon listerner like clicking options
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.info:
            Toast.makeText(getApplicationContext(),
                    "Info Selected",Toast.LENGTH_SHORT).show();
        case R.id.st:
            Toast.makeText(getApplicationContext(),
                    "Setting Selected",Toast.LENGTH_SHORT).show();
        default:
            return super.onOptionsItemSelected(item);
    }
}

main_activity_menu.xml: I have tried "always" & "IfRoom" But its not working.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item
    android:id = "@+id/info"
    android:title="info"
    android:icon="@drawable/ic_info_outline_white_24dp"
    app:showAsAction="always|withText">

</item>

<item
    android:id = "@+id/st"
    android:title="settings"
    android:icon="@drawable/ic_settings_white_24dp"
    app:showAsAction="always">

</item>

Bharat
  • 17
  • 1
  • 6

4 Answers4

2

You should use android:showAsAction = "ifRoom"

1

If you use AppCompatActivity, try this

Add this code to yours Activity class:

@Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
    if (menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
            }
        }
    }
return super.onPrepareOptionsPanel(view, menu);
}
Community
  • 1
  • 1
mohax
  • 4,435
  • 2
  • 38
  • 85
0

You'd better return true in the onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_activity_menu, menu);
    return true;
}

In addition to that there seems some other problems in your code. For example, you should put break at the tail of each case sentence.

case R.id.info:
    Toast.makeText(getApplicationContext(),
            "Info Selected",Toast.LENGTH_SHORT).show();
    break;
case R.id.st:
    Toast.makeText(getApplicationContext(),
            "Setting Selected",Toast.LENGTH_SHORT).show();
    break;
hata
  • 11,633
  • 6
  • 46
  • 69
0

You just needed to change app:showAsAction to android:showAsAction, because you were not using the compatibility themes

Cristi Pufu
  • 9,002
  • 3
  • 37
  • 43