How to hide View after a given time of inactivity - java

Basically I have a volume button that shows a hidden SeekBar when clicked, how to make the SeekBar go hidden again after 2 or 3 seconds of inactivity?
I just wanna know how can I check for how much time been spent since the SeekBar became visible without changing its progress!?

You could create a Runnable that sets the visibility of the SeekBar to invisible:
private final Runnable hideSeekBarRunnable = new Runnable() {
#Override
public void run() {
seekBar.setVisibility(View.INVISIBLE);
}
};
When the volume button is clicked, show the SeekBar and post the Runnable with a 2-3 sec delay:
seekBar.setVisibility(View.VISIBLE);
seekBar.postDelayed(hideSeekBarRunnable, 3000);
And if the SeekBar is interacted with (its progress changes), remove the pending Runnable and re-post it to reset the counter:
seekBar.removeCallbacks(hideSeekBarRunnable);
seekBar.postDelayed(hideSeekBarRunnable, 3000);

Related

Can't understand disable button behavior

I'm new to android and pretty new to the concept of timing in Java also. I've tried to create a simple app that counts the number of user clicks on the screen in five seconds. After the time ends, I want to disable the button and restart everything when clicking the 'Restart' button.
This is the current code:
public void clickCounter(View view){
++counter;
if(showCounter!=null)
showCounter.setText(Integer.toString(counter));
}
public void clickTimer(final View view) {
final Button Tap = findViewById(R.id.Tap);
clickCounter(view);
new CountDownTimer(5000, 5000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Tap.setEnabled(false);
}
}.start();
}
public void restartCounter(View view) {
counter=0;
showCounter.setText("Tap");
final Button Tap = findViewById(R.id.Tap);
Tap.setEnabled(true);
}
The button does disable after 5 seconds, but the restart button sometimes enables and then disables it right away (the counter and text changes properly).
I think the problem might be the way I'm using the Timer to do it (maybe I should use threads?)
You are creating a new timer on every button tap. So, when you reenable the button, one of your timers could be expiring, disabling the button afterwards. You should only create a new timer if there is none running.
To expand on Tiago Loureino's awesome answer, the problem is that you are creating a new CountDownTimer object every time the clickTimer() method is called.
The button does disable after 5 seconds, but the restart button sometimes enables and then disables it right away (the counter and text changes properly).
This 👆🏽 happens because you have several CountDownTimers executing their onFinish() method.
The solution to this problem is to have one single instance of your CountDownTimer. To put that in code, you can declare your CountDownTimer as below:
public CountDownTimer cdt = new CountDownTimer(5000, 5000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Tap.setEnabled(false);
}
};
You can then call cdt.start() anytime you want to start your the timer.

Why not showing progressBar in Android Studio?

I was trying to work with Progressbar in android studio. What I wanted was simply that when I will click on the button of the MainActivity it will intent to a new activity called 'Progress'.
My activities are here:
MainActivity.java:
and Progress.java:
My app runs, but not showing any progressbar motion. What should I do to show progressbar for 30 sec? I am not doing anything in the meantime. I just want to see a progressbar for 30 sec.
1000 is too small for loop but this code do what you want :
//Set visible progres bar
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
//Timer in your case 30 sek
int timer = 30*1000;
//create handler
final Handler handler = new Handler();
//set handler delayed
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Hidden progress bar
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
//your toast
Toast.makeText(this,"your msg",Toast.LENGTH_LONG).show();
//start new activity
startActivity(your intent)
}
}, timer);
}
mPrograssStatus is increasing to 100 in very short amount of time. You need to wait, for example try adding this to your code in loop while:
SystemClock.sleep(300);
value in brackets is amount of milliseconds that system will wait, in your case it is 300 to get 30 seconds. You also need to import library:
import android.os.SystemClock;

SeekBar is jerky during playback

Basically i'm using seek bar to show to user how audio playback is going, my visual update is presented in Thread and looks like this :
#Override
public void run() {
while(mUpdating) {
if(mSeekBar != null) {
mSeekBar.setProgress(mAudioPlaybackManager.getCurrentPosition());
}
//Other stuff is updating
}
}
So, for exqample if user plays audio that's 2500 ms in length, this SeekBar max value will be 2500 and it will be updating every ms
This same code working much slower in runOnUiThread, so i'm guessing when progress is changed something like postInvalidate is called
So basically every ms, the seekbar value should be changed. I guess that's the problem here. On my device Samsung J7 it's working smoothly but on Samsung Galaxy S5 it's just stopping and jumping all the time, like if i put this code in runOnUIThread, it would be really slow.
What could i do to make it smoother? Is there another View that i can use for this pirpose?
The things that current SeekBar is doing:
Showing progress of audio basically in while(updating)
When user changes the position of SeekBar audio is starting from that point.
The thread is a worker thread and not UI's thread, you're manipulating UI component within the thread thus blocking the UI's thread.
If you want to update the seekbar, update it in UI's thread. It can be done with UI's thread handler.
For example
private Runnable updateSeekBarTime = new Runnable() {
public void run() {
//get current position
timeElapsed = mediaPlayer.getCurrentPosition();
//set seekbar progress
seekbar.setProgress((int) timeElapsed);
//repeat yourself again in 100 miliseconds
durationHandler.postDelayed(this, 100);
}
};
Executed in
public void play(View view) {
mediaPlayer.start();
while(mUpdating) {
if(mSeekBar != null) {
// post the Runnable (updateSeekBarTime)
durationHandler.postDelayed(updateSeekBarTime, 100);
}
}
}
Also updating component can't simply be done every millisecond, from user perspective, giving a bit pause won't be noticed.

Android Countdown Timer in Broadcast receiver doesn't update TextView

I am creating application which works with SMS service and FTP networking.
If user does not establish connectivity, it will try to reconnect again in 30 seconds.
I am using CountDownTimer with TextView to inform user about time left to reconnnect.
Either it will be successful or it will start counting again.
My problem is, that if counter restarts while activity is in background or the screen is locked, TextView keeps showing number "1" (it won't update) until the timer restarts again in foreground (but updating numbers without timer restart works fine in backround or lock, I am using wakelock in my foreground service).
After counter restarts again (so it won't stop counting) while application is in foreground, everything comes back to normal, TextView updates from freezed "1" to "30" and starts counting down to "1".
I think problem will be somewhere in communication between counter thread and background activity with UI, but I don't know nothing more about it.
I tried several things like:
creating setter and getter for miliseconds and update them in each
tick, then try to update textview from onReume(), didn't work.
create local variable for TextView inside timer, initialize it inside onTick() and
try to update text from there, also didn't work.
Thanks everyone for help, I will appriciate any advices.
Part of code relative to question:
private CountDownTimer cdt = null;
private final TextView getTextView_ActivityMainMenu_Timer(){
return (TextView) findViewById(R.id.ActivityMainMenu_TextView_Timer);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getBooleanExtra("KEY_FAILED", false)){
cdt = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
getTextView_ActivityMainMenu_Timer().setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
;
}
}
.start();
}
else
{
if(cdt != null)
cdt.cancel();
}
}
};
Finally I found solution of this problem. I didn't realize that I am unregistering the receiver in onPause() method. So I had to change it and put registration of broadcast into onResume() and unregister it only in onDestroy() method.

Showing a progressdialog in android while a large method is executed

I have a long running method that is called during onCreate, this method populates textviews so interacts with the UI, and updates maybe 70 labels (about 3-20 seconds depending on device).
I want to display a progressdialog as this method executes.
Ideally I want to fire my method on the UI thread once the Activity has been displayed and the progress is displayed, this I cannot do, the Activity won't paint until the method has finished.
I hoped to find an event which was fired after the activity was displayed, and I found the one below, but it still leaves the screen black until the method has finished.
#Override
public void onPostCreate(Bundle savedInstanceState)
I am normally a WP7 developer, and in .NET you add an event handler for onLoadComplete which is fired after the ui is displayed, but before the user has a chance to interact withthe UI, how do I do this in Android JAVA?
Thanks
Put a ProgressBar in the View.
Then in the onCreate() or onResume() method do this:
new Thread() {
public void run() {
yourLargeMethod();
}
}.start();
Now you can do this inside your method to update the progressBar
public void yourLargeMethod() {
// doSomething
...
runOnUiThread(new Runnable() {
public void run() {
// update the progress from the thread
progressBar.setProgress(x); // x is your progress, 0 <= x <= progressBar.getMax()
}
});
}

Categories

Resources