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
}
}
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'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'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
}
};
}
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 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);