Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the height of the search view in android?

Tags:

android

I have to reduce the height of the search view in android, i have set the size in the XML file but it appeared as follows

enter image description here

How do i reduce the size of the cursor(The search here hint is not displaying perfectly). I want to set the size of the text in search view as the same size of 'x' button

search view XML is follows

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingLeft="5dip" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="6dp"
        android:text="@string/medium_text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchView"
        android:orientation="horizontal"
        android:weightSum="10" >

        <SearchView
            android:id="@+id/searchView"
            android:layout_width="0dp"
            android:layout_height="25dp"
            android:layout_below="@+id/textView1"
            android:layout_weight="9"
            android:imeOptions="actionSearch"
            android:textSize="2dp" >
        </SearchView>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_weight="0.5"
            android:onClick="hierOnclick"
            android:src="@drawable/ic_list" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_weight="0.5"
            android:src="@drawable/icon_add_account" />
    </LinearLayout>
....
......
........
</LinearLayout>

I cannot set the height as wrap_content(Please suggest some other way)

EDIT: Thanks for your comments I wish to create a search component as follows

enter image description here

to achieve this am planing to set a square background to the search view. but i cannot adjust the height of the search view.

Please help me

Thank you very much

like image 857
Amith Avatar asked Feb 02 '26 08:02

Amith


1 Answers

By default SearchView uses abc_search_view.xml as it's layout file.

abc_search_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

    <!-- This is actually used for the badge icon *or* the badge label (or neither) -->
    <TextView
            android:id="@+id/search_badge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginBottom="2dip"
            android:drawablePadding="0dip"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="?android:attr/textColorPrimary"
            android:visibility="gone" />

   ** Omitted for brevity **

The root view is R.id.search_bar, which can be referenced and manipulated in application code to adjust height of search view:

searchView.findViewById<LinearLayout>(R.id.search_bar)?.run {
    this.layoutParams = this.layoutParams.apply {
        height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25f, resources.displayMetrics).toInt()
    }
}

Then you'll probably want to center the search bar:

searchView.gravity = Gravity.CENTER_VERTICAL
like image 189
veritas1 Avatar answered Feb 04 '26 22:02

veritas1