Haitham Haitham
Haitham Haitham

Reputation: 157

cannot change background color of custom editText in android

i quoted custom editText because i want to draw lines in edittext so i have this class

public class LinedEditText extends android.support.v7.widget.AppCompatEditText {
private Rect mRect;
private Paint mPaint;
private int COLOR;

public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

    mRect = new Rect();
    mPaint = new Paint();        
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    SharedPreferences sh= PreferenceManager.getDefaultSharedPreferences(context);
    String co=sh.getString("line_color", String.valueOf(R.color.blue_line));
    mPaint.setColor(getResources().getColor(Integer.parseInt(co))); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();
    int count = height / line_height;
    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }
    super.onDraw(canvas);
}

}

and i used this class in xml and in another class (MainActivity) without any problem but when i try to change color pro grammatically is not change even if i change that in xml ,is not change

xml :

 <my.app.haythamayyash.note.LinedEditText
        android:layout_width="match_parent"
        android:inputType="textMultiLine"
        android:scrollbars="vertical"
        android:gravity="top|start"
        android:ems="10"
        android:id="@+id/detail2"
        android:layout_height="463dp"
        android:backgroundTint="@android:color/transparent"
        android:textColor="@android:color/black"
        android:minHeight="510dp"/>

i try that by added android:background="@color/gray" but no thing changed , and i try to change that in java by

ed.setBackgroundColor(Color.parseColor("#ffffff"));

and ed.setBackgroundResource(R.color.gray); but nothing change i guess the issue in LinedEditText class because i changed other editText (not LinedEditText) and its work .. how to change background color of this editText (programmatically) ??

Upvotes: 1

Views: 644

Answers (2)

Ankit Patidar
Ankit Patidar

Reputation: 2781

Use this code in LinedEditText in onDraw() method for changing its color:

 canvas.drawColor(Color.BLACK);

or by resources layout file you can do this like:

app:backgroundTint="@android:color/holo_red_dark"

Upvotes: 1

Mayank Garg
Mayank Garg

Reputation: 1304

Use this code that would help u , it help me

        app:backgroundTint="@color/gray"

Upvotes: 0

Related Questions