What I have is a seekbar which is controlled by buttons rather than a slider using set/get progress.
I’m trying to get the seekbar progress to increase when a user presses a button and then stop increasing when they lift off the button. I have an onkey listener the detects up and down. I’m just not sure where to put the setprogress part, if I put a loop in on ACTION_DOWN, it’s then looping rather than detecting ACTION_UP the same seems to happen if I have a while loop for ACTION_DOWN.
I think I may also have to set refreshDraweableState, but I’ve not had much look with that either.
Potentially i’m trying to mess with seekbars too much but I’d appreciate any help/advice. Thanks!
This is my keylistner which is where I have been mostly messing about.
public boolean onKey(View view, int keyCode, KeyEvent event) {
builder.setLength(0);
switch (event.getAction()){
case KeyEvent.ACTION_DOWN:
builder.append(event.getKeyCode());
String text = builder.toString();
Log.w("seekbar", text);
textView.setText(text);
while (sk.getProgress() < 100){
sk.setProgress(sk.getProgress()+1);
sk.refreshDrawableState();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
}
break;
case KeyEvent.ACTION_UP:
break;
}
return true;
}
}
You need to be able to stop your while loop in the ACTION_DOWN even, otherwise it will just keep going until it reaches 99.
Also your while loop blocks the UI thread so I would put it in a new thread (not sure how to do that on plain Android, I use Mono for Android), and either stop that thread when the ACTION_UP event occurs, or put another check in your while loop that breaks it.
In Mono for Android that would be done somewhat like this: