mathexplorer
mathexplorer

Reputation: 365

How to refresh a View

There are a few questions related to this, but I couldn't find anything that helped me. I'm fairly new to Android Studio. Here's my situation: What i want to happen is when I click the touch the screen it stores the x and y coordinate of where I pressed in Globals.touchX and Globals.touchY and displays them to the screen. What appears to be happening is that when I pressed the screen, the touch coordinates are stored correctly, but the View that displays them doesn't refresh to show their new value. From the answers I was able to find, I tried calling invalidate() but I didn't work. I also saw some people suggesting a handler. If this is the way to go, could someone explain what they are and how they work as simply as possible. My code is below. Thanks :)

public class NonmultiplierSixGame extends AppCompatActivity implements View.OnTouchListener {

private NonmultiplierSixView nonmultiplierSixView;

@Override
public boolean onTouch(View v, MotionEvent event) {
    Globals.touchX = (int)event.getX();
    Globals.touchY = (int)event.getY();


    Log.i("GAME", Integer.toString(Globals.touchX));
    Log.i("GAME", Integer.toString(Globals.touchY));

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Globals.SetSelect(1);
            break;
        case MotionEvent.ACTION_UP:
            Globals.SetSelect(2);
            break;
    }

    return true;
}


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

    nonmultiplierSixView = (NonmultiplierSixView)findViewById(R.id.nonmultiplierSixView);
    nonmultiplierSixView.setOnTouchListener(this);

}


}

activity_nonmultiplier_six_game.xml :

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alexandermain.example_5.NonmultiplierSixGame">



<com.example.alexandermain.example_5.views.NonmultiplierSixView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nonmultiplierSixView"
    />

public class NonmultiplierSixView extends View{

public NonmultiplierSixView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

    int x = getWidth();
    int y = getHeight();

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    paint.setColor(Color.parseColor("#da4747"));
    canvas.drawText(Integer.toString(Globals.touchX), 27 * x / 30, 70 * y / 84, paint);
    }






}

Upvotes: 0

Views: 135

Answers (1)

cjnash
cjnash

Reputation: 1248

Are you sure you are calling invalidate() in the right spots? Try putting your invalidate() in each of your cases right before your break statement in your switch.

Upvotes: 1

Related Questions