1

I am attempting to display icon with search bar in Android nav bar but it just keeps displaying just the icon. Here is my xml code

<item android:id="@+id/action_login"
        android:title="Login"
        android:icon="@android:drawable/ic_lock_lock"
        app:showAsAction="always|withText" />

here the xml full code

<?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">


   <item android:id="@+id/action_login"
        android:title="Login"
        android:icon="@android:drawable/ic_lock_lock"
        app:showAsAction="always|withText" />
</menu>

what could be wrong

Float
  • 263
  • 2
  • 14

3 Answers3

0

Change this -:

android:icon="@android:drawable/ic_lock_lock"

To-:

android:icon="@drawable/ic_lock_lock"

or

android:icon="@mipmap/ic_lock_lock"
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
0

I have resolved this problem by following code:

1- Create menu in menu folder

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

<item
   android:id="@+id/action_sign"
   android:title="Sing In"
   app:showAsAction="always|withText"
   app:actionLayout="@layout/menu_sign" />

2- app:actionLayout is the key word, Then create layout named menu_sign and put textView in it. the icon will displayed by drawableLeft

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:clickable="true"
     android:drawableLeft="@mipmap/ic_user_sign"
     android:gravity="center"
     android:orientation="vertical"
     android:text="@string/text"
     android:drawablePadding="@dimen/margin_5"
     android:paddingLeft="@dimen/margin_10"
     android:paddingRight="@dimen/margin_10"
     android:textColor="@android:color/white" />

3- Add listener for your menu

super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_sign, menu);
signItem = menu.findItem(R.id.action_sign);
signItem.getActionView().setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onOptionsItemSelected(signItem);
    }
});
Hakim Khan
  • 317
  • 5
  • 9
0

Your code worked properly in Android 5.0 and above.

But in android 4.2 display only icon, not text!!

So if you worked on below 5.0 then use actionLayout.

Refer this answer: How to get text on an ActionBar Icon?

Hope this will help you

Mitesh Vanaliya
  • 2,491
  • 24
  • 39