Android seekbar textView - java

I have 3 seekbar to change the background color. Each seekbar har a textView from 0-255 to show the colorcode in RGB. When I slide one of the seekbar, the RGB textView change on all. How can I specify, so only the textView that belong to the seekbar, that I slide will change?
private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{
TextView seekBar1Progress;
TextView seekBar2Progress;
TextView seekBar3Progress;
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
updateBackground();
seekBar1Progress = (TextView) findViewById(R.id.viewSlider1);
seekBar1Progress.setText(" " + progress);
seekBar2Progress = (TextView) findViewById(R.id.viewSlider2);
seekBar2Progress.setText(" " + progress);
seekBar3Progress = (TextView) findViewById(R.id.viewSlider3);
seekBar3Progress.setText(" " + progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};

You are getting SeekBar object in OnProgressChanged method, you can compare its id to your individual SeekBar. Try below code:
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(seekBar.getId()==R.id.seekBar1){
seekBar1Progress = (TextView) findViewById(R.id.viewSlider1);
seekBar1Progress.setText(" " + progress);
}else if(seekBar.getId()==R.id.seekBar2){
seekBar2Progress = (TextView) findViewById(R.id.viewSlider2);
seekBar2Progress.setText(" " + progress);
}else{
seekBar3Progress = (TextView) findViewById(R.id.viewSlider3);
seekBar3Progress.setText(" " + progress);
}
}
Hope this will help!!

Related

Android Studio :How to solve this execution error

I am new to android studio, and I don't understand why my app crash when I start using the seekbar.
Could someone please help me?
public class MainActivity extends AppCompatActivity {
SeekBar seekBar; //declare seekbar object
TextView textView, textView2;
ListView simpleListview;
//declare member variables for SeekBar
int discrete;
int start = 50;
int start_position = 50;
//progress tracker
int temp = 0;
//declare objects for ViewStub
ViewStub stub;
CheckBox checkBox;
ListView lv; //declare Listview object
Button button;
View view;
public MainActivity() {
discrete = 0;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare viewstub object
stub = findViewById(R.id.viewStub1);
#SuppressWarnings("unused")
View inflated = stub.inflate();
stub.setVisibility(View.INVISIBLE);
//ViewStub logic
checkBox = findViewById(R.id.checkBox1);
//handle checkbox click event
checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked) {
//remove objs from parent view to allow for child view objs
checkBox.setVisibility(View.GONE);
seekBar.setVisibility(View.GONE);
textView.setVisibility(View.GONE);
stub.setVisibility(View.VISIBLE);
}
}
});
//seekbar logic
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);
textView.setText(" Celsius at 0 degrees");
//set default view
seekBar = findViewById(R.id.seekbar);
seekBar.setProgress(start_position);
//create event handler for SeekBar
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (temp == 0) {
//for initial view result
//Toast.makeText(getBaseContext(), "Fahrenheit result: 32 degrees",
//Toast.LENGTH_SHORT).show();
textView2.setText("Fahrenheit result 32 degrees") ;
}
else{
//Toast.makeText(getBaseContext(), "Fahrenheit result: "
//+ String.valueOf(discrete) +
//" degrees",Toast.LENGTH_SHORT).show();
textView2.setText("Fahrenheit result:" + String.valueOf(discrete) + "degrees");
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar){ }
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// To convert progress passed as discrete (Fahrenheit) value
temp = progress - start;
discrete = (int) Math.round((((temp * 9.0) /
5.0) + 32));
//convert C to F temp textView.setText(" Celsius at " + temp + " degrees");
textView.setText(" Celsius at " + temp + " degrees");
}
});
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);
textView2 is not initialised here...
Change above code to
textView = findViewById(R.id.textview);
textView2 = findViewById(R.id.textView2);
TextView textView = findViewById(R.id.textview);
TextView textView2 = findViewById(R.id.textView2);
initialize textView2
You seem to have missed that part

Multiple seekbar in one java class, need implement each setOnSeekBarChangeListener for each seekbar?

I need to make multiple seek bar in the same Java class. However, I wonder do we need to set each setOnSeekBarChangeListener for each seek bar? The code seems too long if I am doing so (I need to create 10 seek bar in the same class). Is there any other solution which I can shorten the code? Thank you so much for helping. :)
Below is my code
private SeekBar seekbar,seekbar1;
private TextView a,b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schiassess);
seekbar = (SeekBar) findViewById(R.id.delubar);
seekbar1 = (SeekBar) findViewById(R.id.disorbar);
a = findViewById(R.id.a);
b = findViewById(R.id.b);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
a.setText("" + progress);
a.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
b.setText("" + progress);
b.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
Follow the below steps it's simple
JUST IMPLEMENT SeekBar.OnSeekBarChangeListener in your class/fragment
public class Home extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
}
You don't have to write new OnSeekBarChangeListener for Every Seekbar...
Just Implement
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
} to your Activity/Fragment..
Now you can check for the id and perform action according to it

Seek Bar Steps Values to a Billion

I tried several solutions from the seek bar step values question from stack, unfortunately none of it work for my case. The maximum number for my problem is 1,000,000,000. Each steps is increased by 1000.The problem was it does not increase gradually by 1000.
public class MainActivity extends AppCompatActivity {
private SeekBar mSb;
private TextView mTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSb = (SeekBar) findViewById(R.id.sb_a);
mTv = (TextView) findViewById(R.id.textView);
mSb.setMax(1000000);
mSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
double iPoint ;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iPoint=progress*1000;
if(seekBar.getProgress()==0){
mTv.setText(String.valueOf(0));
}else{
mTv.setText(String.valueOf(iPoint));
}
}
#Override
public void onStartTrackingTouch (SeekBar seekBar){
}
#Override
public void onStopTrackingTouch (SeekBar seekBar){
}
});
}
}

Changes the text of a textview from a new event [Android]

I can't change the text of text_view_cantidad_resultados of aTextView` from an event. This is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resultados_busqueda);
text_view_cantidad_resultados = (TextView) findViewById(R.id.text_view_cantidad_resultados);
viewPager = (ViewPager) findViewById(R.id.view_pager_cards_resultados);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d("onPageSelected: ", String.valueOf(position));
/*Here is where a want to changes the text of
text_view_cantidad_resultados.
Something like
text_view_cantidad_resultados.setText(position);
*/
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
I know that is because that object is not defined in that instance of the class (in the event), but a have no idea of how can I do it.
TextView will accept only int value as resource, so convert your int to String add it will work,
Ex:
text_view_cantidad_resultados.setText(position + "");
or
text_view_cantidad_resultados.setText(String.valueOf(position));

Adding multiple seekbar values together in a textview

Having trouble adding values together from multiple Seekbars. Trying to send the total to a total_view but it keeps showing up as 0. Im using two seekbars and the total should change depending on the movement on the bar so Im using onSeekChangeListener
public class CalculateGradeActivity extends Activity {
TextView text_view; //shows percentage
TextView text_view2; //shows percentage
TextView total_view; //show total from text_view and text_view2
SeekBar seek_bar1;
SeekBar seek_bar2;
int exam_grade;
int homework;
int total;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
seek_bar2 = (SeekBar) findViewById(R.id.seekBarHomework);
text_view = (TextView) findViewById(R.id.percentageSeekbar1);
text_view2 = (TextView) findViewById(R.id.percentageSeekbar2);
total_view = (TextView) findViewById(R.id.numeric_grade_id_output);
seekbar();
}
public void seekbar() {
int progress = seek_bar1.getProgress();
text_view.setText(String.valueOf(progress));
int progress2 = seek_bar2.getProgress();
text_view2.setText(String.valueOf(progress2));
total = exam_grade + homework;
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view.setText(String.valueOf(progress) + "%");
exam_grade = progress;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
seek_bar2.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view2.setText(String.valueOf(progress)+"%");
homework = progress;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}
}
Every time you change a value you have to recalculate the total before you display it.
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view.setText(String.valueOf(progress) + "%");
exam_grade = progress;
total = exam_grade + homework;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
seek_bar2.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view2.setText(String.valueOf(progress)+"%");
homework = progress;
total = exam_grade + homework;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
ja

Categories