Samsul Hoq
Samsul Hoq

Reputation: 55

How to set or remove android SearchView top and bottom padding?

I develop an android apps. I use SearchView in my application. I want to customise it.But I don't remove it's top and bottom padding.When SearchView's height decrements,textbox drowned.Flowing output view-

enter image description here

I want to-

enter image description here

XML Layout code:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <SearchView
    android:id="@+id/search_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/search_text" />

  <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

Java Code:

public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener {

private SearchView mSearchView;
private ListView mListView;

private final String[] mStrings = Cheeses.sCheeseStrings;


private ArrayAdapter<String> adaptor;

android.widget.Filter filter ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.searchview_filter);

    mSearchView = (SearchView) findViewById(R.id.search_view);
    mListView = (ListView) findViewById(R.id.list_view);
    adaptor=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            mStrings);
    filter = adaptor.getFilter();
    mListView.setAdapter(adaptor);
    mListView.setTextFilterEnabled(false);
    setupSearchView();
}

private void setupSearchView() {
    mSearchView.setPadding(0, 0, 0, 0);
    mSearchView.setPaddingRelative(0, 0, 0, 0);
     LinearLayout.LayoutParams seachV = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 75);
     seachV.setMargins(5,10, 5, 0);
     mSearchView.setLayoutParams(seachV);
     mSearchView.setBackgroundResource(R.drawable.search_text);

     EditText e = (EditText)mSearchView.findViewById(mSearchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
   e.setBackgroundResource(R.drawable.search_text);
   int searchPlateId = mSearchView.getContext().getResources()
           .getIdentifier("android:id/search_plate", null, null);
   View searchPlateView = mSearchView.findViewById(searchPlateId);
   if (searchPlateView != null) {
       searchPlateView.setBackgroundColor(Color.WHITE);
   }
    mSearchView.setIconifiedByDefault(false);
    mSearchView.setOnQueryTextListener(this);
    mSearchView.setSubmitButtonEnabled(true); 
    mSearchView.setQueryHint("Search Here");

}
@Override
public boolean onQueryTextChange(String newText) {        
    filter.filter(newText);
    return true;
}

 public boolean onQueryTextSubmit(String query) {
      return false;
  }
}

Any Hints and Example will be helpful.

Upvotes: 2

Views: 2462

Answers (1)

Sams
Sams

Reputation: 340

You do not explain it properly. You want to customize SearchView layout. It is a LinearLayout.You go to "*\path-to-your-android-sdk-folder\platforms\android-xx\data\res\layout*" and find search_view layout. And SearchView class is here. Now you customize Programmatically.

Upvotes: 2

Related Questions