Swannie
Swannie

Reputation: 135

EditText Change Canvas drawText Android

i have an activity(not the main activity) that has 2 graphics, an edittext field and a button.

all the button does is return the user back to the main activity.

i want to be able to change one of text graphic's when the EditText field changes. So whenever the user inputs text in the edittext field the graphic text changes to the same instantly. currently it only changes when i close the keypad.

heres my code

public class ThirdActivity extends AppCompatActivity {


    private DemoView demoview;
    private EditText editText;
    private String graphicText = "blank";
    private Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);


        demoview = new DemoView(this);

        Button button = (Button) findViewById(R.id.thirdActButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onButtonPress();
            }
        });

        editText = (EditText) findViewById(R.id.thirdActEditText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!editText.getText().toString().equalsIgnoreCase("")){
                    graphicText = editText.getText().toString();
                    Log.d("MyApp",graphicText);
                }
            }
        });

        LinearLayout v = (LinearLayout) findViewById(R.id.linearlayout);
        v.addView(demoview);

    }

    private class DemoView extends View{
        public DemoView(Context context){
            super(context);
        }

        @Override protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            paint = new Paint();

            // draw some text using STROKE style
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setStrokeWidth(5);
            paint.setColor(Color.BLUE);
            paint.setTextSize(100);
            canvas.drawText("Graphic", 75, 75, paint);

            // draw some text using FILL style
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.RED);
            paint.setTextSize(75);
            canvas.drawText(graphicText, 150, 210, paint);

        }
    }
}

any help would be greatly appreciated. thanks

Upvotes: 0

Views: 669

Answers (3)

Kunal Parikh
Kunal Parikh

Reputation: 463

Write your code in onTextChanged() instead of afterTextChanged() and it will do the trick. If you want to change the graphics after user inputs at least one character then use onTextChanged(). If you want to change the graphics before user gives any inputs then use beforeTextChanged().

Upvotes: 1

Swannie
Swannie

Reputation: 135

thanks to the responses above this was the solution i used.

it didnt actually make a difference whether the code was in the afterTextChanged() method or the onTextChanged(). what made the difference was the demoview.invalidate(); which refreshes the demoview

@Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (!editText.getText().toString().equalsIgnoreCase("")){
                    graphicText = editText.getText().toString();
                    Log.d("MyApp",graphicText);
                    //refreshes the demoview so it will change instantly
                    demoview.invalidate();
                }

            }

thanks for the help

Upvotes: 0

FadedCoder
FadedCoder

Reputation: 1517

Instead of adding the code in afterTextChanged, add it in onTextChanged. For example -

  @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

               if (!editText.getText().toString().equalsIgnoreCase("")){
                graphicText = editText.getText().toString();
                Log.d("MyApp",graphicText);
            }
        }

Upvotes: 0

Related Questions