0

firstble i apologies for my bad english. I'm fairly new to programming and I'm having trouble with making this compound view work. so i create it's layout with two views

  1. EditText: for taking user input
  2. ImageView: for setting EditText enabled

    <EditText
        android:inputType="text"
        android:layout_width="200dp"
        android:ems="10"
        android:layout_height="50dp"
        android:id="@+id/edittext_field"/>
    
    <ImageView
        android:layout_height="50dp"
        android:layout_width="30dp"
        android:src="@drawable/ic_edit"
        android:id="@+id/imageview_edit"
        android:background="#7388A4"/>
    

then i created EditableView class and setup all variable and attributes...

public class EditableView extends LinearLayout
{

    private boolean isEditable=false;

    private boolean isValide=true;

    private int color=0XFF000000;

    private String text="this field is empty";

    private int textLength;

    public EditableView(Context context){

        super(context);
    }

    public EditableView(Context context,AttributeSet attr){

        super(context,attr);

        TypedArray typedArray=context.obtainStyledAttributes(attr,R.styleable.EditableView);

    try{
        isEditable=typedArray.getBoolean(R.styleable.EditableView_setEditable,false);
        isValide=typedArray.getBoolean(R.styleable.EditableView_isValide,true);
        color=typedArray.getColor(R.styleable.EditableView_color,0);
        text=typedArray.getString(R.styleable.EditableView_text);
        }

    finally{

        typedArray.recycle();

        }

        initView(context,attr);
    }

    public void setEditable(boolean isEditable)
    {
        this.isEditable = isEditable;
    }

    public boolean isEditable()
    {
        return isEditable;
    }

    public void setIsValide(boolean isValide)
    {
        this.isValide = isValide;
    }

    public boolean isValide()
    {
        return isValide;
    }

    public void setColor(int color)
    {
        this.color = color;
    }

    public int getColor()
    {
        return color;
    }

    public void setText(String text)
    {
        this.text = text;
        invalidate();
    }

    public String getText()
    {
        return text;
    }


    public void initView(Context context,AttributeSet attr){

        LayoutInflater.from(getContext()).inflate(R.layout.custom_edit_view,this,true);

    }

    @Override
    protected void onFinishInflate()
    {
        // TODO: Implement this method
        super.onFinishInflate();

        final EditText editableText=(EditText)findViewById(R.id.edittext_field);
        ImageView editImage=(ImageView)findViewById(R.id.imageview_edit);

        editableText.setText(text);

        editableText.setTextColor(color);

        editableText.setEnabled(isEditable);


        editImage.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View p1)
                {
                    // TODO: Implement this method

                textLength=editableText.getText().length();

                    if(isEditable){

                        if(textLength<8){

                            isValide=false;
                            color=Color.RED;

                        }else{
                            isValide=true;
                            color=Color.BLACK;
                            isEditable=false;
                            }
                    }else{
                            isEditable=true;
                            }
                    setEditable(isEditable);
                    setColor(color);
                    editableText.postInvalidate();
                    }

                });
    }

finally i placed my view in Main Activity layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center_horizontal"
    android:id="@+id/home"
    android:orientation="vertical"
    android:background="#282828"
    android:padding="10dp">
    <com.tomouh.com.didin.recorder.customviews.EditableView
        android:id="@+id/editableView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center|center_vertical"
        android:padding="4dp"
        android:layout_marginBottom="10dp"
        app:text="some text"
        app:color="#ee9900"
        android:background="#FFFFFF">
    </com.com.didin.recorder.customviews.EditableView>

</LinearLayout>

for some reason when i press ImageView the text color of EditText stay unchanged even after using invalidate() and postInvalidate();

CiDski
  • 1
  • 1
  • 2

1 Answers1

0

Add editableText.setTextColor(color); in setColor(int color)

public void setColor(int color)
{
    this.color = color;
    editableText.setTextColor(color);
}
Tiko
  • 851
  • 8
  • 15
  • thankyou for your respond ,but still don't know why invalidate() does not work . – CiDski Mar 29 '20 at 02:47
  • invalidate() calls onDraw(android.graphics.Canvas). But you are setting editableText textColor in onFinishInflate(), which is called only once when view and all of its children has been inflated from XML. If you override the onDraw(Canvas) method and put editableText.setTextColor(color); there it can work. – Tiko Mar 29 '20 at 02:51
  • I have seen so many tutorials putting invalidate() inside setters then use them externally ,i tried that but no luck. – CiDski Mar 29 '20 at 03:04
  • Once again, invalidate() calls onDraw(Canvas), so changing only your member "int color" cannot affect your views. You need to set it to your editableText, for example if you want to achieve this with invalidate(), you need to do it in onDraw(Canvas). If I helped you please vote for my answer or mark it as answered, thanks – Tiko Mar 29 '20 at 03:14
  • thankyou ,you were helpful.you deserve my vote ,its just i dont have enough reputation. – CiDski Mar 29 '20 at 03:28