2

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?

saeid rasouli
  • 157
  • 1
  • 1
  • 8

2 Answers2

0

For editText try

pointEditor.setHintTextColor(getResources().getColor(color));

For cursor color check out this answer

Junaid Khalid
  • 811
  • 2
  • 11
  • 26
0

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();
saeid rasouli
  • 157
  • 1
  • 1
  • 8