Seekbar error in loading the progress on start of the activity - android

On the start of the activity I have set the progress to 8 ;
so the thumb is on 8 but the background is not on 8 , After changing the seekbar it working fine but on start of the activity it's not...
Thanks in advance...
Code is:-
public static int time=8;
oncreate(){
SeekBar sb=(SeekBar)findViewById(R.id.SB);
TextView tv=(TextView)findViewById(R.id.tv);
sb.setMax(20);
sb.setProgress(time);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
time=progress;
tv.setText(time+"");
}
});
}

Try to set the max value first. Then try to set the progress.

Related

SeekBar Android

How to hide/Show CheckBox,EditText on onProgressChanged of SeekBar.
First when Actvity is Created that time Hide a CheckBox,EditText. & when we Changed the Progress of SeekBar Show the CheckBox,EditText
In oncreate() hide your edittext and checkbox.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edittext.setVisibility(View.GONE);
checkbox.setVisibility(View.GONE);
}
And In setOnSeekBarChangeListener's onProgressChanged() show your checkbox and edittext. show below:-
seekbarObj.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean isUSer) {
// TODO Auto-generated method stub
edittext.setVisibility(View.VISIBLE);
checkbox.setVisibility(View.VISIBLE);
}
});
You can add seekBar listener and then do operations as per you need based on the progress value
SeekBar sk=(SeekBar) findViewById(R.id.seekBar1);
sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
//Change visibility here based on "progress" value
}
});

Android SeekBar: "setOnSeekBarChangeListener", = expected after this token

I'm pretty new to android, and I'm trying to implement SeekBar,
SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(new BatteryBar());
private class BatteryBar implements SeekBar.OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// To be implemented....
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
but I always get error
Syntax error on token "setOnSeekBarChangeListener", = expected after this token
according to Android SeekBar setOnSeekBarChangeListener or http://webtutsdepot.com/2011/12/03/android-sdk-tutorial-seekbar-example/ it should be correct way, how to do it.. Thanks for any help
try to do it like this without creating new class
batteryBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
}
}
One thing I noticed with the code you posted is that you're trying to implement your Seekbar listener as an inner class. If you want your Activity to implement the the OnSeekBarChangeListener it should look this :
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class example extends Activity implements OnSeekBarChangeListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(this);
//this references your class that is implementing OnSeekBarChangeListener
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
If you don't have any specific reasoning for using an inner class then I would suggest using the code below. You will have access to the same methods. It also removes the need for an inner class. This also gives you the advantage of having mutliple OnSeekBarChangeListener() as different variables for different seekbars. If you implement OnSeekBarChangeListener() on a class then you have to go through handling different cases for different Seekbars.
SeekBar.OnSeekBarChangeListener BatteryBar = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
};
I hope this helps
UPDATE:
public class demo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SeekBar batteryBar = (SeekBar) findViewById(R.id.battery_level);
batteryBar.setOnSeekBarChangeListener(BatteryBar);
}
SeekBar.OnSeekBarChangeListener BatteryBar = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
};
}

How to change volume of MediaPlayer in service?

I have an Activity (let call it A) and service (let call it B) with music player in it.
In A there's a seekbar, and I want to change MediaPlayer's volume with it.
Any ideas?
In MainActivity:
MusicService mms;
mms = new MusicService();
Settings_Sound_Music_Volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
sdm.setSetting("SETTINGS_MUSIC_VOLUME",String.valueOf(Settings_Sound_Music_Volume.getProgress()));
mms.player.setVolume(Settings_Sound_Music_Volume.getProgress(), Settings_Sound_Music_Volume.getProgress());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
Settings_Sound_Music_VolumeValue.setText(String.valueOf(progress) + "%");
}
});
But this code crashes my app. I get a NullPointerException at this line: mms.player.setVolume(...);

I want to set Seekbar to zero when remove touch

i m using seekbar in a game in which i want it to set to zero location by default when user leave it
verticalSeekBar=(VerticalSeekBar)findViewById(R.id.vertical_Seekbar);
vsProgress=(TextView)findViewById(R.id.vertical_sb_progresstext);
tv1=(TextView)findViewById(R.id.text1);
verticalSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
setProgress(0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
vsProgress.setText(progress+"");
if(progress==10)
{
check();
}
}
Use
VerticalSeekBar.setProgress(0);
instead of
seekBar.setProgress(0);

How can I get this code implemented with my ActionBar Tab setup?

I recently changed my application to have the action bar tabs to switch between Fragments. I coding I have done for the first fragment which is on the main activity is working fine, but on the second Tab it displays the items in its corresponding XML file but, none of my Java code seems to be working. I want 3 seek bars to change the number of a Text View, I had it working when I wasn't using Fragments, but just cant get it to work.
Do i have to put the code in a certain place of this file?
public class CountdownFragmentTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.countdown, container, false);
}
}
And this is what i want to add to this file:
SeekBar seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
SeekBar seekBar3 = (SeekBar)findViewById(R.id.seekBar3);
final TextView seekBar1Value = (TextView)findViewById(R.id.seekbar1value);
final TextView seekBar2Value = (TextView)findViewById(R.id.seekbar2value);
final TextView seekBar3Value = (TextView)findViewById(R.id.seekbar3value);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar1, int progress, boolean fromUser) {
// TODO Auto-generated method stub
seekBar1Value.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar2, int progress, boolean fromUser) {
// TODO Auto-generated method stub
seekBar2Value.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar3, int progress, boolean fromUser) {
// TODO Auto-generated method stub
seekBar3Value.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}

Categories

Resources