How do you change an EditText based on another EditText? - android

I have two EditTexts. Is there a way to code the program in a way that changing one EditText would change the other?
For example, if chicken is 200 calories per 100 grams, then changing the EditText of chicken to 100 will result in an auto update of 100 grams to 50 grams, without clicking anything.

You could install an OnEditorActionListener on your EditText and perform the action when the user stops typing:
myEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// call some other function to test the value and
// set the result on the other EditText
return true;
}
return false;
}
});

Related

How to remove the hability to focus next EditText when we push the return key?

When I'm writing in an EditText, if I push the next/done key then the focus will move to the next EditText. How to forbid this behaviour (programmatically)?
Just setFocusable(true) to the mother view of your edittext
You may try this code :
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// do something with next printed on your editText
}
return true;
}
});
You can manage cases for different option like other action(Done/Next/Search etc.) too.

input method next not focusing on an edit text

I have 5 edit texts where user enters his options, I keep first two visible and other 3 visibility gone. If user wants to enter option 3 I make it visible by pressing action next key on keyboard while user finishes typing on option 2. But the problem is it doesnt focus on edit text 3. Now to experiment visibility of edittext 3 from gone to invisible, action next method works well. Is there a way to make input next method work when view's visibility turns gone to visible?
I am using this code to make focus appear on edittext 3 which is not working in case of edittext visibility turning visible from gone. Same code works well in case of edittext visibility turning visible from invisible.
option2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_NEXT){
option3.setVisibility(View.VISIBLE);
option3.requestFocus();
}
return false;
}
});
How to make focus on button3 :
button2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
button3.setVisibility(View.VISIBLE);
button3.requestFocus();
}
return false;
}
});

Text watcher in android works once only

I have written a key listener on a edit text in android.
Following is my code:
textview.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter"
// button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on Enter key press
if (textview.getText().toString().length() == 15) {
textvalue = textview.getText().toString();
textview.setText(replacecardformat());
textview.clearFocus();
Log.e(""TAG, "Executed");
return true;
} else {
return false;
}
}
return false;
}
});
However the log statement is executed only once.Is some problem in my return statement.
Two observations:
if you need listener on each text change use view.addTextChangedListener(TextWatcher). Text Watcher has three methods: one fired before, one after, and one on text change. I suppose that this is what you are looking for. More details and tutorial you can find here
is your textview an TextView or EditText? I am asking since only EditText can receive keyboard input. however TextView can have such listener too. Because its text can also change (see documentation here).

Android: how to ignore Enter key so the focus won't change

I have an Activity with two EditText fields. So when I press enter in the first one the second one becomes focused.
However I want to disable Enter key in the first EditText sometimes (for example when I think that the user haven't made a proper input into the first EditText).
I' ve overwriten onKeyDown for the 1st EditText returning true when key event is KEYCODE_ENTER but that doesn't help.
What shall I do?
How about adding an OnFocusChangeListener? When your EditText looses focus, you can do validation and can do a requestFocus for the "right" field.
Just by adding an editorActionListener:
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if(isValidContent(editText1.getText().toString()){
editText2.requestFocus();
return true;
} else {
....
}
}
return false;
}
});

How can I use the android landscape keyboard buttons?

I have an activity with two EditTexts. If, in landscape mode, I select the first EditText, the keyboard that is displayed has a "next" button which should allow me to type in the second EditText. Likewise, the second EditText has a "done" button, which I would like to handle for completing the activity. How can I handle the selection of these buttons?
There is fairly little documentation on how to solve this issue. I found a good solution here. Basically, you can add an IMEoption for each of the EditTexts:
For the first one:
android:imeOptions="actionNext"
For the second one:
android:imeOptions="actionDone"
To handle these in the code, try something like this:
EditText departureAddress, destinationAddress;
departureAddress = (EditText)findViewById(R.id.departure);
//Set the action of the "next" button to bring destinationAddress to focus
destinationAddress(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
destinationAddress.requestFocus();
}
return true;
}
});
destinationAddress = (EditText)findViewById(R.id.destination);
//Set the action of the "done" button to handle the map query
destinationAddress.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//Handle map query
}
return true;
}
});

Categories

Resources