if we have two edittextviews and change the value in one edittextview the value of the other automatically change dynamically.For example if i have a currency converter and when i try to put the dollar value the other edittext value change with respect to dollar value at the same time.
In this case, you need to add a TextChangeListener to the first edittext field from which second edittext is taking its input.
firstEdText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String input = charSequence.toString(); // This is the text entered in your firstEdittext
secondEdText.setText(charSequence);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Related
I need to insert a focus shift from one editText to another editText after a certain logic in my application.
example:
in a credit card number editText have a listener when the length of the value reaches 16 it skips to the expiration month editText ...
My code:
numberCard.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.length()==16){
// how to make it jump to another editText?
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
If your second EditText is editText2 the you could do ediText2.requestFocus()
I have 4 edittexts that act as a "enter your pin" field with each edittext set to only be able to take a single number before requesting focus to the next edittext. If I enter a number for the 1st pin, it then request focus to the 2nd, and then 3rd, etc.
What I am trying to do now is, if the user clicks on the 2nd edittext or 3rd and the 1st field has not been entered, it should go back and focus on the 1st field.
The edittext fields
I tried to do this using an onTouchListener and what I noticed is that after clicking on the 2nd edittext first instead of clicking on the left/1st edittext, I can see the focus going back to the first edittext but then immediately go back to the 2nd edittext almost a split second and ultimately still remain focus on the 2nd edittext.
I also tested using onClickListener. It does work but it takes two clicks. So I'll click on the 2nd edittext, nothing happens, and then I click it again which then moves the focus back to the 1st edittext but I need it to happen on the first click.
Here is part of the code
pin1EditTxt.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(pin1EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin2EditTxt.requestFocus();
}
}
});
// pin2EditTxt.setOnTouchListener(new View.OnTouchListener() {
// #Override
// public boolean onTouch(View v, MotionEvent event) {
// if(pin1EditTxt.length() == 0){
// pin2EditTxt.clearFocus();
// pin1EditTxt.requestFocus();
// }
// return false;
// }
// });
pin2EditTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(pin1EditTxt.length() == 0){
pin1EditTxt.requestFocus();
}
}
});
pin2EditTxt.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(pin2EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin3EditTxt.requestFocus();
}
}
});
pin3EditTxt.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(pin3EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin4EditTxt.requestFocus();
}
}
});
pin4EditTxt.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) {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
editTxtWrapper.requestFocus();
}
});
I guess you should be using pin1EditTxt.getText().toString().length()==0 instead of pin1EditTxt.length()==0 inside onclick.
I have an EditText that will put this sets of value
58.44,44.2 or even negative like -58.44,-44.2
how can I prevent it from surpassing between -100 and 100 i tried this link but no joy.
I wanna make my EditText to type between -100 and 100 only if surpasses then dont continue typing.
You can add the condition like this
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.toString().length() > 0) {
String value = s.toString();
Double numericValue = Double.parseDouble(value);
if (numericValue < -100 || numericValue > 100) {
editText.setText("");
}
}
}
});
Set your EditText inputType as
android:inputType="numberDecimal|numberSigned"
If you want to solve programmatically, TextWatcher is a good option.
Inside onTextChanged(), Check if your changed CharSequence s, matches you need, otherwise replace it.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Using a regex pattern you can handle the numbers between -99.99 and 99.99
^[-+]?[0-9]{1,2}\.[0-9]{1,2}+$
Update 2:
For >=-100 and <=100, use this regex:
^[-+]100|^[-+]?[0-9]{1,2}.[0-9]{1,2}+$
Below sample, checks the text and enables/disables the button if matches or not.
EditText mEditText;
Button mButton;
final static String mRegex = "^[-+]100|^[-+]?[0-9]{1,2}\.[0-9]{1,2}+$";
boolean inputMatches;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = findViewById(R.id.editText);
mEditText.addTextChangedListener(new InputController());
mButton = findViewById(R.id.button);
mButton.setEnabled(inputMatches);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this," Input matches!", Toast.LENGTH_SHORT).show();
}
});
}
public class InputController implements TextWatcher {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
String input = mEditText.getText().toString();
inputMatches = input.matches(mRegex);
}
}
Update:
As per your request on clearing Edittext.. There are multiple ways to do it. You can either follow other answers above or you can use my above answer if it fits with you and instead using TextWatches, you can use OnFocusChangeListener. This is better if you have multiple Edittexts. When your one Edittext loses focus, then this code will trigger to see if it matches the regex pattern. If no match, clears it.
Remove above TextWatcher, follow bellow:
Implement OnFocusChangeListener in your Activity
MainActivity extends AppCompatActivity implements View.OnFocusChangeListener
Override its onFocusChange method.
#Override
public void onFocusChange(View view, boolean b) {}
Inside onFocusChange do your checks.
#Override
public void onFocusChange(View view, boolean b) {
if(!b){ // Lost focus
String text = ((EditText)view).getText().toString();
if(text.matches(mRegex)){
// It matches
Toast.makeText(MainActivity.this, "Input matches!!!", Toast.LENGTH_SHORT).show();
}
else{
// No match - clear Edittext
((EditText)view).setText("");
Toast.makeText(MainActivity.this, "Input doesn't match", Toast.LENGTH_SHORT).show();
}
}
}
Update 2
To include -100 and 100, use this regex pattern:
^[-+]100|^[-+]?[0-9]{1,2}.[0-9]{1,2}+$
I want to create two editable EditText in android. When the user is entering data in one EditText, the other EditText automatically becomes read-only and shows the result of that entered data.
Try The Following Code
Consider two EditText e1 and e2
e1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
e2.setEnabled(false);
return false;
}
});
use this code. And apply your logic, first check if this EditText is empty the call this code
editText.setEnabled(false);
Suppose Your editText is empty then check like this
if(editText.getText().toString.isEmpty()){
editText.setEnabled(false);
}
First you set a :-
edittext.setFocusable(false);
edittest.setFocusableInTouchMode(false);
OR
set android:focusable="false" in XMl file
<EditText
android:id="#+id/EditTextInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:gravity="right"
android:cursorVisible="true">
</EditText>
Then in edittext's OnStateChangeListener you going to android:focusable="true" your next Edittext
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() != 0){
edittext2.setFocusable(true);
edittest2.setFocusableInTouchMode(true);
}
}
});
Hope this helps you.
Use this and in onTextChanged set the data in editText you will get the your desired results.
yourEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
setEnabled false or true in afterTextChanged. do whatever you want according to your need
You can try something like this:
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editText2.setEnabled(false);
editText2.setText(editText1.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Set text watcher to your editText, and make them enabled/disabled when one of them is typed.
It should be ok in this way.
Try this...
when the user wants to put data in one editText the other
automatically becomes read-only dynamically to show the result
Use text watcher to your EditText, and make other edit text enabled/disabled when user typed on first EditText
EditText editText1 = (EditText)findViewById(R.id.text1);
EditText editText2 = (EditText)findViewById(R.id.text2);
editText1 .addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText2.setEnabled(false);
//calculate and display result of that entered data
editText2.setText("your result");
}
});
I am creating an Android Application which gets the text from Input (EditText) and sets the text of Preview (TextView) to the input based on when the user is typing. I know that there is a setOnKeyListener, however that is for specific keys, is it possible to detect any key?
Here is a preview of the App.
Thanks in advance. :)
You can use the TextWatcher class to implement this.
final TextView preview = (TextView) findViewById(R.id.preview);
EditText input = (EditText) findViewById(R.id.input);
input.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
preview.setText(editable);
}
});