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){
}
});
}
}
Related
I was working in a very simple music player with a seek bar. But I got a problem with playing music,it doesn't work correctly every second - it backwards a while, then remain again like it's a buggy.
I know why but I can't solve it.
When the progress bar moves forward, the music recalibrates and vice versa, when the music moves forward, the progress bar recalibrates.
It's probably because of that that every time it goes back.
But I tried several things and still have the same problem.
Another problem is that when the music is paused, the progress bar goes in the opposite direction until it reaches zero. It doesn't stay where it was before the break.
The full code :
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private boolean bool = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.seekBar = (SeekBar) findViewById(R.id.sound_bar);
this.mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition() * seekBar.getMax() / mediaPlayer.getDuration());
}
}, 500, 500);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(seekBar.getProgress() * mediaPlayer.getDuration() / seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void playSound(View view) {
Button button = (Button) view;
if(bool) {
mediaPlayer.pause();
bool = false;
button.setText("Jouer le son");
} else {
mediaPlayer.start();
bool = true;
button.setText("Mettre en pause");
}
}
#Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}
#Override
protected void onStart() {
super.onStart();
if(bool) {
mediaPlayer.start();
}
}
}
Maybe use another thread to control the seekbar would help.
Take a look at this
I created a simple app to learn how to use sensor heart beat with one button and label here is my code:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
Button show;
TextView showHeartRate;
SensorManager sensorMgr;
Sensor heartRate;
String heartRateValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.button);
show.setOnClickListener(displayHeartRate);
showHeartRate = (TextView) findViewById(R.id.showHeartRate);
sensorMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
heartRate = sensorMgr.getDefaultSensor(Sensor.TYPE_HEART_RATE);
}
View.OnClickListener displayHeartRate = new View.OnClickListener() {
#Override
public void onClick(View view) {
showHeartRate.setText(heartRateValue);
}
};
#Override
protected void onResume() {
super.onResume();
sensorMgr.registerListener(this,heartRate,SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
super.onPause();
sensorMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
I add to the manifest:
<uses-permission android:name="android.permission.BODY_SENSORS"/>
the problem is that the onSensorChanged() is not called.
i check a lot of solutions here but i did not find anything
First, why would you expect it to ask permissions to use your camera if its heartrate sensor that you are trying to use?
Second, I think by the time you click 'display heartrate', you just don't have any data from the sensor yet (heartRateValue is empty), and when data finally comes you don't really update UI, you only update your state. What I suggest is to update your UI state on every sensor change, for example:
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
showHeartRate.setText(heartRateValue);
}
or you can avoid code duplication (even thought its only one line), create a setter and and call it from both click handler and sensor handler:
private void setHeartrate(String rate) {
showHeartRate.setText(rate);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
setHeartrate(heartRateValue);
}
View.OnClickListener displayHeartRate = new View.OnClickListener() {
#Override
public void onClick(View view) {
setHeartrate(heartRateValue);
}
};
Third, you are not using data from your sensor, look at SensorEvent, data comes in values array and you are only trying to displat sensor type, why? Try this:
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.values.length > 0 ? sensorEvent.values[0] : 0.0f);
showHeartRate.setText(heartRateValue);
}
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
i have to decrease a counter When a char is written , but my code is decreasing two chars Instead of one but Only When delete key is pressed , but if
if I press another key does not work
private Button send;
private TextView max;
private TextView msg;
int limit=140;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max = (TextView) findViewById(R.id.max);
send = (Button) findViewById(R.id.send);
msg = (TextView) findViewById(R.id.msg);
msg.setOnKeyListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
max.setText(String.valueOf(limit--));
return false;
}
The problem is that onKey is called twice, once for down and one for up.
You can use another method such as onKeyDown:
onKeyDown documentation
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
Or use a filter using onKey.
onKey gets fired twice. Once when the key is pressed down, and once when it is released.
Or you can try:
onKeyDown()
onKeyUp()
KeyEvent.getAction().
In this java image, you can see that the person made both a released and a pressed method. This picture is for java, but the concept for android is the same. Use the methods I gave you in that numbered list.
Otherwise, right now, you are listening for two events.
On the right is key pressed, and the left is key released:
If this was helpful, please mark as best answer. If you need more help, let me know!
Okey i have found the answer,
first of all msg is not a TextView, is a EditText
and then we can use addTextChangeListener.
thanks all of you who responds
private Button send;
private TextView max;
private EditText msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max = (TextView) findViewById(R.id.max);
send = (Button) findViewById(R.id.send);
msg = (EditText) findViewById(R.id.msg);
msg.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
max.setText(String.valueOf(140- (msg.getText().toString().length())));
if(msg.getText().toString().length()>=140){
send.setEnabled(false);
}else
send.setEnabled(true);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onClick(View view) {
}
I am creating an android application, a converter. Every time I am going to press any number, I want it to be displayed automatically on a textfield. I don't know how to use the keypress in android application. Is it just like using keypress in a simple java program, let's say ran on netbeans?
Um ...
public class MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(new EditText());
}
}
Your question is not very clear.. but i think you are saying that your layout contain a EditText and TextView, when user type in a number in EditText at same time the TextView should also set the same text at rumtime. if this is your requirement then you can do it as follows:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main_activity);
TextView tv = (TextView)findViewById(R.id.textview1);
EditText et = (EditText)findViewById(R.id.edittext1);
et.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
tv.setText(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
}
}