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:
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>
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.
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>
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With