ScrollView block some action - java

I have a ScrollView which englobes some TextView.
When I press on a textView there's a "MotionEvent.ACTION_UP/DOWN" method which make a textSize change, then after 2s the size text change again.
Handler handler5 = new Handler();
handler5.postDelayed(new Runnable() {
#Override
public void run() {
Presence.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);
}
}, 2000);
With the ScrollView, the text doesn't change again after 2 seconds, any ideas?

You can use CountDownTimer Class like this:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
Presence.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
}
#Override
public void onTick(long millisUntilFinished) {
Presence.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);
}
}
Then call that class like this:
private CountDownTimer countDownTimer = new MyCountDownTimer(startTime, interval);

Related

How To Destroy CoundownTimer in Java when the button is pressed?

I make a timer with a count time of 5 seconds, then when I press the exit button the counter automatically stops?
Here is my timer code:
public void startTimer(final long finish, long tick) {
CountDownTimer t;
t = new CountDownTimer(finish, tick) {
public void onTick(long millisUntilFinished) {
long remainedSecs = millisUntilFinished / 1000;
textTimer.setText("" + (remainedSecs / 60) + ":" + (remainedSecs % 60));// manage it accordign to you
}
public void onFinish() {
textTimer.setText("00:00");
Toast.makeText(FloatingVideoWidgetShowService.this, "Waktu Habis", Toast.LENGTH_SHORT).show();
long seek = videoView.getCurrentPosition();
videoView.setKeepScreenOn(false);
stopSelf();
WritableMap args = new Arguments().createMap();
args.putInt("index", index);
args.putInt("seek", (int) seek);
args.putString("url", playingVideo.getString("url"));
args.putString("type", "close");
sendEvent(reactContext, "onClose", args);
onDestroy();
cancel();
}
}.start();
}
And this is my code when pressing the stop / exit button :
floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
long seek = videoView.getCurrentPosition();
videoView.setKeepScreenOn(false);
stopSelf();
WritableMap args = new Arguments().createMap();
args.putInt("index", index);
args.putInt("seek", (int) seek);
args.putString("url", playingVideo.getString("url"));
args.putString("type", "close");
sendEvent(reactContext, "onClose", args);
onDestroy();
}
});
How so when btn_deny is clicked Cuntdowntimer stops and does not force close?
Thanks.
You can't use onDestroy() to close your activity or fragment. Instead, you need to call finish().
To close the CountDownTimer, you need to make it a class scope variable. Prepare the timer at your startTimer then stop the timer by calling t.cancel() like the following code:
public class YourActivity extends Activity {
// Declare the variable to be accessed later.
CountDownTimer t;
...
public void startTimer(final long finish, long tick) {
t = new CountDownTimer(finish, tick) {
...
}.start();
}
private void yourOtherMethod() {
floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(t != null) t.cancel();
...
}
});
}
}

android - start an activity onTouch

In my app I have a countdown which counts from five to one. At the moment, the counter starts as soon as the activity is started. What I want is to stay at 5 seconds and wait to count down until the screen is touched. So the countdown timer should be started which a touch event.
public class MainActivity extends Activity implements OnGestureListener {
private static final String FORMAT = "%02d:%02d";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
new CountDownTimer(5000, 10) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%02d:%03d",
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
));
if (animationRunning) {
cancel();
}
}
public void onFinish() {
text.setText("done!");
}
}.start();
}
#Override
public boolean onTouchEvent(MotionEvent touchevent) {
}
}
Place your count down timer inside onTouch like below
public class MainActivity extends Activity implements OnGestureListener {
private static final String FORMAT = "%02d:%02d";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
#Override
public boolean onTouchEvent(MotionEvent touchevent) {
new CountDownTimer(5000, 10) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%02d:%03d",
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
));
if (animationRunning) {
cancel();
}
}
public void onFinish() {
text.setText("done!");
}
}.start();
}
}
Move this section of code to your onTouchEvent, because it's on activity create that is why its starting when your activity starts
new CountDownTimer(5000, 10) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%02d:%03d",
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
));
if (animationRunning) {
cancel();
}
}
public void onFinish() {
text.setText("done!");
}
}.start();
you should be fine

Countdown timer return the value of boolean

I have a Countdown timer and I want to return value when i call the class here is my class CountdownTimer
public class CountDownTimerActivity extends CountDownTimer {
public boolean finish = false;
public CountDownTimerActivity(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish()
{
finishTime(true);
}
#Override
public void onTick(long millisUntilFinished) {
finishTime(false);
Log.e("TESTINg","" + millisUntilFinished/1000);
}
public boolean finishTime(boolean finish) {
return finish;
}
}
Here is my calling countdown timer
CountDownTimerActivity countdowntimer = new CountDownTimerActivity(5000,1000);
countdowntimer.start();
if(// I DONT KNOW WHAT WILL I PUT HERE)
{
Log.e("Testing", "OK na");
}
Anyone can help me? Thank you
I think what you are trying to accomplish is a callback when the timer expires? If so, you have to think about the timer running by itself and then calling another method when finished. For instance:
public class Main extends Activity
{
private MyCountDownTimer myCountDownTimer;
#Override
public void onCreate(Bundle savedInstanceState){
myCountDownTimer = new MyCountDownTimer(5000, 1000);
myCountDownTimer.start();
}
public void finished(){
Log.e("Testing", "OK na");
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
finished();
}
#Override
public void onTick(long millisUntilFinished) {
Log.e("TESTINg","" + millisUntilFinished/1000);
}
}
}
See this link for more details/example:
https://androidcookbook.com/Recipe.seam;?recipeId=1205
EDIT to make a universal class that can be used in other activities, I would do something like this.
Create a MyCountDownTimer class that looks like this in its own file:
public class MyCountDownTimer extends CountDownTimer {
private MyCallback myCallback;
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
public Interface MyCallBack{
void callback();
}
#Override
public void onFinish() {
myCallback.callback();
}
#Override
public void onTick(long millisUntilFinished) {
Log.e("TESTINg","" + millisUntilFinished/1000);
}
public void setMyCallback(MyCallback myCallback){
this.myCallback = myCallback;
}
}
Then, in each of your activities, you would implement the new interface like so:
public class Main extends Activity implements MyCountDownTimer.MyCallback
{
private MyCountDownTimer myCountDownTimer;
#Override
public void callback(){
Log.e("Testing", "OK na");
}
#Override
public void onCreate(Bundle savedInstanceState){
myCountDownTimer = new MyCountDownTimer(5000, 1000);
myCountDownTimer.setMyCallback(this); //will use the callback method in this class which can be different for each activity
myCountDownTimer.start();
}
}
CountDownTimer is a very simple class, you don't need to make an entire class for it, you could create it on your caller class and in that way you could call the callback function on finish.
public class MyActivity extends Activity{
//Other methods and variables
CountDownTimer countdowntimer = new CountDownTimer(5000,1000){
#Override
public void onFinish() {
//Call the callback from your activity
}
#Override
public void onTick(long millisUntilFinished) {
Log.e("TESTINg","" + millisUntilFinished/1000);
}
};
}
If for some reason you must to make it in a different class then in that class you must create an interface that your activity must implement and in your finish method call the method of the interface listener.
Like in fragments: https://stackoverflow.com/a/34192088/2367237
I don't think you can return a value from CountDownTimer.
Ideally you should start the timer and implement what you have to do in the call back methods -
abstract void onFinish()
//Callback fired when the time is up.
abstract void onTick(long millisUntilFinished)
//Callback fired on regular interval.
sample implementation
https://androidcookbook.com/Recipe.seam;jsessionid=DF53064E03C7505C4EBF727E56E0728E?recipeId=1205
I think it's not working because you never set the variable finish to true on your code. You have to change your code in onFinish method to finish = true then put a logic inside onTick method to check if it's already finished then call onFinish() method;

restart or refresh a timer when button is clicked in android [duplicate]

This question already has an answer here:
Restart Countdown Timer with new time android
(1 answer)
Closed 8 years ago.
how to restart a timer in every button click? here given a sample code for timer setting
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
//getSelectedAlphabet.setText(selectedIndex.getText());
}
public void onFinish() {
//mTextField.setText("done!");
getSelectedAlphabet.setVisibility(View.INVISIBLE);
Log.d("Counter", "Finished....");
}
}.start();
Try this
CountDownTimer cdt;
cdt = new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
//getSelectedAlphabet.setText(selectedIndex.getText());
}
public void onFinish() {
//mTextField.setText("done!");
getSelectedAlphabet.setVisibility(View.INVISIBLE);
Log.d("Counter", "Finished....");
}
}.start();
// to restart
cdt.cancel(); // to cancel
cdt.start(); //to start
final CountDownTimer remainingTimeCounter = new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Log.d("Counter", "Finished....");
}
}.start();
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
remainingTimeCounter.cancel();
remainingTimeCounter.start();
}
});
final CounterClass timer = new CounterClass(180000,1000);
button.setOnclickListener(new OnClickListener() {
#Override
public void onClick(View view) {
timer.start();
}
};
CounterClass-
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
System.out.println("finished");
}
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format(
"%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millis)));
System.out.println(hms);
}
}

Unlimited interval for CountDownTimer

in this below simple code i want to set unlimited interval for CountDownTimer. i can not find any document for this action.
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
countDownTimer.cancel();
timerHasStarted = false;
}
#Override
public void onTick(long millisUntilFinished) {
Random rand = new Random();
int rnd = rand.nextInt(1000000000);
text.setText("" + rnd);
}
}
i want to stop that by click on button to finish.
In order to make the CountDownTimer infinite you can restart it onFinish method
like this
public void onFinish() {
if (!finishBtnPressed) {
countDownTimer.start();
} else {
//your logic
}
}
CountDownTimer has a method cancel() and you can call it when finish button is clicked (Note that cancel() call onFinish() so that's why i added the boolean variable finishBtnPressed). If you want to have some alternative then look into TimerTask

Categories