0

My problem is with menu item icon . I can't see any icon in action bar . There is simply just one activity in my app . In fact it's a simple hello word and i want add an icon to action bar enter image description here

menu.xml:

<?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"
>
<!-- black icon -->
<item
    android:id="@+id/action_create_order"
    android:title="create order"
    android:orderInCategory="1"
    app:showAsAction="ifRoom"
    android:icon="@drawable/ic_add_black_36dp"
    >
</item>
<!-- white icon -->
<item
    android:id="@+id/action_setting"
    android:title="settins"
    app:showAsAction="never"
    android:orderInCategory="100"
    >
</item>


</menu>

MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main , menu);
    return super.onCreateOptionsMenu(menu);
}
}

why icon doesn't appear in action bar ?

amir_70
  • 737
  • 1
  • 10
  • 27

3 Answers3

1

try by changing

app:showAsAction="ifRoom"

to

app:showAsAction="always"

EDIT:

I suggest you change your Activity to AppCompatActivity and also use a Toolbar as your ActionBar

  • i tried that before i posting this question . there is no difference between what i'm using ifroom , always , never ..... the result is the same – amir_70 Aug 14 '17 at 23:35
  • @amir_70 OK, Then I suggest you change your Activity to AppCompatActivity and also use a Toolbar as your ActionBar check this [link](http://www.viralandroid.com/2015/08/android-toolbar-example.html) – Adolfo Lozano Mendez Aug 14 '17 at 23:44
  • thank you . it's working now by changing activity to appcompatactivity . but what's the difference ? why using activity won't show the icon – amir_70 Aug 14 '17 at 23:47
  • @amir_70 I think its because AppCompatActivity its a evolution of activity https://stackoverflow.com/a/31297546/3029553 – Adolfo Lozano Mendez Aug 14 '17 at 23:54
  • ok . can you replace your answer with your second comment . i want to check it as true answer – amir_70 Aug 14 '17 at 23:59
1

If you use extends Activity you have to use android:showAsAction inside menu.xml

If you want to use app:showAsAction, you have to extend AppCompatActivity as Adolfo Lozano Mendez suggested

Kubik
  • 610
  • 7
  • 15
0

Maybe there is no enough place to show icon. Try to rotate the screen (phone/emulator).

Boken
  • 4,825
  • 10
  • 32
  • 42