0

When I was working on a new view yesterday I had 7*4 Edit texts , so I had to add lots of code to cover them when the user tries to change the color , for example Red

So I was thinking to do something like this:

if(item.getItemId() == R.id.menu_table_colors_Red){

            EditText Holder = (EditText)mView.findViewById(View_ID);
            Holder.setBackgroundResource(R.color.RED);
            return true;
        }

The View_ID is from onCreateContextMenu() which tells me which view was clicked on and mView is my fragment view , it works fine and all until I needed to save the values of the colors so the user can return and find the colors that he used:

I found two ways after searching either with a way in 11 api but I need a way to work on 8 api and higher.

the second was to implement a custom Edittext class , I copied the class from the solution to test it and here it is:

public class NewEditText extends EditText {
        private int color;
        public NewEditText(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        public NewEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }

        public NewEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
        }   


        @Override
        public void setBackgroundColor(int color) {
            // TODO Auto-generated method stub
            this.color=color;
            super.setBackgroundColor(color);
        }

        public int getBackgroundColor() {

            return color;
        }
        }

but I'm not sure how to use this ? when I do this it crashes because of " class cast exception "

NewEditText holder = (NewEditText)mView.findViewById(View_ID);

how am I supposed to use this ? sorry but I've never used a custom class and I couldn't find a solution.


Edit 1 the xml , sorry it's kinda big so I decided to post one of the days since there are 7 days and they're the same:

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:id="@+id/table_view_mondaylayout"
        android:layout_margin="0.3dp"
        >

        <TextView 
            android:layout_width="32dp"
            android:layout_height="match_parent"
            android:text="Mon"
            android:layout_weight="0"
            android:gravity="center"
            android:background="@drawable/custom_table"
            android:layout_margin="0.3dp"
            />

        <EditText 
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="125 \n    down"
            android:textSize="14sp"
            android:layout_weight="1"
            android:background="@drawable/custom_table"
            android:id="@+id/table_monday1"
            android:layout_margin="0.3dp"
            />

        <EditText 
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="125 \n    down"
            android:textSize="14sp"
            android:layout_weight="1"
            android:background="@drawable/custom_table"
            android:id="@+id/table_monday2"
            android:layout_margin="0.3dp"
            />

        <EditText 
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="125 \n    down"
            android:textSize="14sp"
            android:layout_weight="1"
            android:background="@drawable/custom_table"
            android:id="@+id/table_monday3"
            android:layout_margin="0.3dp"
            />

        <EditText 
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="125 \n    down"
            android:textSize="14sp"
            android:layout_weight="1"
            android:background="@drawable/custom_table"
            android:id="@+id/table_monday4"
            android:layout_margin="0.3dp"
            />

    </LinearLayout>

0 Answers0