How to get seekbar default progress position on activity created - android

I have the following code in onCreate() Activity:
SeekBar vSeekbar = (SeekBar)findViewById(R.id.seekBar1);
final TextView tvText = (TextView) findViewById(R.id.textView);
//sets seekbar tab position from a randomly generated number...
//when the acitivty is first created I would like the SeekBar position to be set in the `textView`
tvText.setText(vSeekbar.getProgress()); //this is not showing anything...
//the onchangelistener works correctly...
vSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
tvText.setText(String.valueOf(progress)); //this works fine...
}
});
The seekBar1 position is set by a random number generated when the view is created so no way for me to know beforehand.
How can I get the seekBar1 position when the view is created and set it in the textView.

After random value set to seekbar call vSeekbar.getProgress() method. before you called then only you got zero value.
vSeekbar.setProgress(randomValue());
tvText.setText(vSeekbar.getProgress()+"");

You better limit your random values to the ProgressBar maximum value.
For example if your maximum value is 100 then use the following code: vSeekbar.setProgress(randomValue() % 100);
It will limit the random values to 0 to 99

Related

Android Multiple seekbar in expandable listview

I have expandable ListView with cds as a groups and tracks as a children. Each child has TextView, ImageButton(playButton) and SeekBar.
Now when I clicked one of the playButtons I want to get access to related seekBar.
My problem is that I have global SeekBar and I can't get the right SeekBar object to set my Runnable object correctly. So when I have for example 3 children, I click the first play button, but the third SeekBar is starting.
I have no idea how to resolve it. Any suggestions? This code is in getChildView() method.
SeekBar sb_songProgress;
iv_trackRowIcon.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
int song = l_cds.get(groupPosition).getTracks().get(childPosition).getTrackID();
if(ArtistCardActivity.mediaPlayer.isPlaying())
ArtistCardActivity.mediaPlayer.stop();
ArtistCardActivity.mediaPlayer.newTrack(song);
int finalTime = ArtistCardActivity.mediaPlayer.getDuration();
sb_songProgress.setMax((int) finalTime);
sb_songProgress.setClickable(false);
updateProgress(sb_songProgress);
}
});
public void updateProgress(SeekBar sb)
{
int timeElapsed = ArtistCardActivity.mediaPlayer.getCurrentPosition();
sb_songProgress.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
}
Runnable updateSeekBarTime = new Runnable()
{
public void run()
{
int timeElapsed = ArtistCardActivity.mediaPlayer.getCurrentPosition();
//set seekbar progress
sb_songProgress.setProgress((int) timeElapsed);
durationHandler.postDelayed(this, 100);
}
};
You have to use the ViewHolder pattern (see here, section "Overriding BaseAdapter"), then you will be able to handle seekbar progress with ViewHolder this way:
myHolder.sb_songProgress.setProgress((int) timeElapsed);
Hope it'll work

seekBar off by one driving me crazy

This seekBar is driving me nuts! Help me do this efficiently? I would like to have my starting value at 0 and max number displayed in textView $1,000,000. I would like to increment by $5,000. I also have tickers (plus and minus buttons) but they are off too because of my progress changed / textView display. it seems my textView is always 5,000 less than my actual progress. I can only get to 995,00 0 or do it differently and it skips from 10k to 0.
textView23 = (TextView) view.findViewById(R.id.textView23);
seekBar1.setMax(199);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser)
{
{
int newValue = seekBar.getProgress()* (5000);
String nValue = NumberFormat.getInstance().format(newValue);
textView23.setText("$" + nValue);
}
tempHouse = progress;
Calculate();
}
}
Since 1000000 = 200 * 5000, it seems that
seekbar.setMax(200)
would be the correct value ;)

Limit SeekBar Progress Within 20<Range<5000 to one popup

My SeekBar thumb must move within range 20 to 5000.
I need to prevent the thumb to move below 20. when this amount is reached, a toast message appear telling the user "minimum value reached".
However when i slide to minimum value and forced slide on limit, that is multiple slide towards 20, several popup appears onscreen. How can i limit to only one popup on screen on multiple slide towards minimum or maximum value.
Here is my .setOnSeekBarChanged listener :
sbMensualite.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<20){
progress=20;
}
else {}
if (duree_int>=Constant.DUREE_MAX_VALUE){
Toast.message="durée maximum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else if (duree_int<=Constant.DUREE_MIN_VALUE){
Toast.message="durée minimum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else {
//set progress duree android
sbDDuree.setProgress(duree_int);
//set amount of month for duree
txDDure.setText(Util.doubleNoDp(duree));
}
//set mensualite amount
txMensualite.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I need to prevent the thumb to move below 20. when this amount is reached, a toast message appear telling the user "minimum value reached".
First. Make the range of your slider be 0-4980. And every time you read the value add +20. If you want to set the value programmatically from a variable add -20.
Second, you really should NOT show a toast to tell that the minimum value was reached, because, using the method described in the previous paragraph, this will be obvious.
EDIT:
Would limiting the duree_int variable work for you???
if (duree_int>Constant.DUREE_MAX_VALUE){
duree_int = Constant.DUREE_MAX_VALUE;
Toast.message="durée maximum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else if (duree_int<Constant.DUREE_MIN_VALUE){
duree_int = Constant.DUREE_MIN_VALUE;
Toast.message="durée minimum atteinte";
m.startActivity(new Intent(m, Toast.class));
}else {
To cancel your existing toast and show a new one after, try this:
Toast myToast = new Toast(getApplicationContext());
sbMensualite.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<20){
progress=20;
}
if (duree_int>=Constant.DUREE_MAX_VALUE){
myToast.cancel(); //hide existing toast
myToast.setText("durée maximum atteinte");
myToast.setDuration(Toast.LENGHT_SHORT);
myToast.show();
}else if (duree_int<=Constant.DUREE_MIN_VALUE){
myToast.cancel(); //hide existing toast
myToast.setText("durée minimum atteinte");
myToast.setDuration(Toast.LENGHT_SHORT);
myToast.show();
}else {
//set progress duree android
sbDDuree.setProgress(duree_int);
//set amount of month for duree
txDDure.setText(Util.doubleNoDp(duree));
}
//set mensualite amount
txMensualite.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Restart CountDownTimer on click

I have a simple CountDownTimer that displays its content in a textView. I am trying to accomplish the following:
during count down, the view is not clickable: textview.setClickable(false);
after count down finish, if the user clicks the textview, the count down should restart.
So I try a combination of
#Override
public void onFinish() {//inside CountDownTimer
view.setClickable(true);
}
and
textview.setOnClickListener(countAgain);
OnClickListener countAgain = new OnClickListener() {//inside activity
#Override
public void onClick(View v) {
// counter.cancel();
counter.start();
}
};
But this is not doing it. Any ideas?
Set onclick listener for that text view, make it clickable .
In onclicklistner, make textview as non-clickable( yourTextView.setclickable(false) ).
change the text view values as you want and every time check the value against 0. When it becomes zero, make the text view clickable( yourTextView.setclickable(true) ).

Using a SeekBar within a ListView

I have a seekbar in a listview, it is disabled to stop the user from changing the value whilst scrolling the list. I wish to enable it if the list item is long clicked, allow the user to change it and then disable it again.
Any suggestions.
I seem unable to access the seekbar from the activity as it is setup inside the ArrayAdapter to display a value from the database
I am able to fire a both a click event and a long click event for the list item, as the seekbar is currently disabled.
Solution
In the ArrayAdapter I set both enabled and focusable to false and added the SeekBar listener, setting the attributes to false allowed me to use the list item onItemClicked listener. Inside the onItemCLickListener I retreived the seekbar, set the attributes to true, this meant it could be slid up or down. I then disabled it after the adjustment had been made. code below
ArrayAdapter Snippet
this code is inside the creation of the list item, in which the seekbar is housed
seekBar.setClickable(false);
seekBar.setFocusable(false);
seekBar.setEnabled(false);
/* attach listener */
attachProgressUpdatedListener(seekBar, positionOfItemInList);
AttachProgressUpdatedListener
this method attaches the listener to the seekbar, inside the arrayAdapter class
private void attachProgressUpdatedListener(SeekBar seekBar,
final int position) {
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int task_id = (Integer) seekBar.getTag();
TaskHandler taskHandler = new TaskHandler(DBAdapter
.getDBAdapterInstance(getContext()));
taskHandler.updateTaskProgress(task_id, progress);
mList.get(position).setProgress(progress);
//need to fire an update to the activity
notifyDataSetChanged();
seekBar.setEnabled(false);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// empty as onStartTrackingTouch listener not being used in
// current implementation
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// empty as onProgressChanged listener not being used in
// current implementation
}
});
}
OnItemCLickListener
this snipped is from the activity which houses the list view.
taskListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
SeekBar sb = (SeekBar) view.findViewById(R.id.seek);
sb.setFocusable(true);
sb.setEnabled(true);
}
});
I had overcame this issue by setting the following attributes to the seekbar in listview item layout file. Then I had the click event of listitem and seekbar working.
android:focusable="false"
android:focusableInTouchMode="false"
<SeekBar
android:id="#+id/playback_icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="0"
android:progressDrawable="#drawable/red_scrubber_progress"
android:thumb="#drawable/red_scrubber_control"
android:focusable="false"
android:focusableInTouchMode="false"/>

Categories

Resources