Reputation: 7301
Hey I want to add background color in search view in android. But I am trying to add selector but it's not working. Also I tried this Stackoverflow but it's not working
Search.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/search_edit_text_rounded_corner"
android:theme="@style/SearchViewTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:closeIcon="@drawable/ic_cancel"
app:searchIcon="@drawable/ic_search" />
</androidx.constraintlayout.widget.ConstraintLayout>
styles.xml
<style name="SearchViewTheme" parent="Widget.AppCompat.SearchView">
<item name="android:textColorHint">@color/hint</item>
<item name="android:editTextColor">@color/grey</item>
<item name="android:fontFamily">@font/source_sans_pro_light</item>
<item name="android:textSize">@dimen/text_size</item>
<item name="queryHint">@string/edit_text_hint</item>
<item name="iconifiedByDefault">false</item>
</style>
search_edit_text_rounded_corner.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/search_edit_text_selected" android:state_selected="true" />
<item android:drawable="@drawable/search_edit_text_selected" android:state_pressed="true" />
<item android:drawable="@drawable/search_edit_text_default" />
</selector>
search_edit_text_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/red" />
<stroke
android:width="1dp"
android:color="@color/red" />
<corners android:radius="4dp" />
</shape>
search_edit_text_default.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/off_white" />
<stroke
android:width="1dp"
android:color="@color/cloudy" />
<corners android:radius="4dp" />
</shape>
Actual Output
Scenario 1 :- This is working fine when unfocused
Scenario 2 :- When I tried to select it not changing the color
Expected Output
Scenario 1 is fine. I want to fix in Scenario 2 when user focus and look like this
Upvotes: 1
Views: 2495
Reputation: 19
Add back ground tint it will change the background color
<androidx.appcompat.widget.SearchView
android:backgroundTint=""/>
Upvotes: 1