I have a SeekBar and EditText.I would like to create a % slider that when the user slides the Seekbar, it adjusts the EditText field.
i tried this:
mInterestRateEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if (i >= 0 && i <= 100) {
mInterestRateSeekBar.setProgress(50);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
mInterestRateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mInterestRateEditText.setText(progress + "");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
but all it does is the second i try sliding the slider, it locks the EditText to 0, and I cannot move the slider?
As I already stated in my comment, you are creating a loop. Changing the Seekbar, changes the text. This changes the seekbar again.
Simple solution: work with a boolean.
boolean changedProgrammatically;
mInterestRateEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if(!changedProgrammatically){
if (i >= 0 && i <= 100) {
mInterestRateSeekBar.setProgress(50);
}
}else{
changedProgrammatically = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
mInterestRateSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
changedProgrammatically = true;
mInterestRateEditText.setText(progress + "");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Related
I have 2 EditText in my activity,lets say editText1 ,editText2 and A Double Variable d=200
I want when user change/insert value for editText1 ,editText2 value will update in real Time as editText1*d
Also when user change/insert value for editText2 ,editText1 will be update in real time as editText2*d
I tried to use addTextChangedListener->onTextChangedbut it works fine for one Edit text,when I set this function for both editText then application crash
because its creating an infinite loop,how can I solve this problem?
update :bellow is my code
et1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Editable a = et1.getText();
Double aa = Double.parseDouble(a.toString())*100;
String aaa = aa.toString();
et2.setText(aaa);
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Editable b=et2.getText();
Double bb=Double.parseDouble(b.toString());
String bbb=bb.toString();
et1.setText(bbb);
}
#Override
public void afterTextChanged(Editable s) {
}
});
EditText et1, et2;
TextWatcher watcher1 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable a = et1.getText();
Double aa = Double.parseDouble(a.toString())*100;
String aaa = aa.toString();
et2.removeTextChangedListener(watcher2);
et2.setText(aaa);
et2.addTextChangedListener(watcher2);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
TextWatcher watcher2 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable b=et2.getText();
Double bb=Double.parseDouble(b.toString());
String bbb=bb.toString();
et1.removeTextChangedListener(watcher1);
et1.setText(bbb);
et1.addTextChangedListener(watcher1);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
et1.addTextChangedListener(watcher1);
et2.addTextChangedListener(watcher2);
i think you have to create textwatcher separately then in your TextWatcher_EdOne
TextWatcher TextWatcher_EdOne = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//disable editext2 textwatcher
et2.removeTextChangedListener(TextWatcher_EdTwo);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
///do your stuff
}
#Override
public void afterTextChanged(Editable s) {
// enable textwatcher on et2 again
et2.addTextChangedListener(TextWatcher_EdTwo);
}
}
and will do it for the other textWatcher , this way you will avoid infinite loop
You should implement a TextWatcher, It works pretty much like how it sounds. When the user changes (adds/removes text from) what is existing in the EditText, it will "Activate" this class. To test it out, you can use logs or set a breakpoint at any one of these statements.
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Your action here
edit = fromEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
}
}
and in onCreate, add this to connect your EditText to the TextWatcher
fromEditText.addTextChangedListener(new MainActivity.MyTextWatcher(fromEditText));
You can add a bunch of EditText's to a single TextWatcher. All you need to do is rinse and repeat. Just change the name of the respective EditText and add it to the TextWatcher. Be aware, that if you have some EditText change the text of another EditText, it can result in an infinite loop.
public class ActivityMain extends AppCompatActivity {
Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button_chatbox_send);
button.setEnabled(false);
tv= findViewById(R.id.edittext_chatbox);
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.toString().equals("")) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Call other method with string from text view as parameter
}
}
});
}
I added a TextChangedListener to my TextView to disabled the Button, while the text view contains no string. During runtime, after I entered a string into the TextView, the Button is still enabled, even if I deleted all the text. How do I solve this problem, the method which I use during on click can not work with an empty string?
Edit
Problem is solved ty.
Update your button in onTextChanged or after textChanged
tv.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
button.setEnabled(!TextUtils.isEmpty(s.toString())); // Update button here OR
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
button.setEnabled(count>0); // Update button here
}
});
move it to afterTextChanged
#Override
public void afterTextChanged(Editable editable) {
button.setEnabled(!TextUtils.isEmpty(editable.toString()));
}
You should disable the button inside the onTextChanged method of the TextWatcher -
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
button.setEnabled(charSequence.toString().length() > 0);
}
In one activity of my app I have 5 EditText fields. However, I want etPlayer5 only to be enabled when the other 4 EditTexts are not empty. I wrote this little code to achieve this:
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer3 = (EditText) findViewById(R.id.etPlayer3);
etPlayer4 = (EditText) findViewById(R.id.etPlayer4);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
if (namePlayer1.matches("")||namePlayer2.matches("")||namePlayer3.matches("")||namePlayer4.matches("")) {
etPlayer5.setEnabled(false);
etPlayer5.setFocusable(false);
}
else {
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
Of course this code doesn't work in real time. What do I need to do that when the last of the EditTexts 1 to 4 is filled in, etPlayer5 is automatically set enabled without using a button or something?
Thank you!
You can have textChangeListener with all 4 Edittexts and keep boolean against all edittext. Update booleans to true when you get call of onTextChange and see if the string length is one in this edittext and further check remaining booleans are also true.This is the trick. Now this is the time to make the edittext enable.Hope that helps.Below is the code for above explanation
public class MainActivity extends AppCompatActivity {
EditText et1, et2, et3, et4, et5;
boolean firsEditText, secondEditText, thirdEditText, fourthEditText, fifthEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
et1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
firsEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
firsEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
secondEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
secondEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et3.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
thirdEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
thirdEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et4.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
fourthEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
fourthEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean checkAllTrue() {
if (firsEditText && secondEditText && thirdEditText && fourthEditText)
return true;
return false;
}
}
You could do something like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean player1Changed = false, player2Changed = false;
setContentView(R.layout.main);
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
etPlayer1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player1Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
etPlayer2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player2Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
so i want to take progress from seek bar and use it in button method, but its always 0(counter),doesnt work. I tried seekB.getProgress();,seekbar works great...
int counter;
seekB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//Randomise Button
but.setOnClickListener(new View.OnClickListener() {
int Plength = 6;
char[] i = new char[Plength];
List<Character> pass = new ArrayList<>();
#Override
public void onClick(View v) {
for (int x = 0; x < Plength; x++) {
pass.add(azL[rand.nextInt(azL.length)]);
pass.add(nzL[rand.nextInt(nzL.length)]);
pass.add(AZL[rand.nextInt(AZL.length)]);
i[x] = pass.get(rand.nextInt(pass.size()));
}
textV.setText(String.valueOf(i));
pass.clear();
}
});
if i do textV.setText(String.Valueof(counter)); is going to display 0 all the time.i dont know how to fix it
Try to change it :
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(progress));
}
to
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(new Integer(progress)));
}
My issue is when first EditText length greater than 10 then second EditText will be enabled and second EditText length greater than 8 then after Button will be enabled what is the problem in my code? Please any one can solve this problem.
public void loginPage(View v) {
editText = (EditText) findViewById(R.id.username);
editText1 = (EditText) findViewById(R.id.pasword);
username = editText.getText().toString();
password = editText1.getText().toString();
btn = (Button) findViewById(R.id.login1);
editText1.setEnabled(false);
btn.setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 10) {
editText1.setEnabled(true);
btn.setEnabled(false);
}
}
});
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 8) {
btn.setEnabled(true);
}
}
});
}
Use this for the equal or greater than 10 word
paste this method in OnCreate() method
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() >= 10)
editText1.setEnabled(true);
else
editText1.setEnabled(false);
}
});
and for the button for 8 words
paste this method in OnCreate() method
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() >= 8)
btn.setEnabled(true);
else
btn.setEnabled(false);
}
});
enjoy coding .........
You need to use Text Watcher with Edit Text.
Try this code.
public class Login extends AppCompatActivity {
EditText editText, editText1;
Button btn;
String username, password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginPage();
}
public void loginPage()
{
editText = (EditText) findViewById(R.id.username);
editText1 = (EditText) findViewById(R.id.password);
username = editText.getText().toString();
password = editText1.getText().toString();
btn = (Button) findViewById(R.id.login1);
editText1.setEnabled(false);
btn.setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 10) {
editText1.setEnabled(true);
btn.setEnabled(false);
}
}
});
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 8) {
btn.setEnabled(true);
}
}
});
}
}
EDIT :
Create new function to Change from Login page on Button Click.
public void changeLoginPage(){
Intent intentNew = new Intent(First.this,Destinatin.this);
startActivity(intentNew);
}
and add this on onCreate method.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLoginPage();
}
});