How to save Seekbar progress in shared Preferences in android? - android

private SharedPreferences sharedPreferences,sharedPreferences1 ;
private String Key_PROGRESS = "key_progress";
private String Key = "key_value";
private String PREFERENCE_PROGRESS = "preference_progress";
private String Key_PROGRESS1 = "key_progress1";
private String Key1 = "key_value1";
private String Seconds = "seconds";
private String PREFERENCE_PROGRESS1 = "preference_progress1";
oncreate methd()
{
sharedPreferences = getSharedPreferences(PREFERENCE_PROGRESS , Context.MODE_PRIVATE);
currentProgress = sharedPreferences.getInt(Key_PROGRESS, 0);
}
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
newProgressValue = seekBar.getProgress();
currentProgress = newProgressValue ;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(Key_PROGRESS, newProgressValue);
editor.commit();

here is the correct answer i am saving three seekbar values at a time,
public class MainActivity extends Activity implements OnSeekBarChangeListener {
public TextView progress1;
private SeekBar bar1;
private int savedProgress1;
private int loadedProgress1;
private String Key_PROGRESS_1 = "bar1_progress";
SharedPreferences mPreferences;
SharedPreferences.Editor editor;
/*********************
*
* ONCREATE
*
********************/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuracion2);
bar1 = (SeekBar)findViewById(R.id.seekbar); //make seekbar object
bar1.setOnSeekBarChangeListener(this); //set seekbar listener
progress1 = (TextView)findViewById(R.id.summary);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = mPreferences.edit();
}
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
Log.v("", "" + bar);
switch (bar.getId()) {
case R.id.fast_bar:
progress1.setText(progress + "%");
break;
case R.id.climb_bar:
progress2.setText(progress + "%");
break;
case R.id.platform_bar:
progress3.setText(progress + "%");
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
}
protected void onResume() {
super.onResume();
loadedProgress1 = mPreferences.getInt("Key_PROGRESS_1", 0);
bar1.setProgress(loadedProgress1);
loadedProgress2 = mPreferences.getInt("Key_PROGRESS_2", 0);
bar2.setProgress(loadedProgress2);
loadedProgress2 = mPreferences.getInt("Key_PROGRESS_3", 0);
bar3.setProgress(loadedProgress3);
}
protected void onPause(/*SeekBar seekBar*/) {
super.onPause();
savedProgress1 = bar1.getProgress();
editor.putInt(Key_PROGRESS_1, savedProgress1);
savedProgress2 = bar2.getProgress();
editor.putInt(Key_PROGRESS_2, savedProgress2);
savedProgress3 = bar3.getProgress();
editor.putInt(Key_PROGRESS_3, savedProgress3);
editor.commit();
}
}}

Depends on what you're trying to achieve but I'm assuming this is some kind of settings activity so I'd suggest saving the value in the method onStop. Here's how you should do it:
public class SettingsActivity extends Activity {
private SharedPreferences prefs;
private const String PREFS_NAME = "progress_prefs";
private const String PROGRESS_KEY = "progress_key";
private int progressValue = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressValue = progress;
}
}
}
protected void onStop() {
super.onStop();
prefs.edit()
.putInt(PROGRESS_KEY, progressValue)
.apply()
}
}
If that is not the solution you're looking for, please clarify your question.

mySeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// progress = progress / 45;
// progress = progress * 45;
Log.i(TAG, "OnSeekBarChangeListener():progress " + progress);
mProgress = progress;
}
#Override
protected void onPause() {
super.onPause();
SharedPreferenceHelper.setSharedPreferenceInt(configActivity.this, SharedPreferenceHelper.PREF_FORTUDE_WALPHABKGND, mProgress);
}

I think your code is only missing seekBar.setProgress(currentProgress) in your onCreate

Related

when i launch the activity the switch is working fine but it still goes off when the activity is going to backhand?

I recently worked in the project for making screen dimmer app but I stuck with one problem. In my app, there is MaterialAnimatedSwitch it works fine but when the app goes to the backhand, the switch is automatically off but the working of the app is perfectly continued. I want that the switch remains active or on whenever the user come back to the app interface.I use MaterialAnimatedSwitch so how to do this task.
i want the switch to be active or ON whenever the app resumes.
public class MainActivity extends AppCompatActivity {
int b1 = 40;
int b2 = 90;
DiscreteSeekBar bSeekBar;
ChatHeadService binder=null;
boolean isOn = false;
boolean mBounded;
public boolean isRunning=false;
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBounded = false;
mService = null;
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder) service;
mService = mLocalBinder.getServerInstance();
int brightness = mService.getBrightness();
int brightness2 = mService.getBrightness2();
seekBar.setProgress(brightness);
bSeekBar.setProgress(brightness2);
}
};
ChatHeadService mService;
RadioGroup radioGroup;
DiscreteSeekBar seekBar;
MaterialAnimatedSwitch aSwitch;
SharedPreferences sharedPrefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Kit[]{new Crashlytics()});
setContentView(R.layout.activity_main);
initInstances();
if (isOn && !isMyServiceRunning(ChatHeadService.class)) {
startService(new Intent(this, ChatHeadService.class));
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
for (RunningServiceInfo service : ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private void initInstances() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
seekBar = (DiscreteSeekBar) findViewById(R.id.seekBar);
bSeekBar = (DiscreteSeekBar) findViewById(R.id.brightnessSeekBar);
int b = sharedPrefs.getInt("b", 40);
int bb = sharedPrefs.getInt("bb", 90);
seekBar.setProgress(b);
bSeekBar.setProgress(bb);
aSwitch=(MaterialAnimatedSwitch)findViewById(R.id.switch1);
aSwitch.setClickable(sharedPrefs.getBoolean("isOn",false));
aSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(boolean b) {
if(b){
sharedPrefs.edit().putBoolean("isOn", true).apply();
isOn = true;
isRunning=aSwitch.isChecked();
startService(new Intent(MainActivity.this, ChatHeadService.class));
bindService();
}
else{
sharedPrefs.edit().putBoolean("isOn", false).apply();
isOn = false;
unBindService();
stopService(new Intent(MainActivity.this, ChatHeadService.class)); }
}
});
seekBar.setOnProgressChangeListener(new OnProgressChangeListener() {
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
b1 = value;
if (mService != null) {
mService.setBrightness(value, b2);
}
sharedPrefs.edit().putInt("b", value).apply();
}
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
}
});
bSeekBar.setOnProgressChangeListener(new OnProgressChangeListener() {
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
b2 = value;
if (mService != null) {
mService.setBrightness(b1, value);
}
sharedPrefs.edit().putInt("bb", value).apply();
}
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
}
});
}
#Override
protected void onStart() {
super.onStart();
if (isOn) {
bindService();
}
}
public void bindService() {
bindService(new Intent(this, ChatHeadService.class), mConnection,Context.BIND_AUTO_CREATE);
}
public void unBindService() {
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
#Override
protected void onPause() {
super.onPause();
isRunning=aSwitch.isPressed();
}
#Override
protected void onResume() {
super.onResume();
if(!isOn == aSwitch.isPressed()){
aSwitch.toggle();
}
}
}
Thanks in advance!
This is the screenshot when app is just open
This is the screenshot when night mode is ON
But When the going to backhand and then open the switch state changed
You are saving switch state in Shared preferences but not using it.
just add the following line above aswitch.setOnCheckedChangeListener
line, it will apply the state you saved in preferences
aSwitch.setChecked(sharedPrefs.getBoolean("isOn", false)); //false default
I suggest store value of your switch in one of your global variable and at time resume set it with the switch again like below:
public boolean isRunning = false;
#Override
protected void onPause() {
super.onPause();
isRunning = switchAler.isChecked();
}
#Override
protected void onResume() {
super.onResume();
binding.switchAler.setChecked(isRunning);
if(!isRunning == switchAler.isChecked()){
switchAler.toggle();
}
}

Custom youtube player doesn't update seekbar

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

Seekbar doesnt play from the point where it is dragged by the user

I have the problem with the seekbar thumb, although I have added every necessary codes for it to work. still the seekbar thumb doesn't play from the point dragged by the user. instead the thumb positioned itself back to the point it was last playing though. Any help will be appreciated.
plus = (ImageButton) findViewById(R.id.imageButton2);
minus = (ImageButton) findViewById(R.id.imageButton4);
player = MediaPlayer.create(this, R.raw.songnames);
player.setLooping(false);
im = (ImageButton) this.findViewById(R.id.imageButton3);
seekbar = (SeekBar) findViewById(R.id.mantraProgressBar);
seekbar.setVisibility(ProgressBar.VISIBLE);
seekbar.setProgress(0);
seekbar.setMax(player.getDuration());
new Thread(this).start();
im.setOnClickListener(this);
player.start();
im.setImageResource(R.drawable.pause_tran);
Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int cu = player.getCurrentPosition();
player.seekTo(cu - 5000);
}
});
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int cu = player.getCurrentPosition();
player.seekTo(cu + 5000);
}
});
}
#Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.imageButton3) {
if (player.isPlaying()) {
player.pause();
Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
ImageButton img1 = (ImageButton) this.findViewById(R.id.imageButton3);
img1.setImageResource(R.drawable.play_tran);
} else {
player.start();
Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
ImageButton img1 = (ImageButton) this.findViewById(R.id.imageButton3);
img1.setImageResource(R.drawable.pause_tran);
}
}
}
#Override
public void run() {
int currentPosition = 0;
String s;
int total = player.getDuration();
while (player != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = player.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekbar.setProgress(currentPosition);
}
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
player.seekTo(progress);
seekbar.setProgress(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
public class MainActivity extends AppCompatActivity {
private MyVideoView mVV;
private Handler mhandler;
private SeekBar seekbar;
private int duration;
private int backPositon = 0;
private int currentPosition;
private SharedPreferences sp;
private MediaController controller;
private TextView tv_curr;
private TextView tv_duration;
private Handler handler;
private boolean mExit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVV = (MyVideoView) findViewById(R.id.vv);
seekbar = (SeekBar) findViewById(R.id.seekbar);
tv_curr = (TextView) findViewById(R.id.tv_currtime);
tv_duration = (TextView) findViewById(R.id.tv_duration);
//get duration of video
getDuration();
//video start
playVideo();
//get current time
getCurrentDuration();
//listener
initEvent();
}
private void initHanlder() {
handler=new Handler(){
#Override
public void handleMessage(Message msg) {
if (msg.what==0x123){
tv_curr.setText(timeFormat(mVV.getCurrentPosition()));
seekbar.setProgress(mVV.getCurrentPosition());
}
if(msg.what==0x133){
int changedProgress = msg.arg1;
tv_curr.setText(timeFormat(changedProgress));
mVV.seekTo(changedProgress);
seekbar.setProgress(changedProgress);
mVV.start();
}
}
};
new Thread(new Runnable() {
#Override
public void run() {
while (mVV.getCurrentPosition()<duration){
Message message = Message.obtain();
message.what=0x123;
handler.sendMessage(message);
SystemClock.sleep(1000);
if(mExit){
break;
}
}
}
}).start();
}
private void getDuration() {
mVV.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
duration = mVV.getDuration();
timeFormat(duration);
Log.d("TAG", "---duration" + timeFormat(duration));
seekbar.setMax(duration);
//TextView time
tv_duration.setText(timeFormat(duration));
//init handler
initHanlder();
}
});
}
private String timeFormat(int time) {
StringBuilder mFormatBuilder;
Formatter mFormatter;
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int totalSeconds = time / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
private void playVideo() {
File file = new File(Environment.getExternalStorageDirectory(), "cgx_video/db.mp4");
if (!file.exists()) {
file.mkdir();
}
Log.d("TAG", file.toString());
mVV.setVideoPath(file.getPath());
mVV.start();
}
private void initEvent() {
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
currentPosition=progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
mVV.pause();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(handler!=null){
Message message = Message.obtain();
message.what=0x133;
message.arg1=currentPosition;
handler.sendMessage(message);
}
}
});
}
#Override
protected void onPause() {
super.onPause();
Log.d("TAG", "---onPause");
mVV.pause();
backPositon = mVV.getCurrentPosition();
Log.d("TAG", "backPositon:" + backPositon);
}
#Override
protected void onStop() {
super.onStop();
Log.d("TAG", "---onStop");
}
#Override
protected void onResume() {
super.onResume();
Log.d("TAG", backPositon + "---backPositon");
mVV.seekTo(backPositon);
mVV.start();
}
#Override
protected void onDestroy() {
//in case of leak of memeroy
mExit=true;
super.onDestroy();
}
}

Set 3 seekbars to change RGB channels

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
}

android seekbar widget

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..

Categories

Resources