Change border color of a SearchView

young_08 picture young_08 · Apr 27, 2016 · Viewed 8.7k times · Source

I want to change a border color of my searchview . Right now its coming transparent . I tried giving styling to it but nothing working .

SearchView.xml

 <LinearLayout
        android:id="@+id/linear1"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">

    <android.support.v7.widget.SearchView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"
        android:background="@android:color/white"
        android:layout_marginTop="10dp"
        style="@style/CitySearchView"
        android:layout_weight="0.9"
        android:layout_marginLeft="10dp"/>

style.xml

<style name="CitySearchView" parent="Widget.AppCompat.Light.SearchView">
        <item name="searchIcon">@drawable/blue_search_bar</item>
        <item name="colorControlActivated">@color/labelColorHighligh</item>
        <item name="colorControlNormal">@color/backgroud_user</item>
        <item name="queryBackground">@color/labelColorHighligh</item>

    </style>

Answer

Rohit  Rathore picture Rohit Rathore · Apr 27, 2016
  1. Create drawable file background.xml with this content:

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="12dp"
        android:shape="rectangle" >
        <solid android:color="#FFFFFF" />
        <stroke
            android:width="0.3dp"
            android:color="#797979" />
    </shape>     
    
  2. Add the drawable to the background:

    <android.support.v7.widget.SearchView
        android:background="@drawable/background"
        android:id="@+id/searchView"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_width="300dp" />