I'm following this guy that wrote a code that change background color based on RGB channels assigned to 3 different seekbars, so R to one sk, G to another and B to another one.
The problem is that I can't get the code to work:
private int seekR, seekG, seekB;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
mySeekBarChangeListener msbclR = new mySeekBarChangeListener();
mySeekBarChangeListener msbclG = new mySeekBarChangeListener();
mySeekBarChangeListener msbclB = new mySeekBarChangeListener();
sbR.setonseekBarChangeListener(msbclR);
sbG.setonseekBarChangeListener(msbclG);
sbB.setonseekBarChangeListener(msbclB);
msbclR.onprogressChanged(sbR, sbR.getProgress(), false);
msbclG.onprogressChanged(sbG, sbG.getProgress(), false);
msbclB.onprogressChanged(sbB, sbB.getProgress(), false);
}
This text give me an error almost everywhere, whatever, nothing seems to work. I just want to know why.. Do I have to set something based on that mySeekBarChangeListener? Thanks in advance!
It should work:
private int seekR, seekG, seekB;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
sbR.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) {
seekR = progress;
doSomethingWithColor();
}
});
sbG.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) {
seekG = progress;
doSomethingWithColor();
}
});
sbB.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) {
seekB = progress;
doSomethingWithColor();
}
});
}
private void doSomethingWithColor() {
int color = Color.rgb(seekR, seekG, seekB);
// Do something with color
}
or more compact
private int seekR, seekG, seekB;
private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = 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) {
switch (seekBar.getId()) {
case R.id.RedSeekBar:
seekR = progress;
break;
case R.id.GreenSeekBar:
seekG = progress;
break;
case R.id.BlueSeekBar:
seekB = progress;
break;
}
doSomethingWithColor();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
sbR.setOnSeekBarChangeListener(onSeekBarChangeListener);
sbG.setOnSeekBarChangeListener(onSeekBarChangeListener);
sbB.setOnSeekBarChangeListener(onSeekBarChangeListener);
}
private void doSomethingWithColor() {
int color = Color.rgb(seekR, seekG, seekB);
// Do something with color
}
another one
private int seekR, seekG, seekB;
public class MySeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.RedSeekBar:
seekR = progress;
break;
case R.id.GreenSeekBar:
seekG = progress;
break;
case R.id.BlueSeekBar:
seekB = progress;
break;
}
doSomethingWithColor();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
MySeekBarChangeListener msbclR = new MySeekBarChangeListener();
MySeekBarChangeListener msbclG = new MySeekBarChangeListener();
MySeekBarChangeListener msbclB = new MySeekBarChangeListener();
sbR.setOnSeekBarChangeListener(msbclR);
sbG.setOnSeekBarChangeListener(msbclG);
sbB.setOnSeekBarChangeListener(msbclB);
}
private void doSomethingWithColor() {
int color = Color.rgb(seekR, seekG, seekB);
// Do something with color
}
Related
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);
}
Have three seekbars on one Activity. All of them have max of 100. I need to sum of all three was 100.
When seek1=50 and seek2=45 then seek3 must be 5.
When I slide seek1 to 55 seek3 must have 0. etc.
How can I code this?
Sorry, here is my code:
//01
final SeekBar seekBar01 = (SeekBar)findViewById(R.id.seek01);
final TextView seekBar01Value = (TextView)findViewById(R.id.txt01val);
seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar01Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//02
SeekBar seekBar02 = (SeekBar)findViewById(R.id.seek02);
final TextView seekBar02Value = (TextView)findViewById(R.id.txt02val);
seekBar02.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar02Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//03
SeekBar seekBar03 = (SeekBar)findViewById(R.id.seek03);
final TextView seekBar03Value = (TextView)findViewById(R.id.txt03val);
seekBar03.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar03Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I can show values on respective TextViews, but don't get how to change another seebar progress from other seekbar onProgressChanged.
Set OnSeekBarChangeListener to your seekbars. When one changes, set the values to the others with setProgress
Trouble was because of late declarations of SeekBar's.
If gather all SeekBar seekBarXX = (SeekBar)findViewById(R.id.seekXX); sooner than Change listeners all works as expected.
Sorry for stupidity.
Here is code:
final SeekBar seekBar01 = (SeekBar)findViewById(R.id.seek01);
final SeekBar seekBar02 = (SeekBar)findViewById(R.id.seek02);
final SeekBar seekBar03 = (SeekBar)findViewById(R.id.seek03);
seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar02.setProgress(100-seekBar01.getProgress());
}
...
});
I have a simple player.
To create a 'connection' between SeekBar and MediaPlayer and I was touched seekbar anywhere where played and Sync seekbar with player. In your opinion, where is the problem?
what can i do ?
If you have an example in this respect share it
my code:
public class MediaPlaybackActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
Handler seekHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getInit();
seekUpdation();
}
public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player = MediaPlayer.create(this, R.raw.win8);
seek_bar.setMax(player.getDuration());
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seek_bar) {
}
#Override
public void onStartTrackingTouch(SeekBar seek_bar) {
}
#Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
if(player != null && fromUser){
player.seekTo(progress * 1000);
}
}
});
}
Runnable run = new Runnable() {
#Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
player.start();
break;
case R.id.pause_button:
player.pause();
}
}}
Use the below method
AudioManager audioManager;
private int maxVolume;
private int curVolume;
/**
* To initialize the seek bar
*/
private void initialiseSeekBar() {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSeekBarVolume.setMax(maxVolume);
mSeekBarVolume.setProgress(curVolume);
mSeekBarVolume
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
arg1, 0);
}
});
}
I have one SeekBar which does specific task. I want to add another SeekBar with different task.
How can I put
seekbar.setOnSeekBarChangeListener(this);
unique for each SeekBar. Now when I touch either seekbar it results same.
You have a couple options:
1) The common practice is to use an anonymous class. Like this:
curSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d("slider","stop tracking");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
2) If you want to use this, you need to use some logic to figure out which SeekBar is being moved (using ids or object references). Like:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (seekBar == mySeekBar1) {
} else if (seekBar == mySeekBar2) {
}
}
you can add any no. of seekbars and every one to do something
I have a sample code with 3 seekbars every one send its progress value and all together create a colore to set as a background to imageview
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends Activity {
SeekBar seekBar1, seekBar2, seekBar3;
ImageView imageView;
public int pro1,pro2,pro3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imageView);
seekBar1 = (SeekBar) this.findViewById(R.id.seekBar1);
seekBar2 = (SeekBar) this.findViewById(R.id.seekBar2);
seekBar3 = (SeekBar) this.findViewById(R.id.seekBar3);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro1= progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#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) {
pro2= progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro3=progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
this youtube
https://youtu.be/AM6lJnze9nA
I have a seekbar in android app to capture the values that ranges from 10-90. What is the java code for capuring the values.
please help
Thanks.
public class seekbar extends Activity {
private SeekBar seekBar;
private TextView textView;
private int newProgressValue,currentProgress=10;
private SharedPreferences sharedPreferences ;
private String Key_PROGRESS = "key_progress";
private String PREFERENCE_PROGRESS = "preference_progress";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(PREFERENCE_PROGRESS , Context.MODE_PRIVATE);
currentProgress = sharedPreferences.getInt(Key_PROGRESS, 10);
setContentView(R.layout.main);
textView=(TextView)findViewById(R.id.radious);
seekBar=(SeekBar)findViewById(R.id.seekbar);
seekBar.setMax(90);
seekBar.setProgress(currentProgress);
textView.setText(String.valueOf(currentProgress));
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
newProgressValue = seekBar.getProgress();
currentProgress = newProgressValue ;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(Key_PROGRESS, newProgressValue);
editor.commit();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
textView.setText(String.valueOf(seekBar.getProgress()));
}
});
} }
You can use seekbar.onSeekbarChangeListener for this.See this link for official java doc..