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);
}
});
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 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");
}
});
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) {
}
});
I have a Profile's form with some EditText like the name, address, city and so on. And I have a Button to save this data once you press it.
So I would like to know how can I check if there have been changes before make my app connect with the database and update it.
I've thought using a TextChangedListener() with some boolean on the onTextChanged() or afterTextChanged(), but I don't know exactly how to do that.
Any suggestion? Thank you very much!
I´ve tried the code below and it works for me. I have this right now:
public class ProfileActivity extends Activity {
TextView done_btn;
EditText local_court;
boolean hasChanges = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
done_btn = (TextView) findViewById(R.id.done_profile_btn);
local_court = (EditText)findViewById(R.id.input_local_court);
done_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if(checkChanges()) {
//saveChanges();
finish();
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
}
});
public boolean checkChanges() {
local_court.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hasChanges = true;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
return hasChanges;
}
}
I guess I should make the same with the other EditText.
Thank you everyone!
If your goal is just to check if something has changed at all, just check the current EditText value to what was before.
But if you want to know if the text has been touched, even reverted back to original, do as below.
EditText etext = (EditText) view.findViewById(R.id.editTxt);
etext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s,
int start, int before,
int count) {
//Set your boolean here to true
}
#Override
public void beforeTextChanged(CharSequence s,
int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
}
);