My seekbar doesn't work in a BottomSheetDialog - java

I created a BottomSheetDialog with a checkbox and a SeekBar. I want that if the checkbox is checked, the seekbar can be used. My code works but only if i use setContentView(MyLayout), but that makes the layout appair on screen and i want that it works on the dialog. How can i make it work without that? My code here:
Ps: I am sorry for my english, but it isn't my first language.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setOverflowIcon(getDrawable(R.drawable.ic_baseline_more_vert_24));
ImageButton menu_filtri = findViewById(R.id.button_filtri);
menu_filtri.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this,R.style.BottomSheetDialogTheme);
View BottomSheetView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_bottom_sheet,(LinearLayout)findViewById(R.id.bottom_sheet_container));
bottomSheetDialog.setContentView(BottomSheetView);
bottomSheetDialog.show();
setContentView(R.layout.layout_bottom_sheet);
range_distanza = (CheckBox) findViewById(R.id.next_checkbox);
seekbar = (SeekBar) findViewById(R.id.range_seekbar);
set_seekbar();
}
});
}
public void set_seekbar() {
range_distanza.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
seekbar.setEnabled(ischecked);
}
});
}

In Kotlin: Use the below code for return value after you seek the bar.
seekBar.setOnSeekBarChangeListener(new AppCompatSeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Your logic
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Related

Android Java Sum of values from Two SeekBar's using setOnSeekBarChangeListener or Button

being new in Android I'm trying to just simply add (sum) adjusted values from two SeekBars using setOnSeekBarChangeListener or using Button.
The project below is work fine but the sum still is 0. I can't find a solution. Thanks for your help. Usually, I'm using Spring Boot and Vaadin and it is so simple :)
private int progress1 = 0;
private int progress2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewMain;
Button button;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
Button buttonSum = findViewById(R.id.button);
SeekBar seekBar = findViewById(R.id.seekBar);
final TextView textViewSeekBar = findViewById(R.id.textView2);
SeekBar seekBar2 = findViewById(R.id.seekBar2);
final TextView textViewSeekBar2 = findViewById(R.id.textView3);
seekBar.setMax(600);
seekBar.setProgress(5);
seekBar2.setMax(900);
seekBar2.setProgress(5);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress1 = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
this.progress1 = progress1;
// getUpdateTotal();
//textViewSeekBar.setText(Integer.parseInt(String.valueOf(seekBar)));//todo not work
textViewSeekBar.setText(Integer.toString(progress1));
Log.i("Seeker1: ", Integer.toString(progress1));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress2 = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
this.progress2 = progress2;
//getUpdateTotal();
textViewSeekBar2.setText(Integer.toString(progress2));
Log.i("Seeker2: ", Integer.toString(progress2));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
buttonSum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSummaryTwoSeekBarValue(progress1, progress2);
Log.i("", getSummaryTwoSeekBarValue(progress1, progress2));
}
});
}
public String getSummaryTwoSeekBarValue(int progress1, int progress2) {
final TextView textViewMain_ = findViewById(R.id.textViewMain);
int total = progress1 + progress2;
String totalString = String.valueOf(total);
Log.i("", totalString);
textViewMain_.setText(String.valueOf(totalString));
return totalString;
}
}```
[enter image description here][1]
[1]: https://i.stack.imgur.com/HDx12.png
You are defining progress1 and progress2 in both the main class and in the listener, so the class variable will keep being 0, you have to use (your class name).this.progress1 = progress1; and same in the second seek bar to modify the main class one instead of the listener one
private int progress1;
private int progress2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewMain;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
SeekBar seekBar = findViewById(R.id.seekBar);
final TextView textViewSeekBar = findViewById(R.id.textView2);
SeekBar seekBar2 = findViewById(R.id.seekBar2);
final TextView textViewSeekBar2 = findViewById(R.id.textView3);
seekBar.setMax(600);
seekBar.setProgress(5);
seekBar2.setMax(900);
seekBar2.setProgress(5);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
getSummaryTwoSeekBarValue(progress1,progress2);
textViewSeekBar.setText(Integer.toString(progress1));
Log.i("Seeker1: ", Integer.toString(progress1));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
getSummaryTwoSeekBarValue(progress1,progress2);
textViewSeekBar2.setText(Integer.toString(progress2));
Log.i("Seeker2: ", Integer.toString(progress2));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public String getSummaryTwoSeekBarValue(int progress1, int progress2) {//int progress1, int progress2
this.progress1=progress1;
this.progress2=progress2;
final TextView textViewMain_ = findViewById(R.id.textViewMain);
int total = progress1 + progress2;
String totalString = String.valueOf(total);
Log.i("", totalString);
textViewMain_.setText(String.valueOf(totalString));
return totalString;
}
}```

Multiple seekbars - set listeneners more efficiently?

I have 12 seekbars in an activity. Each seekbar needs a listener. At the moment I am manually setting a listener for each seekbar, this doesnt seem particularly efficient.
Can anyone recommend a more efficient practice to set the seekbars???
Try something like this:
public class MainActivity extends AppCompatActivity {
SeekBar seekBar1;
SeekBar seekBar2;
SeekBar.OnSeekBarChangeListener mlistener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar1 = findViewById(R.id.seekBar_1);
seekBar2 = findViewById(R.id.seekBar_2);
mlistener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seekBar_1:
//do something
break;
case R.id.seekBar_2:
//do something else
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
seekBar1.setOnSeekBarChangeListener(mlistener);
seekBar2.setOnSeekBarChangeListener(mlistener);
}
If your SeekBars all require different interaction logic then obviously you will need to provide unique code to each of the objects. However, you can re-use listeners if each SeekBar is doing the same thing. For example:
public class SeekBarTest extends Activity{
private Context myContext;
public SeekBarTest(Context c){
myContext = c;
}
private void initialize(){
SeekBar seekbar1 = new SeekBar(myContext);
SeekBar seekbar2 = new SeekBar(myContext);
SeekBar seekbar3 = new SeekBar(myContext);
SeekBar.OnSeekBarChangeListener myListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//logic
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//logic
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//logic
}
};
seekbar1.setOnSeekBarChangeListener(myListener);
seekbar2.setOnSeekBarChangeListener(myListener);
seekbar3.setOnSeekBarChangeListener(myListener);
}
}
Instead of creating switch in onProgressChanged method you can send variables to SeekBar initiating method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
SeekBar volBar = initiateSeekBar(R.id.volume,R.id.volumeValue,0,1,"%");
SeekBar playSpeedBar = initiateSeekBar(R.id.autoPlay,R.id.autoPlayValue,100, 100,"ms");
}
private SeekBar initiateSeekBar(int barId, int textId, final int min, final int step, final String unit) {
SeekBar bar = findViewById(barId);
final TextView text = findViewById(textId);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int value = min + progress * step;
text.setText(value + " " + unit);
autoPlayDelayTime = value;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return bar;
}
You can just make a reference equality, like this:
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
seekBar?. let {
if (this.seekBar1 === it) {
...
}
if (this.seekBar2 === it) {
...
}}
...
}
and call outside:
seekBar1.setOnSeekBarChangeListener(this)
seekBar2.setOnSeekBarChangeListener(this)

I cannot get OnSeekBarChangeListener to work in Android code

I'm trying to use OOP in my Java/Android code, but I don't understand why OnSeekBarChangeListener is not working in my new class.
Is it prohibited to use a OnSeekBarChangeListener outside main activity?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
timescroller = (SeekBar) findViewById(R.id.seekBar);
TimeScroller Tm = new TimeScroller();
Tm.mp = mp;
Tm.ts = timescroller;
Tm.mTimeScroller();
timescroller.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b){
mp.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
mp.setVolume(0,0);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.setVolume(1,1);
}
});
}
And here is my class
package com.phillipvm.gridlayout;
import android.media.MediaPlayer;
import android.widget.SeekBar;
public class TimeScroller {
MediaPlayer mp;
SeekBar ts;
public void mTimeScroller(MediaPlayer _mp,SeekBar _ts){
mp =_mp;
ts = _ts;
ts.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b){
mp.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
mp.setVolume(0,0);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.setVolume(1,1);
}
});
}
}
i can't see the code where you are using the class!
you are calling Tm.mTimeScroller(); but in your class the method takes 2 parameters !

how to dynamically change textview location?

I have a textview that shows the value of a seeker bar. I want to display that textview on top of the scrubber/slider as it is pulled left and right by the user.
What code do I need to do this?
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
final TextView seekBarValue = (TextView)findViewById(R.id.seekervalue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
Just use TextView.setX(float X) (and if you need vertical movement) TextView.setY(float Y) inside your onProgressChanged method.
Just adjust the values depending on what you want.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.textView);
SeekBar sb=(SeekBar)findViewById(R.id.seekBar);
final float startpos=tv.getX();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv.setX(startpos+progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}

How do I disable my Seekbar in Android with a Switch?

I want to disable my Seekbar and buttons with a switch which is Powerswitch
Here is a code snippet:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
String sval = String.valueOf(i);
speakerVal.setText(sval);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// button - Favorite Channel ABC
cabc = (Button) findViewById(R.id.buttonABC);
cabc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Set Favorite
curChannel.setText("007");
}
});
and here is my switch code:
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(this);
more switch code:
public void onSwitchClicked(View view) {
final TextView PowerSwitch = (TextView) findViewById(R.id.PwrSwitchVal);
Switch sw = (Switch) view;
if (sw.isChecked()) {
PowerSwitch.setText("ON");
pwr = true;
} else {
PowerSwitch.setText("OFF");
pwr = false;
}
}
I know I can use the switch.isChecked(), but when I change the seekba1.setEnabled(false), I can't seem to get it to turn back on.
I haven't even tried to disable the buttons yet.
any help would be appreciated.
In your onSwitchClicked method, you should be calling seekbar.setEnabled in the if checked if-else statement. If checked seekbar.setEnabled(true) else seekbar.setEnabled(false)
try this.
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean arg1) {
if (arg1) {
seekbar1.setEnabled(true);
}
else{
seekbar1.setEnabled(false);
}
}
});

Categories

Resources