Press button and button counting from 3 to 0

130 views Asked by At

Button back counts and button making activate and when press it will open new activity...
I m trying to new things for my app, so is it possible? (I need to show interstitial adds and its for make button wait with counting.)

Method A

  1. User clicks a button
  2. Button counting 3. 2. 1. 0
  3. Button Active!
    ( and then Click button and open Next/new activity etc..)

Method B

  1. User opens app / OnCreate starts...
  2. Button counting 3. 2. 1. 0
  3. Button Active!

( ... Click button and open Next /new activity etc..)

Which code or method can do this please help. Thanks :)

1

There are 1 answers

2
Rajakumar On BEST ANSWER

Please try this code and at the end instead of toast you just pass a intent to next activity and in the second activity do paste this same code it will work better.

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Sample extends Activity {

// Declare a variable to hold count down timer's paused status
private boolean isPaused = false;
// Declare a variable to hold count down timer's paused status
private boolean isCanceled = false;

// Declare a variable to hold CountDownTimer remaining time
private long timeRemaining = 0;
CountDownTimer timer;
long millisInFuture = 4000; // 30 seconds
long countDownInterval = 1000; // 1 second
TextView tView;
Button btnStart;
Button btnPause;
Button btnResume;
Button btnCancel;

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

    // Get reference of the XML layout's widgets
    tView = (TextView) findViewById(R.id.tv);
    btnStart = (Button) findViewById(R.id.btn_start);

    // Set a Click Listener for start button
    btnStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            timercall();

            // Disable the start and pause button
            btnStart.setEnabled(false);

            // Initialize a new CountDownTimer instance

        }
    });

    // Set a Click Listener for pause button

    // Set a Click Listener for resume button

}

private void timercall() {
    // TODO Auto-generated method stub
    btnStart.setEnabled(false);
    timer = new CountDownTimer(millisInFuture, countDownInterval) {
        public void onTick(long millisUntilFinished) {
            // do something in every tick
            if (isPaused || isCanceled) {
                // If the user request to cancel or paused the
                // CountDownTimer we will cancel the current
                // instance
                cancel();
            } else {
                // Display the remaining seconds to app interface
                // 1 second = 1000 milliseconds
                tView.setText("" + millisUntilFinished / 1000);
                // Put count down timer remaining time in a variable
                timeRemaining = millisUntilFinished;
            }
        }

        public void onFinish() {
            // Do something when count down finished
            tView.setText("Activated");
            btnStart.setText("Active");
            btnStart.setEnabled(true);
            btnStart.setClickable(true);
            // Enable the start button
            btnStart.setEnabled(true);
            // Disable the pause, resume and cancel button
            btnStart.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "next page",
                            Toast.LENGTH_SHORT).show();
                }
            });

        }
    }.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    // noinspection SimplifiableIfStatement
    /*
     * if (id == R.id.action_settings) { return true; }
     */

    return super.onOptionsItemSelected(item);
}
}

//activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CountDownTimer will display here..."
    android:textColor="#000000" />

<Button
    android:id="@+id/btn_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv"
    android:text="Start" />

</RelativeLayout>