Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove icon/title for actionbar with expanded SearchView?

Tags:

android

I created a new project, targeting api 11 and above. I have an activity where I want a SearchView expanded by default:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search); 
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setIconifiedByDefault(false);
}

which works, but I don't want any icon or title on the actionbar, just the searchview. I tried getting rid of my title and icon by doing:

getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(android.R.color.transparent);

But my actionbar still has this padding to the left of the searchview:

enter image description here

Is there a way to get rid of it?

Thanks

-------- Edit --------------------

Here's my activity in full:

public class GreatAndroid extends FragmentActivity {
    private SearchView mSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActionBar().setDisplayShowHomeEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem mi = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) mi.getActionView();
        mSearchView.setIconifiedByDefault(false);

        return true;
    }
}

and here's the menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/action_search"
      android:title="@string/search"
      android:icon="@android:drawable/ic_menu_search"
      android:showAsAction="always"
      android:actionViewClass="android.widget.SearchView" />
</menu>
like image 438
user291701 Avatar asked Sep 14 '13 22:09

user291701


3 Answers

Even though the icon is transparent, it still takes up the space:

getActionBar().setIcon(android.R.color.transparent);

Remove this line and add:

getActionBar().setDisplayShowHomeEnabled(false);

Now, you should have the SearchView take up all the available space.

Action items that can be collapsed have a max width. The only workaround I can think of is the following:

public class GreatAndroid extends FragmentActivity {
    private SearchView mSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem mi = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) mi.getActionView();
        mSearchView.setIconifiedByDefault(false);

        // You can use Display.getSize(Point) from API 13 onwards.
        // For API 11 and 12, use Display.getWidth()

        final Point p = new Point();

        getWindowManager().getDefaultDisplay().getSize(p);

        // Create LayoutParams with width set to screen's width
        LayoutParams params = new LayoutParams(p.x, LayoutParams.MATCH_PARENT);

        mSearchView.setLayoutParams(params);

        return true;
    }
}

Although it looks like there's still space to the left, you can use mSearchView.setBackgroundColor(Color.RED); to see that there really isn't.

like image 70
Vikram Avatar answered Oct 22 '22 00:10

Vikram


Regarding left padding you could try using collapseActionView

Here is an example menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@string/search_hint"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView" />
</menu>
like image 36
vasanth Avatar answered Oct 22 '22 00:10

vasanth


I could not get @Vikram's awnser to work, but I found that another answer from Stack Overflow:

Open your styles.xml file and add below codes in your Actionbar style

<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
<item name="displayOptions">showHome|homeAsUp|showTitle</item>
<item name="android:icon">@android:color/transparent</item> <--this do the magic!

p/s: I'm using Actionbar Sherlock and this works just fine

(See Remove icon/logo from action bar on android work)

like image 28
jbejar Avatar answered Oct 21 '22 23:10

jbejar