3

I want to set the text color and hint text color to the text in a android.support.v7.widget.SearchView which is not in ActionBar and not in Menu option.I have tried few ways mentioned in stackoverflow. But none of them is working.

int searchSrcTextId =getResources().getIdentifier("android:id/search_src_text", null, null);  
EditText searchEditText = (EditText) searchView.findViewById(searchSrcTextId);  
searchEditText.setTextColor(Color.WHITE);  
searchEditText.setHintTextColor(Color.LTGRAY);  

I tried the above code,but it is giving Null pointer exception at the setTextColor line.Any kind of help will be appreciated.Thanks in advance.

TheHound.developer
  • 396
  • 1
  • 3
  • 16

4 Answers4

13

I would do this kind of job through the xml layout file. This can be found in the res/layout folder.

You would need to do something similar to the following:

Add this to the parent theme.

<item name="android:editTextColor">@android:color/white</item>

This should change the entered text.

You can also use something like this:

<item name="android:textColorHint">@android:color/white</item>

It will change the hint text for the SearchView.

Hope this helps :)

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
5

I have got one more solution for my issue.While changing textcolor programmatically as in my question the id I have given was wrong. We should edit the code like below for android.support.v7.widget.SearchView.

EditText editText = ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
    editText.setHintTextColor(getResources().getColor(R.color.white));
    editText.setTextColor(getResources().getColor(R.color.white));
TheHound.developer
  • 396
  • 1
  • 3
  • 16
2

For androidx:

 EditText searchEditText = (EditText) search_view_home.findViewById(androidx.appcompat.R.id.search_src_text);
 searchEditText.setTextColor(getResources().getColor(R.color.white));
 searchEditText.setHintTextColor(getResources().getColor(R.color.white));
shubomb
  • 672
  • 7
  • 20
-1

Use this on the toolbar.

    <androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/toolbarColor"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:contentInsetStartWithNavigation="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:navigationIcon="@drawable/ic_arrow_back"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:title="Toolbar"
    app:titleTextColor="@color/toolbarTextColor"/>

and paste this to style.xml or theme.xml

    <style name="ThemeOverlay.AppCompat.Dark.ActionBar" parent="Base.ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="ThemeOverlay.AppCompat.Light" parent="Base.ThemeOverlay.AppCompat.Light"/>