I am trying to write a program in which I am deleting the seekbar if the seekbar is set to zero. So far, I have been able to remove the visibility of the seekbar from the android screen, but it is not deleted completely. Can anyone please hep me so that I can completely remove the seekbar when the position of the seekbar is set to zero.
Following is my code snippet:
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(i==0)
{
sb.setVisibility(View.INVISIBLE);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can use sb.setVisibility(View.GONE);
Related
I'm not able to get multiple SeekBar values in multiple TextViews by implementing OnSeekBarChangeListener interface.
By anonymous class I have implemented this but I want this way for my application.
seekBar1.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (seekBar.getId()){
case R.id.seek1:{
seek1Val=String.valueOf(2000+i*1000);
tvAmount.setText(seek1Val);}
break;
case R.id.seek2:{
seek2Val = String.valueOf(0+i*.5);
tvRate.setText(seek2Val);
}
break;
}
I expect output in each TextView for different SeekBar but value of progress I is taking same for every SeekBar.
better would be to add different setOnSeekBarChangeListener for each SeekBar.
Or if you don't want that then use tag instead id
Best would be:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seek1Val=String.valueOf(2000+i*1000);
tvAmount.setText(seek1Val);}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seek2Val = String.valueOf(0+i*.5);
tvRate.setText(seek2Val);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I need to make multiple seek bar in the same Java class. However, I wonder do we need to set each setOnSeekBarChangeListener for each seek bar? The code seems too long if I am doing so (I need to create 10 seek bar in the same class). Is there any other solution which I can shorten the code? Thank you so much for helping. :)
Below is my code
private SeekBar seekbar,seekbar1;
private TextView a,b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schiassess);
seekbar = (SeekBar) findViewById(R.id.delubar);
seekbar1 = (SeekBar) findViewById(R.id.disorbar);
a = findViewById(R.id.a);
b = findViewById(R.id.b);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
a.setText("" + progress);
a.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
b.setText("" + progress);
b.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
Follow the below steps it's simple
JUST IMPLEMENT SeekBar.OnSeekBarChangeListener in your class/fragment
public class Home extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
}
You don't have to write new OnSeekBarChangeListener for Every Seekbar...
Just Implement
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
} to your Activity/Fragment..
Now you can check for the id and perform action according to it
I was working in a very simple music player with a seek bar. But I got a problem with playing music,it doesn't work correctly every second - it backwards a while, then remain again like it's a buggy.
I know why but I can't solve it.
When the progress bar moves forward, the music recalibrates and vice versa, when the music moves forward, the progress bar recalibrates.
It's probably because of that that every time it goes back.
But I tried several things and still have the same problem.
Another problem is that when the music is paused, the progress bar goes in the opposite direction until it reaches zero. It doesn't stay where it was before the break.
The full code :
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private boolean bool = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.seekBar = (SeekBar) findViewById(R.id.sound_bar);
this.mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition() * seekBar.getMax() / mediaPlayer.getDuration());
}
}, 500, 500);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(seekBar.getProgress() * mediaPlayer.getDuration() / seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void playSound(View view) {
Button button = (Button) view;
if(bool) {
mediaPlayer.pause();
bool = false;
button.setText("Jouer le son");
} else {
mediaPlayer.start();
bool = true;
button.setText("Mettre en pause");
}
}
#Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}
#Override
protected void onStart() {
super.onStart();
if(bool) {
mediaPlayer.start();
}
}
}
Maybe use another thread to control the seekbar would help.
Take a look at this
another thing is the timer for seekBar its for the duration of the audio file the issue within it is that the audio file do (mini) stop every 1 sec so how to make seek bar update itself every second without doing any stops
seekBar2.setMax(mediaPlayer.getDuration());
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
seekBar2.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
public void reset(View v) {
mediaPlayer.reset();
}
Try this to Reset Your mediaPlayer
public void reset(View v) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
Try this to update your seekbar
private Handler mHandler = new Handler();
//you have to update Seekbar on UI thread
Your.Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar2.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
and whenyou need to update the MediaPlayer's position while user drag your SeekBar use below code
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mediaPlayer != null && fromUser){
mediaPlayer.seekTo(progress * 1000);
}
}
});
I'm making a project - Android - Arduino.
I have a seekbar to handle the temperature, to set it to 20'-35'.
When i open the program i want the seekbar to be set to the temperature that the sensor is set, not to 0 and let me set it.
This is the code for my seekbar:
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
tvTemp.setText(Double.toString(progress + 20));
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
<SeekBar
android:id="#+id/seek_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="35"
android:progress="20" />
Through xml you can do it as :
android:progress="20" means 20 is default position. Starting point of seek bar.
Or in your code you can do it like:
your_seekBar.setProgress(start_position);
ie.your_seekBar.setProgress(20);
Then you can try :
your_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(your_activity.this,"seek bar progress:"+progressChanged,
Toast.LENGTH_SHORT).show();
}
});
Just add
seekBar.setProgress(value)
below
seekBar.setOnSeekBarChangeListener
If your value is between 20 and 35, you may also want to specify
android:max="35"
android:min="20"
for your seekbar in your xml layout
SeekBar is inherited from ProgressBar,
So you can use 'setProgress()' method to set progress on startup.
seekBar.setProgress(someValue);
it will work
You can set it in your xml file:
android:progress="20"