38

I have a SearchView inside a LinearLayout and I'm trying to set a different TextSize for it (the SearchView) but when setting android:textSize="60sp" for example, nothing happens.

This is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:padding="2dp">
<SearchView
        android:id="@+id/searchView"
        android:layout_width="350dp"
        android:textSize="60sp"
        android:layout_height="80dp"
        android:layout_marginTop="15dp"/>
<ListView
        android:id="@+id/search_results"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

Does anyone know how to modify the TextSize of the control?

tetius
  • 455
  • 1
  • 4
  • 7

7 Answers7

66

You can achieve this using a theme:

in styles.xml

<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
        <item name="android:textSize">60sp</item>
    </style>

And using a SearchView

<android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="350dp"
            app:theme="@style/AppSearchView"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"/>
42

You could do these using code

SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
autoComplete.setTextSize(60);

Here is another solution

SearchView searchView = new SearchView(context);
AutoCompleteTextView search_text = (AutoCompleteTextView) searchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
search_text.setTextColor(Color.WHITE);
search_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.text_small));
Pongpat
  • 13,248
  • 9
  • 38
  • 51
12

Try my solution (AndroidX).

styles.xml

 <style name="SearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <item name="android:textSize">20sp</item>
 </style>

your_layout_name.xml

<androidx.appcompat.widget.SearchView
      app:defaultQueryHint="Search"
      android:theme="@style/SearchViewStyle" // Not style!!
      .
      .
      .
         />
halfer
  • 19,824
  • 17
  • 99
  • 186
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
4

When using ActionBarSherlock you have to do this:

    AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
    searchTextView.setTextColor(getResources().getColor(R.color.search_text_color));
Vitaly Babiy
  • 6,114
  • 4
  • 26
  • 24
  • Hy. I'm not using ActionBarSherlock. – tetius Oct 29 '12 at 11:49
  • 1
    The question was about text *size*, not colour. For completeness, the second line in your answer should be something like: `searchTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_size))` – gnuf Apr 07 '15 at 11:57
  • Why the change in text_color? OP just wanted to change the text_size – iCantC Jan 17 '20 at 06:23
1

Though AutoCompleteTextView is superclass of SearchAutoComplete, SearchView use SearchAutoComplete rather than AutoCompleteTextView. use it and do more.

SearchView searchView=...;
SearchView.SearchAutoComplete autoComplete= searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
float textSpSize=10.f;
if (autoComplete!=null){
    autoComplete.setTextSize(textSpSize);
   }
chen
  • 172
  • 1
  • 7
1

I've created kotlin extension function

fun SearchView.setTextSize(size: Float) {
    findViewById<EditText>(R.id.search_src_text).textSize = size
}
vadiole
  • 337
  • 1
  • 6
  • 21
-2

Check this answer Can we limit the number of character in edittext of SearchView in Android?. It uses SearchView.OnQueryTextListener in order to check if exceeded the limit.

Community
  • 1
  • 1
Sneg
  • 1,039
  • 1
  • 11
  • 15