Custom ActionBar AppCompatActivity buttons not showing on Android 4.1.2
It works fine on 5.1 but not 4.1.2
What am I missing?
Here is the xml activity:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/transfer"
android:title="@string/transfer"
android:icon="@drawable/ic_transfer"
myApp:showAsAction="always" />
<item android:id="@+id/refresh"
android:title="@string/refresh"
android:icon="@drawable/ic_refresh"
myApp:showAsAction="always" />
<item android:id="@+id/about"
android:title="@string/about"
android:icon="@android:drawable/ic_menu_info_details"
android:orderInCategory="100"
myApp:showAsAction="never" />
<item android:id="@+id/help"
android:title="@string/title_activity_help"
android:icon="@android:drawable/ic_menu_info_details"
android:orderInCategory="101"
myApp:showAsAction="never" />
<item android:id="@+id/settings"
android:title="@string/settings"
android:icon="@android:drawable/ic_menu_info_details"
android:orderInCategory="102"
myApp:showAsAction="never" />
<item android:id="@+id/search"
android:title="@string/search"
android:icon="@android:drawable/ic_menu_info_details"
android:orderInCategory="103"
myApp:showAsAction="never" />
<item android:id="@+id/logout"
android:title="@string/logout"
android:icon="@android:drawable/ic_menu_info_details"
android:orderInCategory="104"
myApp:showAsAction="never" />
</menu>
And here is my activity class (witch extends another activity that extends AppCompatActivity):
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_activity, menu);
return true;
}
I am using the following appcompat library:
'com.android.support:appcompat-v7:22.2.0'
+3
source to share
2 answers
Thanks for your reply. I did what these two answers suggested:
How to force menu overflow on devices with menu button
Actionbar not shown with AppCompat
To summarize the solution:
- I changed "myApp" to "compat" in xml action (missing buttons fixed)
-
I added this code
try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); if(menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception ex) { // Ignore }
to my base activity (which extends to all other activities) which extends AppCompatActivity (missing menu fixed)
It now works in both 5.1 and 4.1.2
+6
source to share
You should use the toolbar instead.
Put this in your XML:
<android.support.v7.widget.Toolbar
android:id="@id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
This code goes in your activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getTitle());
}
}
Than you just inflate your menu:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
+4
source to share