Java – Changing font color runtime Android


I am using EditText for getting userInput but the problem I am
facing is that I want to change the font color at runtime but in the
same Edittext as I am able to change the font type but as I change the
font color it changes for the whole editext but I need to change the
color for specfic text only .

And one more issue issue is that when making an edittext with the
height more than the "fillparent" property I am getting the cursor
postion in middle but I want it to be at the top always i.e. at the
start ..

Is there any other widget available which will provide me the solution
for both the problems?

Best Solution

Alignment of text within an EditText widget can be controlled via gravity:


To attach markup objects to the EditText's content you can use the Editable interface:

EditText editText = (EditText) findViewById(;
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("one red word");
builder.setSpan(new ForegroundColorSpan(Color.RED), 4, 7, Spanned.SPAN_COMPOSING);
editText.setText(builder, BufferType.EDITABLE);