Can I show icon in overflow menu of Android Toolbar?
create a menu in menu, for example: main_activity_actions.xml
and use this code for the toolbar, try it:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:All4One="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/algo"//some id
android:icon="@drawable/kami_nomi_zu_shiru_sekai3" //add the image
android:title="nose"
All4One:showAsAction="always">
<menu>
<item android:id="@+id/WIFI"//option to display on the menu
android:icon="@drawable/nisekoi1"//this is a option of the submenu
android:title="@string/WIFI"/>
<item android:id="@+id/DATOS"
android:icon="@drawable/nisekoi6" //this one also is a option
android:title="@string/DATOS"/>
<item android:id="@+id/BT"
android:icon="@drawable/sao1"
android:title="@string/BT"/>
<item android:id="@+id/NADA"
android:icon="@drawable/nisekoi6" //the same
android:title="@string/NADA"/>
</menu>
</item>
</menu>
Show icons in Overflow Menu in Contextual Action Bar
I waited for 2 days but didn't get any answer on this. So solved it myself.
The idea is quite simple here. You need to create your own overflow item in the and the create a nested menu to show both the icon and the text.
See the example code below,
<item
android:id="@+id/overflow"
android:icon="@drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="@string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="@+id/cab_menu_select_all"
android:icon="@drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="@string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
The trick here is to create nested menus. You can add as many items as you want.
How do I show Overflow Menu Items to Action Bar in Android
To show three dot icon, to Action Bar, just use below method in your OnCreate():
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
ActionBar: Up icon and Overflow menu icons not appearing, but working
As you are using Theme.AppCompat.Light.DarkActionBar
, your toolbar back
icon and overflow
icon showing as white
color and I guess you have used colorPrimary
as white
.
Change colorPrimary
to other color OR you can use Theme.AppCompat.Light
, it will show back
and overflow
icon as black
.
Here is the fully working code
Style.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item> <!-- WHITE -->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
AndroidManifest.xml:
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
OUTPUT:
Hope this will help~
How to add icon to the items in the overflow menu in the action bar
If you want icon you need to change the value of ShowAsAction="never" to ShowAsAction="always"
Try the next code.
<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="com.example.MainActivity" >
<item
android:id="@+id/menu_item_options"
android:icon="@drawable/ic_config"
android:title="@string/action_settings"
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_config"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="always"/>
<item
android:id="@+id/action_close"
android:orderInCategory="100"
android:title="@string/action_close"
app:showAsAction="ifRoom"/>
</menu>
</item>
Related Topics
How to Create a User-Defined Function in SQLite
How to Change Endianness Mid-Execution on Arm (Android/Linux)
Android Textwatcher.Aftertextchanged VS Textwatcher.Ontextchanged
How to Create Edittext Accepts Alphabets Only in Android
How to Record Phone Calls in Android
How to Set Dialog to Show in Full Screen
"Native Typeface Cannot Be Made" Only for Some People
Android 4.3 Bluetooth Low Energy Unstable
How to Prevent Android Bluetooth Rfcomm Connection from Dying Immediately After .Connect()
Listview with Clickable/Editable Widget
Shell Script in Android Gives [: Not Found
How to Set Versionname in APK Filename Using Gradle
Android 5.0 Android:Elevation Works for View, But Not Button
Android: Activity Getting Destroyed After Calling Camera Intent