Learning Always
Learning Always

Reputation: 1561

Change color of android.support.v7.widget.SearchView

I am using search view.

  <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/imageViewMenu"
            >
  </android.support.v7.widget.SearchView>

Is this possible to change text color and icon color to view?

Currently it shows black text and icon. I want to change it to white color text and white search icon.

Upvotes: 6

Views: 15551

Answers (6)

beshoy samy
beshoy samy

Reputation: 144

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
    <item name="colorControlHighlight">@color/LightGray</item>
    <item name="android:textColorHint">@color/LightGray</item>
    <item name="android:editTextColor">@color/TransparentGray</item>
</style>

apply this theme to your toolbar hint that (colorControlHighlight) color of ripple effct of your menu items

Upvotes: 3

saiedmomen
saiedmomen

Reputation: 1431

If it resides in toolbar, set theme for toolbar. For example this makes the text(and icon) white

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

Upvotes: 5

user1365789
user1365789

Reputation: 81

For the android.support.v7.widget.SearchView you can change the text color, text hint color and the icon with this code :

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem searchMenuItem = menu.findItem(R.id.menu_action_search);
    SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());

    searchViewAction.setSearchableInfo(searchableInfo);
    searchViewAction.setIconifiedByDefault(true);

    EditText searchEditText = (EditText)searchViewAction.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.white));
    searchEditText.setHintTextColor(getResources().getColor(R.color.white));

    ImageView searchMagIcon = (ImageView)searchViewAction.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchMagIcon.setImageResource(R.drawable.ic_search_white);


    return super.onPrepareOptionsMenu(menu);
}

Upvotes: 7

Naveen Kumar M
Naveen Kumar M

Reputation: 7557

To Change the Text color :

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); 

To Change the close button :

int closeButtonId = getResources().getIdentifier("android:id/search_close_btn", null, null);  
ImageView closeButtonImage = (ImageView) searchView.findViewById(closeButtonId);  
closeButtonImage.setImageResource(R.drawable.ic_action_cancel);  

// This excerpt is from Android source code (SearchView.java)

mSearchButton = findViewById(R.id.search_button);  
mSearchEditFrame = findViewById(R.id.search_edit_frame);  
mSearchPlate = findViewById(R.id.search_plate);  
mSubmitArea = findViewById(R.id.submit_area);  
mSubmitButton = findViewById(R.id.search_go_btn);  
mCloseButton = (ImageView) findViewById(R.id.search_close_btn);  
mVoiceButton = findViewById(R.id.search_voice_btn);  
mSearchHintIcon = (ImageView) findViewById(R.id.search_mag_icon); 

Reference : link

Upvotes: 6

Stanislav Parkhomenko
Stanislav Parkhomenko

Reputation: 489

Yes, you can do it, one of ways to do it:

public static void customizeSearchView(SearchView searchView) {

        int searchTextViewId = searchView.getContext().getResources()
                .getIdentifier("android:id/search_src_text", null, null);
        AutoCompleteTextView searchTextView
                = (AutoCompleteTextView) searchView.findViewById(searchTextViewId);
        searchTextView.setTextSize(14);
        searchTextView.setTextColor(Color.WHITE);
}

Upvotes: 1

Krishna Kachhela
Krishna Kachhela

Reputation: 793

This may work..

SearchView searchView = (SearchView) findViewById(R.id.search);
EditText searchEditText = (EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));

Upvotes: 14

Related Questions