This is my Code to the java class wherein I draw three towers onto which I place certain amount of rings. In the onTouchEvent function, I notice activity and increase the number of moves accordingly, which is, in turn being displayed on the screen. But it doesn't update until I go to the previous activity and come back.
Number.java
package com.syncnlink.myapplication;
public class Number {
public static int tower1[] = new int[10];
public static int tower2[] = new int[10];
public static int tower3[] = new int[10];
public static void initialize() {
for (int i = 0; i < 10; i++)
{
tower1[i] = i+1;
tower2[i] = -1;
tower3[i] = -1;
}
}
public static int numelements(int arr[]) {
int count = 0;
for (int i = 0; i < 10; i++)
{
if (arr[i] > 0)
count++;
else
continue;
}
return count;
}
public static void setit(int arr[], int a)
{
arr[a]=-1;
}
public static void setit1(int a)
{
tower1[a]=-1;
}
public static boolean isEmpty(int arr[])
{
for(int i=0;i<5;i++)
if(arr[i]>-1)
{
return false;
}
return true;
}
}
NewGame Activity
package com.syncnlink.myapplication;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class NewGame extends Activity {
public CustomRectangle cr;
static float x,y,width, height;
public static int moves =0;
private static Bitmap background;
static Canvas canvas;
public int ff=0,initialized=0;
public static int t1,t2,t3;
@Override
protected void onCreate(Bundle savedInstanceState) {
background = BitmapFactory.decodeResource(getResources(),R.drawable.background);
super.onCreate(savedInstanceState);
CustomRectangle cr = new CustomRectangle(this);
setContentView(cr);
Number.initialize();
t1 = Number.numelements(Number.tower1);
t2 = Number.numelements(Number.tower2);
t3 = Number.numelements(Number.tower3);
}
public static class CustomRectangle extends View {
CustomRectangle(Context c) {
super(c);
invalidate();
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
NewGame n = new NewGame();
if(n.initialized == 0)
{
Number.initialize();
}
Paint p[]= new Paint[11];
for(int i=0;i<11;i++)
p[i] = new Paint();
p[0].setColor(Color.YELLOW);
p[1].setColor(Color.BLUE);
p[2].setColor(Color.CYAN);
p[3].setColor(Color.RED);
p[4].setColor(Color.GRAY);
p[5].setColor(Color.MAGENTA);
p[6].setColor(Color.BLUE);
p[7].setColor(Color.CYAN);
p[8].setColor(Color.RED);
p[9].setColor(Color.GRAY);
p[10].setColor(Color.MAGENTA);
canvas.drawBitmap(background,0,0,null);
p[0].setStrokeWidth(24);
height = getHeight();
width = getWidth();
float w = width/1794;
float h = height/1008;
canvas.drawLine(100*(width/1794),1000*(height/1008),500*(width/1794),1000*(height/1008),p[0]);
canvas.drawLine(300*(width/1794),1000*(height/1008),300*(width/1794),400,p[0]);
canvas.drawLine((100+600)*(width/1794),1000*(height/1008),(500+600)*(width/1794),1000*(height/1008),p[0]);
canvas.drawLine((300+600)*(width/1794),1000*(height/1008),(300+600)*(width/1794),400*(height/1008),p[0]);
canvas.drawLine((100+1200)*(width/1794),1000*(height/1008),(500+1200)*(width/1794),1000*(height/1008),p[0]);
canvas.drawLine((300+1200)*(width/1794),1000*(height/1008),(300+1200)*(width/1794),400*(height/1008),p[0]);
int j=0;
for (int i = 0; i < 10; i++) {
if(Number.tower1[i]<0)
continue;
else {
canvas.drawRect((150 + (i * 14))*w, (942 - (j * 44))*h, (450 - (i * 14))*w, (987 - (j * 44))*h, p[i + 1]);
j++;
}
}
j=0;
for (int i = 0; i < 10; i++) {
if(Number.tower2[i]<0)
continue;
else {
canvas.drawRect((150 + (i * 14) + 600)*w, (942 - (i * 44))*h, (450 + 600 - (i * 14))*w, (987 - (i * 44))*h, p[i + 1]);
j++;
}
}
j=0;
for (int i = 0; i < 10; i++) {
if(Number.tower3[i]<0)
continue;
else {
canvas.drawRect((150 + 1200 + (i * 14))*w, (942 - (i * 44))*h, (450 + 1200 - (i * 14))*w, (987 - (i * 44))*h, p[i + 1]);
j++;
}
}
p[0].setTextSize(48);
canvas.drawText(" No. of Moves " + moves ,100,40,p[0]);
}
}
@Override
public boolean onTouchEvent( MotionEvent event1) {
try {
switch (event1.getAction()) {
case MotionEvent.ACTION_DOWN: {
x=event1.getX();
y=event1.getY();
if(x<width/3)
{
if(!Number.isEmpty(Number.tower1)) {
Toast.makeText(this, "Pop Tower1 " + t1--, Toast.LENGTH_SHORT).show();
ff=0;
moves++;
Number.setit1(t1);
cr.invalidate();
}
else {
Toast.makeText(this, "Empty! Nothing to Pop", Toast.LENGTH_SHORT).show();
ff = 1;
}
}
else
if(x<width*2/3) {
if (!Number.isEmpty(Number.tower2)) {
Toast.makeText(this, "Pop Tower2 " + t2--, Toast.LENGTH_SHORT).show();
Number.setit(Number.tower2, t2);
ff=0;
moves++;
cr.invalidate();
} else {
ff = 1;
Toast.makeText(this, "Empty! Nothing to Pop", Toast.LENGTH_SHORT).show();
}
}
else
{
if(!Number.isEmpty(Number.tower3)) {
Number.setit(Number.tower3, t3);
Toast.makeText(this, "Pop Tower3 " + t3--, Toast.LENGTH_SHORT).show();
moves++;
cr.invalidate();
ff=0;
}
else {
Toast.makeText(this, "Empty! Nothing to Pop", Toast.LENGTH_SHORT).show();
ff = 1;
}
}
break;
}
case MotionEvent.ACTION_UP: {
x=event1.getX();
y=event1.getY();
if(x<width/3)
{
if(ff==0)
{
Number.setit2(Number.tower1,++t1);
Toast.makeText(this, "Push Tower1 " + t1, Toast.LENGTH_SHORT).show();
}
}
else
if(x<width*2/3)
{
if(ff==0)
{
Number.setit2(Number.tower2,++t2);
Toast.makeText(this, "Push Tower2 " + t2, Toast.LENGTH_SHORT).show();
}
}
else
{
if(ff==0) {
Number.setit2(Number.tower3, ++t3);
Toast.makeText(this, "Push Tower3 " + t3, Toast.LENGTH_SHORT).show();
}
}
break;
}
}
}
catch(Exception e)
{
}
return true;
}
}
So, I can see all the Toasts on the screen and also if I put up a toast in onDraw(), I can see that being called several times, but it isn't updating the information or not redrawing the new canvas on top of the previous one. Any help is welcome. Been stuck at it for a few days now