0

I have a problem that I'm sure it's not to complicated to resolve but no matter what I do it just won't work !

Here's my problem : I have this layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/backgroundshape"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_timer"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textColor="#f44242"
    android:textSize="95dp"
    android:textStyle="bold"
    />

<ImageView
    android:id="@+id/image_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="25dp"
    android:layout_centerVertical="true"/>

<ImageView
    android:id="@+id/image_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="25dp"
    android:layout_centerVertical="true"/>

</RelativeLayout>

The point is to click on the last ImageView that take all the screen to begin to instantiate the other Views. Here's how I do it :

public class GameScreen extends Activity {

ImageView imgEmpty, img1, img2;
TextView tvTimer;

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

    img1 = (ImageView)findViewById(R.id.image_1);

    img2 = (ImageView)findViewById(R.id.image_2);

    imgEmpty = (ImageView)findViewById(R.id.image_empty);
    imgEmpty.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            img1.setBackgroundResource(R.drawable.feuille);
            img2.setBackgroundResource(R.drawable.feuille);
            tvTimer.setTextSize(95);
            for(int i=0; i<3; i++) {
                imgEmpty.setVisibility(View.GONE);
                try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); Log.d("Ratatataaa","Y passe pas !!!!"); }
                tvTimer.setText(""+i);
                view.invalidate();
            }
        }
    });

    tvTimer = (TextView)findViewById(R.id.text_timer);
    tvTimer.setTextSize(50);
    tvTimer.setText("Ready ?");
}
}

No matter what I do (invalidate(),...), the screen only refresh at the end of the onClick(View view)

please help

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Kolopox
  • 276
  • 1
  • 4
  • 17
  • 2
    your expected behavior is not clear at all , explain what you want and what is happening – Pavneet_Singh Sep 28 '17 at 15:55
  • Also, why are you trying to `setVisibility()` and `invalidate()` the same view thrice inisde the loop. – zeekhuge Sep 28 '17 at 16:51
  • @Pavneet_Singh My expected behavior is that when you click on "imgEmpty", the 2 others ImageView must display the image I want and the TextView must print 0 then 1 then 2. What's happening is that the image and the text are displayed but at the end of the fonction, I have my 2 images and "2" and not my 2 images and 0 then 1 then 2 – Kolopox Sep 29 '17 at 12:18
  • @ZeekHuge If you don't understand well what I want read the comment above. And I have setVisibility to make my first image disaper and I tryed invalidate to refresh my screen – Kolopox Sep 29 '17 at 12:20
  • look into [this](https://stackoverflow.com/questions/1520887/how-to-pause-sleep-thread-or-process-in-android) and [this](https://stackoverflow.com/questions/8369718/sleep-function-in-android-program) – Pavneet_Singh Sep 29 '17 at 13:35

2 Answers2

0

There are few bad implimentations in there. For example using sleep() for the main thread, and initializing the TextView at wrong point.

Try this instead :

....
img1 = (ImageView)findViewById(R.id.image_1);
img2 = (ImageView)findViewById(R.id.image_2); 

tvTimer = (TextView)findViewById(R.id.text_timer);
tvTimer.setTextSize(50);
tvTimer.setText("Ready ?");

imgEmpty = (ImageView)findViewById(R.id.image_empty);
imgEmpty.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        img1.setBackgroundResource(R.drawable.feuille);
        img2.setBackgroundResource(R.drawable.feuille);
        tvTimer.setTextSize(95);
        imgEmpty.setVisibility(View.GONE);
        view.invalidate();
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick (long millisUntilFinished) {
                tvTimer.setText("" + ((int)(3000 - millisUntilFinisehd)/1000));
            }

            @Override
            public void onFinish () {}
        };
    }
});
....
zeekhuge
  • 1,594
  • 1
  • 13
  • 23
0

I solved this problem by adding an Handler and a Runnable.

See this : How to set a timer in android

Kolopox
  • 276
  • 1
  • 4
  • 17