Hot questions for Using Android EditText in colors

Question:

I know how to set shadow for an editText in android:

editText.setShadowLayer(float radius, float dx, float dy, int color);

But my question is : How to get a Text's shadow color programatically in android?

*

Of course I have to add that my minimum API level is 15.

*

Thanks to everybody who can answer this question


Answer:

You can use the following method for API level >= 16:

int shadowColor = editText.getShadowColor();

For API level < 16, there seem to be no direct way to get the shadow color of an EditText.

What i would do in this case:

Extend EditText, override setShadowLayer() and write your own method to get the shadow color:

public class CustomEditText extends EditText {
    int shadowColor = 0;

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setShadowLayer(float radius, float dx, float dy, int color) {
        shadowColor = color;
        super.setShadowLayer(radius, dx, dy, color);
    }

    public int getShadowLayerColor() {
        return shadowColor;
    }
}

Then use CustomEditText instead of EditText in your code/layout and call getShadowLayerColor() to get the color of the shadow.

Question:

I have been using the code below that I got from another question to split into two lines to edit the color programmatically.

Changing tint color of Android EditText programmatically

((EditText) row1.getVirtualChildAt(i))

The below code is what I'm using after the above line to change the color.

.getBackground().mutate().setColorFilter(getResources().getColor(R.color.Green), PorterDuff.Mode.SRC_ATOP);

Right now it sets the underline color so its always green whether or not the EditText box is being used.

How can I set it so it goes back to the default color after I click away from the EditText box. I would also be fine with specifying another color as the default like a light gray color.


Answer:

You can register your edit text with OnFocusChangeListener.java and on focus change you can change the color.

void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// color while typing
}else{
// color when clicked away
}

}

Question:

I'm creating a text editor, and i wont to able users to select some words in EditText and change color of the selected words, How can I do that? Or how can i understand which words, user selected? Thanks


Answer:

Use getSelectionStart and getSelectionEnd to get the active selection.

Use SpannableString and SpannableString.setSpan to colorize the selection using a ForegroundColorSpan.