-1

All I want to do is to highlight text inside an EditText like this

Is there any good way for it?

Neriko
  • 50
  • 4
  • ``android:selectAllOnFocus="true"`` then ``yourEditText.setBackgroundColor(yourcolor);`` – Donald Wu Sep 19 '17 at 14:13
  • i think [here](https://stackoverflow.com/questions/25357295/android-select-and-highlight-text-in-edittext) and [here](https://stackoverflow.com/questions/30476534/highlighting-a-focused-edit-text) can help you – White Druid Sep 19 '17 at 14:16

2 Answers2

1

i think here and here can help you

you need selector and aandroid:selectAllOnFocus="true"

    <EditText 
android:id="@+id/tvOrdinanceTitle" 
android:layout_width="wrap_content" 
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background="@color/etxt_color" > 
</EditText>

and :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:color="#000000" /> <!-- pressed -->
 <item android:state_focused="true"
   android:color="#000000" /> <!-- focused -->
  <item android:color="#FFFFFF" /> <!-- default -->
 </selector>
White Druid
  • 295
  • 1
  • 12
0

You need to call the "selectAllOnFocus" method either in XML or programatically.

To do so in the XML, use the following example:

<EditText
                    android:id="@+id/group_name"
                    android:text="Group Name"
                    android:selectAllOnFocus="true"/>

You could also do it programmatically, reference the edit text in your code and call the selectAllOnFocus() method.

EditText edittext = ...;
edittext.setText("Group Name ");
edittext.selectAllOnFocus();       //You can also use "edittext.selectAll();"

Make sure you are using "android:text = " rather than "android:hint = ". Hint cannot use selectAllOnFocus and is not supposed to.

android:text will automatically disappear if you start typing while focused, but will stay if you click it again.

Old Age
  • 1
  • 1