2

I have a string variable 'user' derived from an edittext. I use this variable in a hint with some text. It works well but I need the color of that varibale 'user' inside the hint to be displayed in red. I tried previous answers but they are not helpful as the colour remains unchanged.

final EditText modifyTxt1 = (EditText) findViewById(R.id.editText4);
String user = modifyTxt1.getText().toString().trim();

newTxt.setHint("You have just now created a title : " +user+"");
user3144078
  • 145
  • 2
  • 9

5 Answers5

2

You can use like this

1) from java code

newTxt.setHintTextColor(getResources().getColor(R.color.white));

OR

2) from layout xml

android:textColorHint="#FFFFFF"
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
2

in your xml file where you want to add hint add the following line

 android:textColorHint=""

and fill it with the colour you want to add

Rakshit Nawani
  • 2,604
  • 14
  • 27
2

Do it this way

String hintText="You have just now created a title : <font color='red'>" +user+"</font>";

Use the Html.fromHtml method to convert this back into a styled character sequence.

newTxt.setHint(Html.fromHtml(hintText ));

Note that Html.fromHtml() parses your text as html.

MChaker
  • 2,610
  • 2
  • 22
  • 38
1
newTxt.setHint(Html.fromHtml("<font color='#FF0000'>Hello</font> "));
Meysam
  • 694
  • 6
  • 20
1

Try this:

String myText = "You have just now created a title : ";
    String user = "abc";
    Spannable wordtoSpan = new SpannableString(user);        
    String total = myText+user;
    int length = total.length();
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 36, length-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    newTxt.setText(textViewProgressStatus.toString());
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46