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
  • 19
  • 1
  • 6

5 Answers5

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,235
  • 2
  • 35
  • 81
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
  • 10,652
  • 6
  • 37
  • 62
0

try following,

ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
Ganpat Kaliya
  • 1,234
  • 2
  • 9
  • 16
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. It's also recommended that you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue. – SuperBiasedMan Sep 22 '15 at 13:49
0

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

Cristi Pufu
  • 8,792
  • 3
  • 34
  • 42