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);
Related
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
}
});
I need to run a method every time the position of my seekbar is changed.
It does not matter:
how far it has moved
the direction
if its at the end or the start.
just that it has changed
is there a equivalent to onclickListener() for a seekbar.
Use this method provided by SeekBar class:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//progress changed
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
The seekbar has a setOnSeekBarChangeListener. Check this for more details https://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html
seekBar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I've made an Android app. In this app there is a sound player. I've put a Seekbar for audio player. However, when playing audio, the sound is on and off frequently. This problem has been created since adding seekbar.I use Thread for solve this problem but it not solved.I use this codes in OnCreate method :
btn_playSound.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
stop_background_sound();
if (mPlayer.isPlaying()) {
mPlayer.pause();
} else {
mPlayer.start();
new Thread() {
public void run() {
seekBar.post(new Runnable() {
#Override
public void run() {
// seekTo(getCurrentPosition());
seekBar.setProgress(mPlayer.getCurrentPosition());
seekBar.setMax(mPlayer.getDuration());
mHandler.postDelayed(this, 100);
}
});
}
}.start();
}
call_audio_player();
}
});
seekBar.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) {
seekTo(progress);
}
});
As I expected, you are updating progress of player's playback even when you change the position of seekbar with your handler, not changed by user.
replace
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekTo(progress);
}
with
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser) {
seekTo(progress);
}
}
I have customized seek bar in my application. I can drag the seek bar and access the unimplemented methods too. And my code is
public class Income extends Activity implements OnSeekBarChangeListener, OnItemSelectedListener
{
SeekBar net_sales1,cogs,operating,other_revenue;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.income);
seek_txt1 = (TextView)findViewById(R.id.net_sales);
seek_txt2 = (TextView)findViewById(R.id.cogs_te);
seek_txt3 = (TextView)findViewById(R.id.operate_exp);
seek_txt4 = (TextView)findViewById(R.id.other_rev);
net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
operating = (SeekBar)findViewById(R.id.mySeekingBar3);
other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
net_sales1.setOnSeekBarChangeListener(this);
cogs.setOnSeekBarChangeListener(this);
operating.setOnSeekBarChangeListener(this);
other_revenue.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
seek_count=0;
progress1=progress-150;
verify = fromUser;
if (fromUser)
{
if (progress >= 0 && progress <= seekBar.getMax())
{
progressString = String.valueOf(10*progress-150);
}
}
seek_id=seekBar.getId();
switch(current_spinner)
{
case R.id.mySeekingBar1 :
//tablelay.removeView(tableLayout);
seek_txt1.setText(progressString);
int seekbar1_val = Integer.parseInt(seek_txt1.getText().toString());
progress_of_spinner1 = progress;
break;
case R.id.mySeekingBar2 :
seek_txt2.setText(progressString);
progress_of_spinner2 = progress;
break;
case R.id.mySeekingBar3 :
seek_txt3.setText(progressString);
progress_of_spinner3 = progress;
break;
case R.id.mySeekingBar4 :
seek_txt4.setText(progressString);
progress_of_spinner4 = progress;
break;
}
net_sales1.setProgress(progress_of_spinner1);
cogs.setProgress(progress_of_spinner2);
operating.setProgress(progress_of_spinner3);
other_revenue.setProgress(progress_of_spinner4);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
int progress2 = progress1;
originalProgress=seekBar.getProgress();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
progress2=progress1;
}
Now my query is, is it possible to access all unimplemented methods of seek bar within onCreate() method of an activity? Can any body tell me? Thanks in advance.
You want to use in
onCreate
than you need to remove
implements OnSeekBarChangeListener
and below line
net_sales1.setOnSeekBarChangeListener(this);
cogs.setOnSeekBarChangeListener(this);
operating.setOnSeekBarChangeListener(this);
other_revenue.setOnSeekBarChangeListener(this);
and give separate
OnSeekBarChangeListener
for all seekbar than and than you use in onCreate
But in this for all seek bar all method override separately.
See below code for this
net_sales1.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
}
});
cogs.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
}
});
operating.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
}
});
other_revenue.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
}
});
yes, its possible, use as below:
Either set onSeekBarChangeListener to all seek bars and and provide definition of unimplemented inline. As below:
public class Income extends Activity implements OnItemSelectedListener
{
SeekBar net_sales1,cogs,operating,other_revenue;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.income);
seek_txt1 = (TextView)findViewById(R.id.net_sales);
seek_txt2 = (TextView)findViewById(R.id.cogs_te);
seek_txt3 = (TextView)findViewById(R.id.operate_exp);
seek_txt4 = (TextView)findViewById(R.id.other_rev);
net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
operating = (SeekBar)findViewById(R.id.mySeekingBar3);
other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
net_sales1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});
cogs.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});
operating.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});// Do same as cogs
other_revenue.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});//Do Same as cogs
}
}
Another option to create a listener object, in onCreate method, and set this listener to all seekbar, as below:
public class Income extends Activity implements OnItemSelectedListener
{
SeekBar net_sales1,cogs,operating,other_revenue;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.income);
seek_txt1 = (TextView)findViewById(R.id.net_sales);
seek_txt2 = (TextView)findViewById(R.id.cogs_te);
seek_txt3 = (TextView)findViewById(R.id.operate_exp);
seek_txt4 = (TextView)findViewById(R.id.other_rev);
net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
operating = (SeekBar)findViewById(R.id.mySeekingBar3);
other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
OnSeekBarChangeListener listener= new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
//Do some operation in this block
}
});
net_sales1.setOnSeekBarChangeListener(listener);
cogs.setOnSeekBarChangeListener(listener);
operating.setOnSeekBarChangeListener(listener);// Do same as cogs
other_revenue.setOnSeekBarChangeListener(listener);//Do Same as cogs
}
}
This is a strange one. I have 2 seek bars and I have established a listener for both. However when I slide the thumbbar for one seekbar - it updates that seekbar AND it also moves the thumb of the second seekbar.
If I move the thumb on the second seekbar - it only changes the second sekbar NOT the first.
If I remove the listener for the first seekbar - I get the same problem.
I have attached code pertiaing to the seekbar listeners. mSeekBarA is the first one. mSeekBarB is the second.
mSeekBarA.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
On_SFX_Volume_Change(progress);
}
});
mSeekBarB.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
On_MUS_Volume_Change(progress);
}
});
Thank you !
How did you instantiate these sliders? I bet that they are actually referring to the same instance. Possibly you added the same slider to the layout instead of adding the each one individually.