1

guys I develop an app in Java for Android (min SDK 19), and I try to add a search button in my app but this icon don't appear. Please see the code below:

This is my search.xml in @menu folder

<?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/search"
    android:icon="@drawable/ic_search"
    android:title="@string/search"
    android:orderInCategory="100"
    app:showAsAction="ifRoom"/>
</menu>

This is may Activity Main WHY my icon search dosen't appear? because I already added the icon in @drawble

package com.squarcy.equals;

import android.support.v7.widget.Toolbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.res.Resources;

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);
    TextView title = toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    title.setText(toolbar.getTitle());
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
}

// Get Status Bar Height
private int getStatusBarHeight() {
    int height;
    Resources myResources = getResources();
    int idStatusBarHeight = myResources.getIdentifier(
            "status_bar_height", "dimen", "android");
    if (idStatusBarHeight > 0) {
        height = getResources().getDimensionPixelSize(idStatusBarHeight);
    }else{
        height = 0;
    }
    return height;
    }

    // Inflate Search
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search,menu);
    return true;
    }

    // If Icon Search is Clicked
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
    }

Android Emulator

String Search

2 Answers2

0

Try first setting showAsAction="always" and removing this line toolbar.setPadding(0, getStatusBarHeight(), 0, 0); to be sure the padding is not interfering in the render process of the search button.

Marc Estrada
  • 1,657
  • 10
  • 20
  • I remove toolbar.setPadding and change showAsAction to always but icon dosen't appear! –  May 13 '18 at 10:24
  • Opening the options menu (3 dots) does it appears on it? – Marc Estrada May 13 '18 at 10:57
  • Yes, i have a string in options menu android:title="@string/search" –  May 13 '18 at 11:06
  • If you want I can invite you to the project in GitLab to solve this problem –  May 13 '18 at 11:08
  • Ok, so if the button appears the inflate is correct. What is not working is the `showAsAction`. Which version of support do you use? Depends on the support lib version it's needed to use `app:showAsAction` or `android:showAsAction`. You could try to use `android:showAsAction` and also could provide your Gradle module file. – Marc Estrada May 13 '18 at 12:22
  • @Marc_Estrada app:showAsAction="ifRoom" is work, and resolve dependencies! Its work fine now! –  May 19 '18 at 09:54
  • @Marc_Estrada I have another issue https://stackoverflow.com/questions/50424094/titles-in-tablayout-donsent-appear –  May 19 '18 at 10:11
0

I finally found the solution!! Android Studio was giving me the error "should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto". If I changed my XML as suggested, my menus in the actionBar disappeared into the overflow.

Which imported the appcompat library, and caused all the trouble. Since I only targeted Android 4.4 and up, I was able to remove these two lines. Problem solved!

The real culprits turned out to be the following lines in the file build.gradle:

dependencies {
    …
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1'
}

And this for me search.xml

 <item
        android:id="@+id/search"
        android:title="@string/search"
        android:icon="@drawable/ic_search"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />

Solved

More Info: https://developer.android.com/about/versions/android-4.4

Also can help: Item with app:showAsAction not showing