Change background color of edittext in android

前端 未结 10 1611
一个人的身影
一个人的身影 2020-12-25 12:00

If I change the background color of my EditText using the below code, it looks like the box is shrunken and it doesn\'t maintain the ICS theme of a blue bottom

相关标签:
10条回答
  • 2020-12-25 12:10

    Here the best way

    First : make new xml file in res/drawable name it rounded_edit_text then paste this:

    <?xml version="1.0" encoding="utf-8"?>
    <shape  xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle" android:padding="10dp">
        <solid android:color="#F9966B" />
        <corners
            android:bottomRightRadius="15dp"
            android:bottomLeftRadius="15dp"
            android:topLeftRadius="15dp"
            android:topRightRadius="15dp" />
    </shape>
    

    Second: in res/layout copy and past following code (code of EditText)

    <EditText
        android:id="@+id/txtdoctor"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rounded_edit_text"
        android:ems="10" >
        <requestFocus />
    </EditText>
    
    0 讨论(0)
  • 2020-12-25 12:15

    one line of lazy code:

    mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2020-12-25 12:16

    I worked out a working solution to this problem after 2 days of struggle, below solution is perfect for them who want to change few edit text only, change/toggle color through java code, and want to overcome the problems of different behavior on OS versions due to use setColorFilter() method.

        import android.content.Context;
    import android.graphics.PorterDuff;
    import android.graphics.drawable.Drawable;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.widget.AppCompatDrawableManager;
    import android.support.v7.widget.AppCompatEditText;
    import android.util.AttributeSet;
    import com.newco.cooltv.R;
    
    public class RqubeErrorEditText extends AppCompatEditText {
    
      private int errorUnderlineColor;
      private boolean isErrorStateEnabled;
      private boolean mHasReconstructedEditTextBackground;
    
      public RqubeErrorEditText(Context context) {
        super(context);
        initColors();
      }
    
      public RqubeErrorEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initColors();
      }
    
      public RqubeErrorEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initColors();
      }
    
      private void initColors() {
        errorUnderlineColor = R.color.et_error_color_rule;
    
      }
    
      public void setErrorColor() {
        ensureBackgroundDrawableStateWorkaround();
        getBackground().setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(
            ContextCompat.getColor(getContext(), errorUnderlineColor), PorterDuff.Mode.SRC_IN));
      }
    
      private void ensureBackgroundDrawableStateWorkaround() {
        final Drawable bg = getBackground();
        if (bg == null) {
          return;
        }
        if (!mHasReconstructedEditTextBackground) {
          // This is gross. There is an issue in the platform which affects container Drawables
          // where the first drawable retrieved from resources will propogate any changes
          // (like color filter) to all instances from the cache. We'll try to workaround it...
          final Drawable newBg = bg.getConstantState().newDrawable();
          //if (bg instanceof DrawableContainer) {
          //  // If we have a Drawable container, we can try and set it's constant state via
          //  // reflection from the new Drawable
          //  mHasReconstructedEditTextBackground =
          //      DrawableUtils.setContainerConstantState(
          //          (DrawableContainer) bg, newBg.getConstantState());
          //}
          if (!mHasReconstructedEditTextBackground) {
            // If we reach here then we just need to set a brand new instance of the Drawable
            // as the background. This has the unfortunate side-effect of wiping out any
            // user set padding, but I'd hope that use of custom padding on an EditText
            // is limited.
            setBackgroundDrawable(newBg);
            mHasReconstructedEditTextBackground = true;
          }
        }
      }
    
      public boolean isErrorStateEnabled() {
        return isErrorStateEnabled;
      }
    
      public void setErrorState(boolean isErrorStateEnabled) {
        this.isErrorStateEnabled = isErrorStateEnabled;
        if (isErrorStateEnabled) {
          setErrorColor();
          invalidate();
        } else {
          getBackground().mutate().clearColorFilter();
          invalidate();
        }
      }
    }
    

    Uses in xml

    <com.rqube.ui.widget.RqubeErrorEditText
                android:id="@+id/f_signup_et_referral_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/referral_iv"
                android:layout_toRightOf="@+id/referral_iv"
                android:ems="10"
                android:hint="@string/lbl_referral_code"
                android:imeOptions="actionNext"
                android:inputType="textEmailAddress"
                android:textSize="@dimen/text_size_sp_16"
                android:theme="@style/EditTextStyle"/>
    

    Add lines in style

    <style name="EditTextStyle" parent="android:Widget.EditText">
        <item name="android:textColor">@color/txt_color_change</item>
        <item name="android:textColorHint">@color/et_default_color_text</item>
        <item name="colorControlNormal">@color/et_default_color_rule</item>
        <item name="colorControlActivated">@color/et_engagged_color_rule</item>
      </style>
    

    java code to toggle color

    myRqubeEditText.setErrorState(true);
    myRqubeEditText.setErrorState(false);
    
    0 讨论(0)
  • 2020-12-25 12:25

    You should use style instead of background color. Try searching holoeverywhere then I think this one will help you solve your problem

    Using holoeverywhere

    just change some of the 9patch resources to customize the edittext look and feel.

    0 讨论(0)
  • 2020-12-25 12:27

    This is my working solution

    View view = new View(getApplicationContext());
    view.setBackgroundResource(R.color.background);
    myEditText.setBackground(view.getBackground());
    
    0 讨论(0)
  • 2020-12-25 12:28

    The color you are using is white "#ffffff" is white so try a different one change in the values if you want until you get your need from this link Color Codes and it should go fine

    0 讨论(0)
提交回复
热议问题