i'm trying to use a SeekBar to control the speed of the CountDownTimer and i'm stopped in 2 issues.
1 - The DownTimerInterval doesn't reset when i change the seekbar progress...but add the progress value every time i change.
2 - I want to stop the countdown when the seekbar progress is set to 0....and it don't work.
How to fix that?
Here is my code
rolagem.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
rolagemAutomatica();
}
});
private void rolagemAutomatica() {
barra = (SeekBar)findViewById(R.exibir_musica.barraFonte);
barra.setVisibility(1);
barra.setMax(4);
barra.setProgress(0);
barra.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String[] VR = {"0","10","50","75","100"};
//Define crono +1 cause position 0 crash the app
int crono = Integer.parseInt(VR[progress]+1);
CountDownTimer test = new CountDownTimer(400000 , crono) {
#Override
public void onTick(long millisUntilFinished) {
scroll_letra.smoothScrollBy(0,
(int) (millisUntilFinished / 300000));
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
if(crono-1 != 0){
test.start();
}else{
test.cancel();
}
}
});
}
Hold the CountDownTimer as an instance field so that you can cancel it when you need to. If starting one with a tick timer of 0 crashes you, then just don't start one when the speed is 0.
Something like this:
barra.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String[] VR = {"0","10","50","75","100"};
int crono = Integer.parseInt(VR[progress]);
if (fromUser) {
this.countDownTimer.cancel();
if (crono > 0) {
this.countDownTimer = new CountDownTimer(400000 , crono) {
#Override
public void onTick(long millisUntilFinished) {
scroll_letra.smoothScrollBy(0,
(int) (millisUntilFinished / 300000));
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
if(crono-1 != 0){
test.start();
}else{
test.cancel();
}
}
}
}
});
There are some more optimizations you could do. For example, you could probably define VR as just an array of ints so you don't have to parse them, and you could keep them on the instance so you don't have to create the array over and over, etc.:
int[] VR = new int[] {0, 10, 50, 75, 100};
I found the solution for this issue.
i declare the countDownTimer as a global variable.
CountDownTimer ctimer = null;
after that...i create a method
private void timerRol(int contador) {
if (contador == 0){
Log.v(String.valueOf(contador), "Stopped");
}else{
ctimer = new CountDownTimer(400000, contador) {
#Override
public void onTick(long millisUntilFinished) {
scroll_letra.smoothScrollBy(0, (int) (millisUntilFinished / 300000));
}
#Override
public void onFinish() {
}
};
ctimer.start();
}
}
after created the method...i call it on OnCreate passing a value as parameter...and stop it after the call.
timerRol(100);
ctimer.cancel();
after that i create the seekbar and call the method when the user change the position.
barraRolagem = (SeekBar) findViewById(R.exibir_musica.barraRolagem);
barraRolagem.setVisibility(1);
barraRolagem.setMax(5);
barraRolagem.setProgress(0);
final int[] crono = { 0, 750, 500, 100, 50, 1};
barraRolagem.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) {
if((crono[progress] == 0)) {
ctimer.cancel();
}else{
Log.v(String.valueOf(crono[progress]), "valor do crono");
ctimer.cancel();
timerRol(crono[progress]);
}
}
});
Related
I am playing mp3 file from url, but SeekBar is not updating while playing song.
It showing a buffering but not moving automatically when song starts.
When i am trying to move forcefully then also it working fine.
Below code i am using to play and update SeekBar.
I wanted to create a seekBar that track the progress of a mediaplayer but it doesnt work out quite well, the music is playing but the seekbar stay idle. Is there something that I left out?
Please help me i am new in android.
public class XYZ extends Fragment implements MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnCompletionListener{
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int mediaFileLength;
final Handler handler = new Handler();
private int realtimeLength;
Button b,b3,b4;
private double startTime = 0;
private double finalTime = 0;
public static int oneTimeOnly = 0;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_outside, container,
false);
rootView.setBackgroundResource(R.drawable.jotirling);
seekBar = rootView.findViewById(R.id.seekbar);
seekBar.setMax(99); // 100% (0~99)
seekBar.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mediaPlayer.isPlaying()) {
SeekBar seekBar = (SeekBar) v;
int playPosition = (mediaFileLength / 100) * seekBar.getProgress();
mediaPlayer.seekTo(playPosition);
}
return false;
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ProgressDialog mDialog = new ProgressDialog(getActivity());
#SuppressLint("StaticFieldLeak") AsyncTask<String, String, String> mp3Play = new AsyncTask<String, String, String>() {
#Override
protected void onPreExecute() {
mDialog.setMessage("Please wait It will take time according to your network speed...!");
mDialog.show();
mDialog.setCancelable(false);
}
#Override
protected String doInBackground(String... params) {
try {
mediaPlayer.setDataSource(params[0]);
mediaPlayer.prepare();
} catch (Exception ignored) {
}
return "";
}
#Override
protected void onPostExecute(String s) {
mediaFileLength = mediaPlayer.getDuration();
realtimeLength = mediaFileLength;
if (!mediaPlayer.isPlaying()) {
p=1;
mediaPlayer.start();
Toast.makeText(getActivity(), "Playing sound", Toast.LENGTH_SHORT).show();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
b3.setBackgroundResource(R.drawable.pp);
if (oneTimeOnly == 0) {
oneTimeOnly = 1;
}
} else {
p=0;
mediaPlayer.pause();
Toast.makeText(getActivity(), "Pausing "+
"sound",Toast.LENGTH_SHORT).show();
b3.setBackgroundResource(R.drawable.p);
}
updateSeekBar();
mDialog.dismiss();
}
};
mp3Play.execute("URL"); // direct link mp3 file
}
});
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
return rootView;
}
private void updateSeekBar() {
seekBar.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / mediaFileLength) * 100));
if (mediaPlayer.isPlaying()) {
Runnable updater = new Runnable() {
#Override
public void run() {
updateSeekBar();
}
};
}
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
seekBar.setSecondaryProgress(percent);
}
}
You should consider using setOnSeekbarChangeListener()
Reference : setOnSeekBarChangeListener()
Simple example would be
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) {
// TODO Auto-generated method stub
if(fromUser){
//do your things, like updating time durations etc.
}
}
});
Also if u a want working example, look at this link for code (i have just used circularSeekbar instead of regular one) https://github.com/iamSahdeep/Bop/blob/master/app/src/main/java/com/sahdeepsingh/Bop/Activities/PlayerView.java#L324
More Examples : https://www.javatips.net/api/android.widget.seekbar.onseekbarchangelistener
You can create use Timer for this:
seekBar.setMax(mediaPlayer.getDuration());
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
You are defining Runnable updater but not calling it.
if (mediaPlayer.isPlaying()) {
Runnable updater = new Runnable() {
#Override
public void run() {
updateSeekBar();
}
};
seekBar.post(updater)
}
I created my own player layout and used Youtube api to show the video content, however when the video starts, my seekbar doesn't update and remains fix. I think that the problem is inside mSeekBarChangeListener but i'm not sure.
Here's my code:
public class ItemDetails extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,View.OnClickListener {
private final static String TAG = "ItemDetails";
#BindView(R.id.item_title)TextView itemTitle;
#BindView(R.id.item_date)TextView itemDate;
#BindView(R.id.item_plot)TextView itemPlot;
#BindView(R.id.item_short_description)TextView itemShortDescription;
#BindView(R.id.backdop_image_details)ImageView imageBackdrop;
#BindView(R.id.tv_more)TextView tvSeeMore;
#BindView(R.id.trailer_video)YouTubePlayerView traileVideo;
#BindView(R.id.video_control)LinearLayout videoControlLayout;
#BindView(R.id.play_time)TextView mPlayTimeTextView;
#BindView(R.id.video_seekbar)SeekBar mSeekBar;
YouTubePlayer mPlayer;
private Handler mHandler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_details);
ButterKnife.bind(this);
findViewById(R.id.play_video).setOnClickListener(this);
findViewById(R.id.pause_video).setOnClickListener(this);
mSeekBar.setMax(100);
mSeekBar.setProgress(0);
mSeekBar.setOnSeekBarChangeListener(mVideoSeekBarChangeListener);
mHandler = new Handler();
}
#OnClick(R.id.back_arrow_detail)void backArrowClick() {
onBackPressed();
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if(youTubePlayer != null) {
mPlayer = youTubePlayer;
displayCurrentTime();
// start buffering
if(!b) {
youTubePlayer.cueVideo(trailerKey);
}
// set style and show control layout
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
videoControlLayout.setVisibility(View.VISIBLE);
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
youTubePlayer.setPlaybackEventListener(playbackEventListener);
}
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onPlaying() {
mHandler.postDelayed(runnable,100);
displayCurrentTime();
}
#Override
public void onPaused() {
mHandler.removeCallbacks(runnable);
}
#Override
public void onStopped() {
mHandler.removeCallbacks(runnable);
}
#Override
public void onBuffering(boolean b) {
}
#Override
public void onSeekTo(int i) {
mHandler.postDelayed(runnable,100);
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoading() {
}
#Override
public void onLoaded(String s) {
}
#Override
public void onAdStarted() {
}
#Override
public void onVideoStarted() {
displayCurrentTime();
}
#Override
public void onVideoEnded() {
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
};
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG,youTubeInitializationResult.toString());
}
SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
long lengthPlayed = (mPlayer.getDurationMillis() * progress) / 100;
mPlayer.seekToMillis((int) lengthPlayed);
seekBar.setProgress(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.play_video:
if(mPlayer != null && !mPlayer.isPlaying()) {
mPlayer.play();
}
break;
case R.id.pause_video:
if(mPlayer != null && mPlayer.isPlaying()) {
mPlayer.pause();;
}
break;
}
}
private void displayCurrentTime() {
if (null == mPlayer) return;
String formattedTime = formatTime(mPlayer.getDurationMillis() - mPlayer.getCurrentTimeMillis());
mPlayTimeTextView.setText(formattedTime);
}
private String formatTime(int millis) {
int seconds = millis / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
return (hours == 0 ? "--:" : hours + ":") + String.format("%02d:%02d", minutes % 60, seconds % 60);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
displayCurrentTime();
mHandler.postDelayed(this, 100);
}
};
}
For all who are looking for a solution:
Set the seekbar progress in within the runnable:
private Runnable runnable = new Runnable() {
#Override
public void run() {
displayCurrentTime();
//Progress in percentage
int progress = mPlayer.getCurrentTimeMillis() * 100 / mPlayer.getDurationMillis();
mSeekBar.setProgress(progress);
mHandler.postDelayed(this, 100);
}
};
Since this will start the OnSeekBarChangeListener, you need to add an additional if:
SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Only when the user change the progress
if(fromUser){
long lengthPlayed = (mPlayer.getDurationMillis() * progress) / 100;
mPlayer.seekToMillis((int) lengthPlayed);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
;)
Custom youtube player CHROMELESS style - set SeekBar Progress and Seek Bar TrackingTouch Forward/backward
mSeekBar = (SeekBar) findViewById(R.id.video_seekbar);
mSeekBar.setOnSeekBarChangeListener(mVideoSeekBarChangeListener)
SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
long lengthPlayed = (mYouTubePlayer.getDurationMillis() * seekBar.getProgress()) / 100;
mYouTubePlayer.seekToMillis((int) lengthPlayed);
}
};
private Runnable runnable = new Runnable() {
#Override
public void run(){
displayCurrentTime();
mHandler.postDelayed(this, 100);
}
};
private void displayCurrentTime() {
if (null == mYouTubePlayer) return;
String formattedTime = formatTime(mYouTubePlayer.getDurationMillis() - mYouTubePlayer.getCurrentTimeMillis());
play_timetv.setText(formattedTime);
playPercent = (int) (((float) mYouTubePlayer.getCurrentTimeMillis()/(float) mYouTubePlayer.getDurationMillis()) * 100);
System.out.println("get youtube displayTime 2 : "+playPercent);
// update live progress
mSeekBar.setProgress(playPercent, true);
}
I've implemented a youtube player (fragment) in my android project. I'm tryin' to play the video in chromeless player style, while implementing player control on my own. Play and pause buttons are working fine, however seekbar isn't moving as the video is progressing. Though, I'm able to skip video through it (play video ahead), but the seekbar isn't moving on it's own. Where did I go wrong?
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
displayCurrentTime(); // It displays current progress of video in --:-- format. It's working fine...
YPlayer.loadVideo(id); // id -> YouTube video id
YPlayer.play();
YPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
YPlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoading() {
}
#Override
public void onLoaded(String s) {
}
#Override
public void onAdStarted() {
}
#Override
public void onVideoStarted() {
seekBar.setMax(YPlayer.getDurationMillis()/1000);
}
#Override
public void onVideoEnded() {
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
});
YPlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
#Override
public void onPlaying() {
handler.postDelayed(runnable, 100); //Handler's instance initialized as instance variable
displayCurrentTime();
}
#Override
public void onPaused() {
handler.removeCallbacks(runnable);
}
#Override
public void onStopped() {
}
#Override
public void onBuffering(boolean b) {
handler.postDelayed(runnable, 1000);
}
#Override
public void onSeekTo(int i) {
handler.removeCallbacks(runnable);
}
});
playerControl.setVisibility(View.VISIBLE); //Layout with play (and pause) button, Seekbar (which isn't moving along with the video), and timer (string being returned by the above displayCurrentTime() method)
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
long lengthPlayed = (YPlayer.getDurationMillis() * progress) / 100;
YPlayer.seekToMillis((int) lengthPlayed);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
while(YPlayer != null && seekBar.getMax() > YPlayer.getCurrentTimeMillis())
{
int videoProgressed = 0;
try
{
Thread.sleep(1000);
videoProgressed = YPlayer.getCurrentTimeMillis();
}
catch (Exception e)
{
e.printStackTrace();
}
seekBar.setProgress(videoProgressed);
}
}
});
private void displayCurrentTime() {
if (YPlayer != null) {
String formattedTime = formatTime(YPlayer.getCurrentTimeMillis());
playTime.setText(formattedTime);
}
}
private String formatTime(int millis) {
int seconds = millis / 1000;
int minutes = seconds / 60;
return String.format("%02d:%02d", minutes % 60, seconds % 60);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
if(YPlayer != null) {
displayCurrentTime();
}
handler.postDelayed(this, 1000);
}
};
USE THIS CODE
Custom youtube player CHROMELESS style - set SeekBar Progress and Seek Bar TrackingTouch Forward/backward,
mSeekBar = (SeekBar) findViewById(R.id.video_seekbar);
mSeekBar.setOnSeekBarChangeListener(mVideoSeekBarChangeListener)
SeekBar.OnSeekBarChangeListener mVideoSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
long lengthPlayed = (mYouTubePlayer.getDurationMillis() * seekBar.getProgress()) / 100;
mYouTubePlayer.seekToMillis((int) lengthPlayed);
}
};
private Runnable runnable = new Runnable() {
#Override
public void run(){
displayCurrentTime();
mHandler.postDelayed(this, 100);
}
};
private void displayCurrentTime() {
if (null == mYouTubePlayer) return;
String formattedTime = formatTime(mYouTubePlayer.getDurationMillis() - mYouTubePlayer.getCurrentTimeMillis());
play_timetv.setText(formattedTime);
playPercent = (int) (((float) mYouTubePlayer.getCurrentTimeMillis()/(float) mYouTubePlayer.getDurationMillis()) * 100);
System.out.println("get youtube displayTime 2 : "+playPercent);
// update live progress
mSeekBar.setProgress(playPercent, true);
}
Check this tutorial if it can help you. This tutorial is all about Android Building Audio Player. It discuss here on how to build a simple audio player with basic controls like play, pause, forward, backward, next, previous, playlist and seekbar.
Just check the code in where the tutorial explain on how to update progress bar timer implemented in a background thread which runs in background using a Handler. You can check here how the seekbar being implemented.
For more information, check this related SO question.
Add youtubeSeekBar.setProgress(yPlayer.getCurrentTimeMillis()/100); in on playing() event
YPlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
#Override
public void onPlaying() {
handler.postDelayed(runnable, 100); //Handler's instance initialized as instance variable
displayCurrentTime();
youtubeSeekBar.setProgress(yPlayer.getCurrentTimeMillis()/100); //this is your key of success
}
#Override
public void onPaused() {
handler.removeCallbacks(runnable);
}
#Override
public void onStopped() {
}
#Override
public void onBuffering(boolean b) {
handler.postDelayed(runnable, 1000);
}
#Override
public void onSeekTo(int i) {
handler.removeCallbacks(runnable);
}
});
playerControl.setVisibility(View.VISIBLE); //Layout with play (and pause) button, Seekbar (which isn't moving along with the video), and timer (string being returned by the above displayCurrentTime() method)
}
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
}
}