I am working on a fragment that holds a seek bar, the seek bar value is updated in the editText element if used . User can also update seekbar by typing in the value on the editText.(this all works, until i implemented the clearing of the previous value)
the seekbar is defaulted to 500 (midpoint) and the textView shows 500. As the seekbar progress changes, the value in the editText matches it. (this kind of works)
When the user inputs a new value by selecting the EditText element I wanted the 500 (or what ever value) to disappear for the new input. (this works).
The problem is: every time I enter a new value on the softkeyboard and select the tick,the new value just clears, the softkeyboard still stays up and the progress bar or the edittext is not updated. What have done wrong?
SEEKBAR - Updating EditText
private int radius = 500;
//START SEEKBAR DISTANCE
distControl=(SeekBar)view.findViewById(R.id.sb_loc_dist_set);
txtInMaxMiles=(EditText)view.findViewById(R.id.txt_max_num_input);
distControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
int progressChange = 0;
@Override
public void onProgressChanged (SeekBar seekBar,int progress, boolean fromUser){
radius = progress;
txtInMaxMiles.setText(String.valueOf(radius));
}
@Override
public void onStartTrackingTouch (SeekBar seekBar){
distControl.setProgress(radius);
}
@Override
public void onStopTrackingTouch (SeekBar seekBar){
txtInMaxMiles.setText(String.valueOf(radius));
}});//END: SEEK BAR DISTANCE
Edit Text - updating progress bar
txtInMaxMiles.addTextChangedListener(new TextWatcher() {
String txtIn;
@Override //Before screen fully loads
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// txtIn = txtInMaxMiles.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
txtIn = txtInMaxMiles.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
txtIn = txtInMaxMiles.getText().toString();
if (txtIn == null || txtIn.trim().equals("")) {
radius = 0;
}
if (radius < 1 || radius > 1000) {
txtInMaxMiles.setError("Please input a number between 0 and 1001");
} else {
txtInMaxMiles.setError(null);
distControl.setProgress(Integer.parseInt(s.toString()));
// txtInMaxMiles.setText(Integer.toString(radius));
}
}
});
On Click EditText element
txtInMaxMiles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtInMaxMiles.setText("");
}
});
return view;
I think this is a quickfix, but im just missing something so any help would be appreciated.
Ok, Managed to work it out.This time i didn't hardcode the radius (naughty), made the code cleaner. However even after doing this....OnClickStill didnt work. So i tried another approach where the text is highlighted and replaced as the user inputs the new value in, this thread was useful: Select all text inside EditText when it gets focus However, I still tried onClick...and it didnt work, even though it was a step closer. In the end Lukas Batteau saved the day on this thread Android EditText onClickListener And instead of using onClick I changed to OnTouchListener.
txtInMaxMiles.setSelectAllOnFocus(true);
txtInMaxMiles.addTextChangedListener(new TextWatcher() {
int txtIn;
@Override //Before screen fully loads
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
txtInMaxMiles.clearFocus();
txtInMaxMiles.requestFocus();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
txtIn = Integer.parseInt(txtInMaxMiles.getText().toString());
if (txtInMaxMiles.getText().toString() == null || txtInMaxMiles.getText().toString().trim().equals("")) {
}
if (txtIn < 1 || txtIn > 1000) {
txtInMaxMiles.setError("Please input a number between 0 and 1001");
} else {
txtInMaxMiles.setError(null);
distControl.setProgress(Integer.parseInt(s.toString()));
}
}
});
txtInMaxMiles.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
txtInMaxMiles.clearFocus();
txtInMaxMiles.requestFocus();
}
return false; // return is important...
}
});
return view;
}
Ok, so I am still very much a novice, so any clear up coding advice will be appreciated, and if anyone knows the reason how onClickListener did not work please let me know, as i have no clue why.