Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix cut-off custom SearchView background in landscape?

I've followed the great answer for this question to make the SearchView widget in my ActionBar fit a custom theme. It works just fine for portrait mode, but in landscape the OS will shrink the height of the ActionBar to maximize content screen estate. Instead of fitting the SearchView widget without margins into the ActionBar just like the other items, it is chopped off at the top:

SearchView widget with chopped off top

I've dug through the res-folder of the Android source, but could not find any specialized layouts or resources that would make obvious how to fix this or even how Android itself manages to fit the default Holo SearchView Widget into the reduced ActionBar.

Any insights are appreciated. THX.

like image 547
Chris Avatar asked Jun 20 '13 09:06

Chris


2 Answers

You have to use a smaller version of the edit text. The drawables instrinsic dimensions are smaller than the default edit text drawables.

Check out the drawables used here

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/frameworks/support/v7/appcompat/res/drawable/abc_textfield_searchview_holo_dark.xml?av=f

like image 80
Jonathan S. Avatar answered Oct 08 '22 12:10

Jonathan S.


I had a similar problem, and was able to fix it by simply changing the Gravity of the EditText:

try {
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    AutoCompleteTextView searchPlate = (AutoCompleteTextView) searchView.findViewById(searchPlateId);

    //fixes the gravity (fixes text cutoff for landscape mode)
    searchPlate.setGravity(Gravity.LEFT|Gravity.BOTTOM);
}
catch (Throwable t)
{
    //not sure if the above code will always work (layouts change), but works well for the current API.
    t.printStackTrace();
}
like image 22
Phil Avatar answered Oct 08 '22 13:10

Phil