android seekbar widget - android

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

Related

How to save Seekbar progress in shared Preferences in 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

How to simultaneously change the value of one seekbar based on other

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

Simple Interest Calculator calculate button error

I am very new to Android development and have been trying to make a simple interest calculator. It uses a SeekBar to take the number of years input. The App crashes as I click the Calculate Button.
I am using Eclipse and trying to run the app directly on the device without any emulator.
package com.sevendaytutorials.interestcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener{
private EditText principal_amount;
private EditText rate;
private SeekBar seekbar;
private TextView seekBarValue;
private Button calculate;
private TextView result;
private String Principal, Rate;
private int number_of_years;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
principal_amount = (EditText)findViewById(R.id.principal_amount);
Principal = principal_amount.getText().toString();
rate = (EditText)findViewById(R.id.interest_rate);
Rate = rate.getText().toString();
seekbar = (SeekBar)findViewById(R.id.seekBar1);
seekbar.setOnSeekBarChangeListener(this);
seekBarValue = (TextView) findViewById(R.id.number_of_years);
calculate = (Button) findViewById(R.id.calculate_button);
calculate.setOnClickListener(this);
result = (TextView) findViewById(R.id.result_textview);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == calculate) {
Calculate();
}
}
private void Calculate() {
// TODO Auto-generated method stub
double p = Double.parseDouble(Principal);
double R = Double.parseDouble(Rate);
double r = R/100.0;
double A = p*(1+r*number_of_years);
double I = A - p;
result.setText("The amount of interest after " +
Integer.toString(number_of_years)+" year(s) would be " + Double.toString(I));
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(Integer.toString(progress));
number_of_years = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
in your onCreate(), you did these-
Principal = principal_amount.getText().toString();
and
Rate = rate.getText().toString();
Now both of your values are null as nothing are typed during onCreate()
Now when are you calling Calculate(), you are doing these-
double p = Double.parseDouble(Principal);
double R = Double.parseDouble(Rate);
as we can see, you didn't assign any proper value to Principal & Rate yet anywhere, so you should get Exception at these lines. you should modify to-
private void Calculate() {
// TODO Auto-generated method stub
Principal = principal_amount.getText().toString();
Rate = rate.getText().toString();
if (Principal == null || Principal.isEmpty() || Rate == null || Rate .isEmpty())
{
// user are yet to fill the edittext, validation error
}else{
double p = Double.parseDouble(Principal);
double R = Double.parseDouble(Rate);
double r = R/100.0;
double A = p*(1+r*number_of_years);
double I = A - p;
result.setText("The amount of interest after " +
Integer.toString(number_of_years)+" year(s) would be " + Double.toString(I));
}
}
Local variables
SeekBar seek1, seek2, seek3;
float amount, rate, time, interest, total;
TextView tv1, tv2, tv3, tv4, tv5;
Button calculate;
String val, val1, val2, inter, totall;
In onCreate() find ids
seekBar1 = findViewById(R.id.seek1);
seekBar2 = findViewById(R.id.seek2);
seekBar3 = findViewById(R.id.seek3);
calculate = findViewById(R.id.calculate);
tvAmount = findViewById(R.id.tv1);
tvRate = findViewById(R.id.tv2);
tvtime = findViewById(R.id.tv3);
tvEMI = findViewById(R.id.tv4);
tvInterest = findViewById(R.id.tv5);
Apply setOnClickListener() on Button
calculate.setOnClickListener(this);
Apply setOnSeekBarChangeListener() using anonymous class and implement
all methods
seek1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
Get Value of SeekBar in String Variable and set on text view.
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
val = String.valueOf(1000 + i * 1000);
tv1.setText(val);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
seek2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
val1 = String.valueOf(5 + i * .5);
tv2.setText(val1);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
seek3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
}
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
val2 = String.valueOf(i);
tv3.setText(val2);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
Set maximum limit of Seek Bars
seek1.setMax(99);
seek2.setMax(30);
seek3.setMax(100);
Implement onClick() for button click Action
#Override
public void onClick(View view)
{
calInterest();
tv4.setText(": ₹"+inter);
tv5.setText(": ₹"+totall);
}
Create a method for calculation calInterest() which is called at time
of button Click see onClick()
public void calInterest()
{
amount = Float.parseFloat(val);
rate = Float.parseFloat(val1);
time = Float.parseFloat(val2);
interest = (amount * rate * (time / 12))/100;
total =interest+amount;
inter = String.valueOf(interest);
totall = String.valueOf(total);
}

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
}

Using a simple seekbar in android

How to get the values from SeekBar and pass them as intents to another activity
I have three seekBars as below
PRICEbar
DISTANCEbar
RATINGbar
I want to get the selected value from the three seek bars and pass them as intents to another activity through DoneIntent
How to make this happen
Filters.java
public class Filters extends Activity implements OnSeekBarChangeListener{
// declare text objects variables
private SeekBar PRICEbar,DISTANCEbar, RATINGbar;
private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
Button back;
Button Done;
private RadioGroup rdg;
private RadioButton indian;
private RadioButton thai;
private RadioButton chinese;
private String selectedType="";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
/** Finding all the views in this Activity. */
back=(Button) findViewById(R.id.TopNavigationBarFilterBackButton);
Done=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
Done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent DoneIntent=new Intent(Filters.this,AndroidTabRestaurantDescFilterListView.class);
DoneIntent.putExtra("REST1",selectedType);
startActivity(DoneIntent);
}
});
PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
PRICEbar.setOnSeekBarChangeListener(this);
PRICEbar.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
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
PRICEtextProgress.setText("Price:: Rs "+progress);
seekBar.setMax(100);
}
});
}
use PRICEbar.getProgress(); to get current value of your seekbar. and then pass them as intent with this code:
Intent intent = new Intent(MainActivity.this, YourNewActivity.class);
intent.PutExtra("PriceBar", PRICEbar.getProgress());
intent.PutExtra("DistanceBar", DISTANCEbar.getProgress());
intent.PutExtra("RatingBar", RATINGbar.getProgress());
startActivity(intent);

Categories

Resources