erik
erik

Reputation: 4948

how to change highlight color on editText field

Below is a screen shot of a custom edit text field i have in my android app. While you are type a given word, the text for the word you are currently typing highlights in grey and shows the text as black, until you hit the space bar at which time the text turns white as expected. Is there a way to change the color of the highlight and the text that is highlighted?

enter image description here

my edit text xml looks like this

 <EditText
                    android:id="@+id/searchField"
                    android:layout_width="160dp"
                    android:layout_height="44dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="60dp"
                    android:background="@null"
                    android:cursorVisible="true"
                    android:ems="10"
                    android:textColor="@color/white"
                    android:textColorHighlight ="#ff0000"
                    android:textCursorDrawable="@null" >
                </EditText>

the whole layout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!--
         As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions.
    -->

    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff" >



        <fragment
            android:id="@+id/map"
            android:name="com.sapientnitro.inhouse.drop.components.DRPCustomMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />



        <ImageButton
            android:id="@+id/btn_center_local"
            android:layout_width="44dp"
            android:layout_height="44dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="50dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/btn_center_on_local_up" />

        <RelativeLayout
            android:id="@+id/top_bar"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:background="#ddffffff" >

            <ImageButton
                android:id="@+id/btn_menu"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="12dp"
                android:layout_toLeftOf="@+id/btn_search"
                android:background="@drawable/btn_menu_up" />


            <ImageButton
                android:id="@+id/btn_create"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="12dp"
                android:layout_toRightOf="@+id/btn_search"
                android:background="@drawable/btn_create_up" />

            <ImageButton
                android:id="@+id/btn_search"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:background="@drawable/btn_search_up" />
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ddffffff"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/search_bar"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:background="#dd00cccb" >

                <ImageView
                    android:id="@+id/searchBox"
                    android:layout_width="238dp"
                    android:layout_height="44dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="20dp"
                    android:src="@drawable/search_field" />

                <ImageView
                    android:id="@+id/clear"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="220dp"
                    android:src="@drawable/btn_clear_field" />

                <EditText
                    android:id="@+id/searchField"
                    android:layout_width="160dp"
                    android:layout_height="44dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="60dp"
                    android:background="@null"
                    android:cursorVisible="true"
                    android:ems="10"
                    android:textColor="@color/white"
                    android:textColorHighlight ="@color/white"
                    android:textCursorDrawable="@null" >
                </EditText>

                <TextView
                    android:id="@+id/cancelBTN"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="16dp"
                    android:textColor="@color/white"
                    android:textSize="22dp"
                    android:text="@string/cancel" />

            </RelativeLayout>

            <ScrollView
                android:id="@+id/search_results_container"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:paddingTop="2dp"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/search_results_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <RelativeLayout
                        android:id="@+id/header_artists"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#dd00cccb" />

                    <HorizontalScrollView
                        android:id="@+id/results_artists"
                        android:layout_width="match_parent"
                        android:layout_height="150dp" />

                    <RelativeLayout
                        android:id="@+id/header_followers"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#dd00cccb" />

                    <HorizontalScrollView
                        android:id="@+id/results_followers"
                        android:layout_width="match_parent"
                        android:layout_height="150dp" />

                    <RelativeLayout
                        android:id="@+id/header_places"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:background="#dd00cccb" />

                    <HorizontalScrollView
                        android:id="@+id/results_places"
                        android:layout_width="match_parent"
                        android:layout_height="150dp" />
                </LinearLayout>
            </ScrollView>
        </LinearLayout>
    </RelativeLayout>


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="255dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#dd00cccb"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:listSelector="@drawable/selector_transparent" />

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="255dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#dd00cccb"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:listSelector="@drawable/selector_transparent" />

</android.support.v4.widget.DrawerLayout>

Upvotes: 29

Views: 47828

Answers (9)

Mehreen Abia
Mehreen Abia

Reputation: 1

well there's a simpler way to do it...I was here to look for an answer, but im answering my own answer.

If you go to the attractions place go all the way down to the letter T and there should be an TextColorHighlight but if you don't see it the there should be ones that aren't fully shown so go hover over them and then the full word will show the showed be little color icons beside them you should press them and select your color. And then go to your code and enter TextColorHighlight Press Tab and then enter the name of the color, then run it!

Hope that this worked out for you, sorry I couldn't add any pictures of how'd it would of looked. Oh and maybe you're wondering why didn't it automatically do that then have me do it? Now im wondering that too, ill try to see what i can do

Upvotes: 0

Kirk
Kirk

Reputation: 5077

Simple, just use TextHightLight Property in XML Mode

android:textColorHighlight="#ff0000"

or add this in your theme

<item name="android:textColorHighlight">"#ff0000"</item>

[Image attached here is because for a comment below]

and when i do, this is how it looks

enter image description here

Upvotes: 21

Cristan
Cristan

Reputation: 14085

In code:

et.setHighlightColor(ContextCompat.getColor(getContext(), R.color.highlight));

Upvotes: 4

Abhijeet
Abhijeet

Reputation: 392

And if you have to change the Marker handle then you should change

<item name="colorAccent">@color/edittext_handle_selection</item>
<item name="android:textColorHighlight">@color/edittext_selection</item>

Upvotes: 7

Mridul Shrivastava
Mridul Shrivastava

Reputation: 109

In material design you will get the default ascent color in style so the all text selection and other theme would be according to that color. You can change this ascent color in following way -

Go to the style.xml and change the following color -

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorAccent">@color/colorAccent</item> // Replace this color with your own choice
    </style>

Upvotes: 1

l-l
l-l

Reputation: 3854

I was able to change the highlight of my edit text by adding this line to my themes.xml file:

<item name="android:textColorHighlight">@color/m_highlight_blue</item>

Upvotes: 32

Pankaj Arora
Pankaj Arora

Reputation: 10274

use this code,might be its help you

android:textColorHighlight="your color code"

Upvotes: 0

Dreagen
Dreagen

Reputation: 1743

You should use android:textColorHighlight in the EditText element in you layout xml file

Upvotes: 2

rachit
rachit

Reputation: 1996

you should use a selector to change text colors`:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed State -->
<item android:state_pressed="true"
    android:color="#FF0000" />
<!-- Focused State -->
<item android:state_focused="true"
    android:color="#FF0000" />
<!-- Default State -->
<item android:color="#FFFFFF" />

And then set your textColor property to @drawable/selector_name

Upvotes: 0

Related Questions