Anon
Anon

Reputation: 147

Enable EditText underline

By Default EditText has an underline. I want to start my program with the Edittext disabled and when someone clicks on an icon, the EditText is enabled.

Here is a picture showing the "to..." EditText not having an underline where the others do and the one with focus has it in the Accent color.

enter image description here

The code I have tried:

foodIconButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            foodCurrentEditText.setEnabled(true);
            foodDesiredEditText.setEnabled(true);
            foodDesiredEditText.setFocusable(true);
            foodDesiredEditText.setBackgroundColor(Color.TRANSPARENT);
            //foodDesiredEditText.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#03DAC5")));
            //MyDrawableCompat.setColorFilter(foodDesiredEditText.getBackground(), ContextCompat.getColor(MainActivity.this, R.color.colorAccent));

            foodDesiredEditText.getBackground().setColorFilter(ContextCompat.getColor(MainActivity.this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        }
    });

The bottom 3 lines of code are the different methods I have tried from suggestions in other questions on here.

Upvotes: 1

Views: 74

Answers (1)

aminography
aminography

Reputation: 22832

If you want to hide and show the underline of the EditText, you can do it like the following:

To hide the line:

ColorStateList colorStateList = ColorStateList.valueOf(Color.TRANSPARENT);
ViewCompat.setBackgroundTintList(foodDesiredEditText, colorStateList);

To show the line:

ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(applicationContext, R.color.colorAccent));
ViewCompat.setBackgroundTintList(foodDesiredEditText, colorStateList);

A better solution if you are using androidx is:

To hide the line:

foodDesiredEditText.setBackgroundResource(0);

To show the line:

foodDesiredEditText.setBackgroundResource(androidx.appcompat.R.drawable.abc_edit_text_material);

Upvotes: 2

Related Questions