'Change HintTextColor and Cursor Color of TextInputEditText Programmatically
I use Spinner and TextInputEditText in my app that I would like to change some EditText features(Cursor and HintText color) when user selects spinner item. Here is my code:
private void setSpinner(){
String [] types = {"q_a", "attention", "reminder", "explain", "important", "image", "voice"};
TypeAdapter adapter = new TypeAdapter(getActivity(), types);
spinner.setAdapter(adapter);
int [] colors = {
R.color.actual_purple, R.color.actual_pink, R.color.actual_orange,
R.color.actual_carrot, R.color.actual_green,
R.color.deep_blue_grey_flat_icon, R.color.deep_blue_grey_flat_icon
};
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextEditor(colors[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@SuppressLint("NewApi")
private void setTextEditor(int color){
Drawable unwrappedDrawable = AppCompatResources.getDrawable(getActivity(), R.drawable.important_cursor);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(getActivity(), color));
pointEditor.setTextCursorDrawable(unwrappedDrawable);
pointEditorLayout.setBoxBackgroundColor(ContextCompat.getColor(getActivity(), color));
pointEditor.setHintTextColor(ContextCompat.getColor(getActivity(), color));
}
I don't realize why that's not working. Can someone help me out?
Solution 1:[1]
For those who work with TextInputLayout, I found the answer. Just simply use:
textInputLayout.setHintTextColor(ColorStateList.valueOf(your_color));
textInputLayout.setBoxStrokeColor(your_color);
And for the cursor:
editText.setTextCursorDrawable();
Solution 2:[2]
For editText try
pointEditor.setHintTextColor(getResources().getColor(color));
For cursor color check out this answer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | saeid rasouli |
| Solution 2 | Junaid Khalid |
