Search code examples
androideclipsecountdowncountdowntimer

android - Countdown then an action


I want to make an app , when you press a button a countdown has been startted. When a countdown reach the end it looks how mutch you have clicked on the button , if its more then 10 it do nothing but if it under the 10 it has to activivate an action , like showing a pickture.

I also want to make a reset button. When you hit the reset button the countdown is set on 10 seconds en when you click the countdown button again it starts over.

StartButton --->

|click = counter running|
|click after the first one = counting the clicks|
|if the clicks is above the 10 = it will happen nothing (break;)|
|if the clicks is under the 10 = it will show a pickture|

ResetButton --->

|resets the countdown|

I hope I sed it good. I want to work with a "switch and case" system.

This is wath i have:

int counter = 0;

ImageButton finishhimbutton;
ImageButton resetbutton;
ImageView jb1;
ImageView jb2;
ImageView jb3;
ImageView jb4;
ImageView jb5;
ImageView jb6;
    TextView txtCount;





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        mTextView = (TextView)findViewById(R.id.mcounter);
        txtCount = (TextView)findViewById(R.id.timer);              
        txtCount.setText(String.valueOf(counter));
        jb1 = (ImageView) findViewById(R.id.JB1);
        jb2 = (ImageView) findViewById(R.id.JB2);
        jb3 = (ImageView) findViewById(R.id.JB3);
        jb4 = (ImageView) findViewById(R.id.JB4);
        jb5 = (ImageView) findViewById(R.id.JB5);
        jb6 = (ImageView) findViewById(R.id.JB6);
        finishhimbutton = (ImageButton) findViewById(R.id.finishhim);
        resetbutton = (ImageButton) findViewById(R.id.reserbutton);







        finishhimbutton.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                counter++;
                txtCount.setText(String.valueOf(counter));
           switch(counter){


                case 10:
                    jb1.setVisibility(ImageView.INVISIBLE);
                    jb2.setVisibility(ImageView.VISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);

                    break;

                case 20:
                    jb1.setVisibility(ImageView.INVISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.VISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);

                    break;

                case 30:
                    jb1.setVisibility(ImageView.INVISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.VISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);

                    break;

                case 40:
                    jb1.setVisibility(ImageView.INVISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.VISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);

                    break;

                case 49:

                    Toast.makeText(MainActivity.this, "Finish Him!",        Toast.LENGTH_SHORT).show();
                    break;
                case 50:            

                    jb1.setVisibility(ImageView.INVISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.VISIBLE);

                     break;

                case 51:
                    jb1.setVisibility(ImageView.VISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);
                    counter = 0;
                    txtCount.setText(String.valueOf(counter));

                    break;

                 default:

                     break;



                }

                }
            }



);



resetbutton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {               
            counter = 0;
            txtCount.setText(String.valueOf(counter));
            jb1.setVisibility(ImageView.VISIBLE);
            jb2.setVisibility(ImageView.INVISIBLE);
            jb3.setVisibility(ImageView.INVISIBLE);
            jb4.setVisibility(ImageView.INVISIBLE);
            jb5.setVisibility(ImageView.INVISIBLE);
            jb6.setVisibility(ImageView.INVISIBLE);

        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Thanks!


Solution

  • Thanks for the response

    I did this :

    public class MainActivity extends Activity implements OnClickListener{
    
    private Handler timer = new Handler();
    
    
    
    int counter = 0;
    
    TextView mTextView;
    ImageButton finishhimbutton;
    ImageButton resetbutton;
    ImageView jb1;
    ImageView jb2;
    ImageView jb3;
    ImageView jb4;
    ImageView jb5;
    ImageView jb6;
       TextView txtCount;
    
    
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
            txtCount = (TextView)findViewById(R.id.timer);              
            txtCount.setText(String.valueOf(counter));
            jb1 = (ImageView) findViewById(R.id.JB1);
            jb2 = (ImageView) findViewById(R.id.JB2);
            jb3 = (ImageView) findViewById(R.id.JB3);
            jb4 = (ImageView) findViewById(R.id.JB4);
            jb5 = (ImageView) findViewById(R.id.JB5);
            jb6 = (ImageView) findViewById(R.id.JB6);
            finishhimbutton = (ImageButton) findViewById(R.id.finishhim);
            resetbutton = (ImageButton) findViewById(R.id.reserbutton);
    
    
    
            final Runnable hMyTimeTask = new Runnable() {
                   public void run() {              
                        counter = 0;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.VISIBLE);
                        jb2.setVisibility(ImageView.INVISIBLE);
                        jb3.setVisibility(ImageView.INVISIBLE);
                        jb4.setVisibility(ImageView.INVISIBLE);
                        jb5.setVisibility(ImageView.INVISIBLE);
                        jb6.setVisibility(ImageView.INVISIBLE);
    
    
                   }
    
                }; 
    
    
    
            finishhimbutton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                                        if (counter == 1) {
                        timer = new Handler();
                        timer.removeCallbacks(hMyTimeTask);
                        timer.postDelayed(hMyTimeTask, 1000);
    
                     }
    
               switch(counter){
    
               case 1:
                    counter++;
                    txtCount.setText(String.valueOf(counter));
                    jb1.setVisibility(ImageView.VISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);
                    break;
    
                    case 10:
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.INVISIBLE);
                        jb2.setVisibility(ImageView.VISIBLE);
                        jb3.setVisibility(ImageView.INVISIBLE);
                        jb4.setVisibility(ImageView.INVISIBLE);
                        jb5.setVisibility(ImageView.INVISIBLE);
                        jb6.setVisibility(ImageView.INVISIBLE);
    
                        break;
    
                    case 20:
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.INVISIBLE);
                        jb2.setVisibility(ImageView.INVISIBLE);
                        jb3.setVisibility(ImageView.VISIBLE);
                        jb4.setVisibility(ImageView.INVISIBLE);
                        jb5.setVisibility(ImageView.INVISIBLE);
                        jb6.setVisibility(ImageView.INVISIBLE);
    
                        break;
    
                    case 30:
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.INVISIBLE);
                        jb2.setVisibility(ImageView.INVISIBLE);
                        jb3.setVisibility(ImageView.INVISIBLE);
                        jb4.setVisibility(ImageView.VISIBLE);
                        jb5.setVisibility(ImageView.INVISIBLE);
                        jb6.setVisibility(ImageView.INVISIBLE);
    
                        break;
    
                    case 40:
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.INVISIBLE);
                        jb2.setVisibility(ImageView.INVISIBLE);
                        jb3.setVisibility(ImageView.INVISIBLE);
                        jb4.setVisibility(ImageView.INVISIBLE);
                        jb5.setVisibility(ImageView.VISIBLE);
                        jb6.setVisibility(ImageView.INVISIBLE);
    
                        break;
    
                    case 49:
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        Toast.makeText(MainActivity.this, "Finish Him!", Toast.LENGTH_SHORT).show();
                        break;
                    case 50:            
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.INVISIBLE);
                        jb2.setVisibility(ImageView.INVISIBLE);
                        jb3.setVisibility(ImageView.INVISIBLE);
                        jb4.setVisibility(ImageView.INVISIBLE);
                        jb5.setVisibility(ImageView.INVISIBLE);
                        jb6.setVisibility(ImageView.VISIBLE);
    
                         break;
    
                    case 51:
                        counter++;
                        txtCount.setText(String.valueOf(counter));
                        jb1.setVisibility(ImageView.VISIBLE);
                        jb2.setVisibility(ImageView.INVISIBLE);
                        jb3.setVisibility(ImageView.INVISIBLE);
                        jb4.setVisibility(ImageView.INVISIBLE);
                        jb5.setVisibility(ImageView.INVISIBLE);
                        jb6.setVisibility(ImageView.INVISIBLE);
                        counter = 0;
                        txtCount.setText(String.valueOf(counter));
    
                        break;
    
                     default:
                            counter++;
                            txtCount.setText(String.valueOf(counter));
                         break;
    
    
    
                    }
    
                    }
                }
    
    
    
    );
    
    
    
    resetbutton.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
                counter = 0;
                txtCount.setText(String.valueOf(counter));
                jb1.setVisibility(ImageView.VISIBLE);
                jb2.setVisibility(ImageView.INVISIBLE);
                jb3.setVisibility(ImageView.INVISIBLE);
                jb4.setVisibility(ImageView.INVISIBLE);
                jb5.setVisibility(ImageView.INVISIBLE);
                jb6.setVisibility(ImageView.INVISIBLE);
    
            }
        });
    }   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
    
    }
    }
    

    Thanks for all help!