how to dynamically change textview location? - java

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

Related

My seekbar doesn't work in a BottomSheetDialog

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

Updating int value from Seekbar Java

I've this Seekbar and an int defined outside the seekbar. The value of the int is the seekbar progress. All is fine, except that I want the value of the int to change as the progress of the seekbar changes. Right now, it gets the value from the seekbar only when the program is launched.
int tableNumber = seekBar.getProgress();
SeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setMin(1);
seekBar.setMax(20);
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Number", Integer.toString(i));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You have to update the tableNumber variable every time the seekbar progress changes.
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Number", Integer.toString(i));
tableNumber = i; // add this line
}
declare tableNumber outside method and set its value in onProgressChanged
int tableNumber = 0; // declare above method
public void someInitiatingMethodMaybeOnCreate(){
SeekBar seekBar = findViewById(R.id.seekBar);
tableNumber = seekBar.getProgress(); // initial set
seekBar.setMin(1);
seekBar.setMax(20);
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Number", Integer.toString(i));
tableNumber = i; // set current progress
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}

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 !

Categories

Resources