Reputation: 337
I noticed that many people looking for the answer to same question however they always have different results and incomplete, I would like help from someone explain as simple as possible to help all users.
I will start programming an android now and wanted help, is as follows:
I have a TextView with id = value and one SeekBar with id = seekbar.
Wanted the seekbar 0-200 than 1 in 1, showing the values in TextView. And to correct the problem of sensitivity was wondering how to put two buttons + and - to increment or decrement a.
I thank you on behalf of all my college class.
Upvotes: 0
Views: 7463
Reputation: 1431
Here you dont need two diffrent buttons to perform increment & decrement operations.
Just set start and end positions here and do the operations, it will perform as like for your need.
Try this code...
float discrete=0;
float start=0;
float end=100;
float start_pos=0;
int start_position=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=-10; //you need to give starting value of SeekBar
end=10; //you need to give end value of SeekBar
SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
seek.setProgress(start_position);
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "discrete = "+String.valueOf(discrete), Toast.LENGTH_SHORT).show();
}
@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
// To convert it as discrete value
float temp=progress;
float dis=end-start;
discrete=(start+((temp/100)*dis));
}
});
}
Upvotes: 0
Reputation: 16558
You should have read the documentation before asking such a generic question. Indeed, I would like to answer to this question in layman terms. Just think about seekbar as a simple sensor. It can fire events like onProgressChanged(). Means, the interface for seekbar can tell us when the seekbar progress is changed. Thus, we can do some operations inside the callbacks provided by Android. So take a look at the code and see this is what you are looking for. This is a test application I have created for this question.
public class SeekbarActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
private SeekBar seekbar;
private TextView value;
private Button plus,minus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
value = (TextView) findViewById(R.id.value);
seekbar = (SeekBar) findViewById(R.id.seekbar);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
seekbar.setOnSeekBarChangeListener(this);
seekbar.setMax(201);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {seekbar.setProgress((int)seekbar.getProgress() + 1);}});
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {seekbar.setProgress((int)seekbar.getProgress() - 1);}});
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch) {
value.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {}
@Override
public void onStopTrackingTouch(SeekBar arg0) {}
}
And this is the XML used for this application
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Progress" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/plus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/minus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
Feel free to ask if you have any doubt on this code. By the way, this is not a code generating forum! Seems like I'm breaking one of the SOF terms... :)
Upvotes: 2
Reputation: 29199
To listen seek position changes, use
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
txtProgress.setText(""+progress);
}
});
on + and - Button click implement onClickListener, and in onClick method, write
int newProgress=seekBar.getProgress()+10;
seekBar.setProgress(""+newProgress);
Upvotes: 1
Reputation: 4245
Assume this is the UI you want (pretty bad ASCII art)
+-------------------+
| Progress bar value|
| TextView |
+-------------------+
+-----+ Progress Bar +-----+
| - | |--------------------------------------|| + |
+-----+ +-----+
Layout required to achieve this (pseudocode)
<RelativeLayout>
<TextView id=progValue
alignHorizontalCenter=true>
<ProgressBar id=progBar
alignHorizontalCenter=true
layout_below=id/progValue
android:max=200>
<Button id=negative
layout_toLeftof=id/progBar>
<Button id=positive
layout_toRightof =id/progBar>
</RelativeLayout>
Listener for Progress Bar (pseudocode)
progBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progValue.setText(""+progress);
}
});
Listener for buttons (pseudocode)
OnClickListener(){
public void onClick(View v) {
if(positive == v){
progBar.setProgress(progBar.getProgress()+1);
}else if (negative == v){
progBar.setProgress(progBar.getProgress()-1);
}
}
}
Upvotes: 0