Press Enter to change EditText - android

My problem is that I have a login screen that contains 2 EditText views. When I enter the username and click Enter, it adds more lines, but I want it to change to the EditText password instead. Can someone please help me?
public void onClick(View v){
final TextView user = (TextView)findViewById(R.id.tnome);
final TextView pw = (TextView)findViewById(R.id.tpw);
if (user.getText().toString().equals("") || pw.getText().toString().equals((""))) {
Toast.makeText(MainActivity.this, "Têm de Preencher todos os Campos", Toast.LENGTH_SHORT).show();
user.requestFocus();
}else{
if (user.getText().toString().endsWith(" ")){
user.setText(user.getText().toString().substring(0,user.length()-1));
}
user.setText(user.getText().toString().toLowerCase());
if (user.getText().toString().equals("admin") && pw.getText().toString().equals("admin")){
startActivity(new Intent(MainActivity.this,Home.class));
user.setText("");
pw.setText("");
}
user.setOnKeyListener(new View.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
user.clearFocus();
pw.requestFocus();
return true;
}
return false;
}
});
pw.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
// check for username - password correctness here
return true;
}
return false;
}
});
}
}

Using the android:inputType="text" (or another value) to limit input text to one line.
Usually, when input controls are laid out in a layout file (resx), pressing ENTER will cause focus to go to the next field. You shouldn't need any custom handling code to have this work (i.e. you shouldn't need code on the username field to make ENTER set focus to the password field).
You can use the <requestFocus /> tag to force focus to a field when the page is loaded.
<EditText
android:id="#+id/etUserName"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:focusable="true"
>
<requestFocus />
</EditText>
<EditText
android:id="#+id/etPassword"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:cursorVisible="true"
android:hint="Password"
/>
The most likely reason that the wire-up of the listener isn't working is that the code is only run when a button is clicked (it's in an onClick function). Perform the wire-up for the password (pw) field during page initialization instead - onFinishInflate() is a good spot.
You might, however want to wire-up a key listener to make an ENTER press in the password field submit the data (or do validation).

Related

EditText OnKeyListener with setFocus not working as per expected

I am using an Android device with physical hardware keyboard.
I have two EditTexts (Part No and Qty) and Two Buttons (Reset and Save) on screen.
After user key in Qty, they want to press Enter key from physical keyboard to trigger "Save" function.
My current Save function will save the data to file and clear the data on screen and set the focus to Part No.
It seems like my onKeyListener didn't return True once it was consumed and it trigger another Enter key action to move my cursor to Qty field (by right, it should stay in Part No field).
I thought return True will indicate this Enter key has been consumed, am I understanding correct?
My two EditText are put directly under ConstraintLayout (not under any sub-layout).
Any idea on where my code goes wrong?
Below is my piece of code for layout.
<EditText
android:id="#+id/etPart"
android:layout_width="200dp"
android:maxLength="20"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:maxLines="1"
android:inputType="text"
android:imeOptions="actionNext"
app:layout_constraintLeft_toRightOf="#+id/tvPart"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="#+id/etQty"
android:layout_width="100dp"
android:maxLength="5"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:inputType="number"
android:maxLines="1"
android:imeOptions="actionDone"
app:layout_constraintLeft_toRightOf="#+id/tvQty"
app:layout_constraintTop_toBottomOf="#id/etPart"
android:nextFocusDown="#id/etPart"/>
Below is my Java code.
This is in onCreate method.
etQty.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER){
saveData(null);
// I tried to use return true here, sames result, no different.
}
return false;
}
});
This is the functions.
public void saveData(View view) {
// check and save data to file logic.
// ...... etc.
mProductViewModel.insert(product);
resetView(null);
}
public void resetView(View view) {
etPart.setText(null);
etQty.setText(null);
// I am expecting the focus will stay in Part No box but it is not. It will jump to Qty field.
etPart.requestFocus();
}
After checking with the specialist, I came to understand that I also need to handle Action Up (Key Up) too.
Below is the working code.
etQty.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP){
saveData(null);
return true;
}
return false;
}
});
This is usually caused by soft keyboard, can you try this code?
etPart.requestFocus();
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.restartInput(etPart);
imm.showSoftInput(etPart, 0);

AutoCompleteTextView enter press

In EditText android:nextFocusDown="#+id/fuelfrom" will goes to next field when press done, similarly, how can I make a AutoCompleteTextView goes to next field when pressing enter key...
Use
android:singleLine="true"
it will show focus on next field on pressing enter.
EDIT:
try this one
android:imeOptions="actionNext"
use this.
<EditText android:imeOptions="actionDone"
android:inputType="text"/>
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Here you can write the code");
}
return false;
}
});
should return true in the if clause to say you've handled i

Soft keyboard, edittext setImeActionLabel() isn't working?

I am using this code in XML file for edit text.
<EditText
android:id="#+id/et_login_password"
android:layout_width="match_parent"
android:layout_height="#dimen/fragment_login_views_height"
android:layout_below="#+id/et_login_email"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
android:text="Vidcode2015"
android:textColor="#color/primary_dark"
android:textColorHint="#android:color/tertiary_text_light"
android:textSize="#dimen/fragment_login_views_text_size" />
And in java file i am setting edittext properties.
et_password.setImeActionLabel("Login",EditorInfo.IME_ACTION_SEND );
et_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
performLogin(et_user.getText().toString(), et_password.getText().toString());
handled = true;
}
return handled;
}
});
Android only provide with specific labels through imeOptions on edit text to set on softkeyboard button, like done or send e.t.c. User cannot set his/her own label like login or abc e.t.c

Not setOnKeyListener nor setOnEditorActionListener working unexpectedly

i am trying to do some thing when user press enter button in the EditText (It is multi lines editText means user can write more than one lines)
simply i want to detect either the enter key pressed
the code used is given below
<EditText
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:hint="#string/hint"
android:padding="5dp"
android:scrollbars="vertical"
android:textColor="#android:color/black"
android:textCursorDrawable="#null"
android:textSize="22sp" />
and listener is
mBodyText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((keyEvent.getAction()== KeyEvent.ACTION_UP)) {
if(i == KeyEvent.KEYCODE_ENTER){
Toast.makeText(getApplicationContext(), "neter presed", Toast.LENGTH_LONG).show();
return true;
}
}
return false;
}
});
but it do not show any thing when i press but show when i long press enter key
and setOnEditorActionListener is
mBodyText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent.getAction()== KeyEvent.ACTION_UP)) {
if(i == KeyEvent.KEYCODE_ENTER){
Toast.makeText(getApplicationContext(), "neter presed", Toast.LENGTH_LONG).show();
return true;
}
}
return false;
}
});
it shows nothing when i press enter key and application crashes when i long press the enter key
please help me i am trying it from long time
Disappointed no one there to Answer my question but i solved it on my own resources
setOnKeyListener was not working because the some of the android devices do not support this but most of them do support
and setOnEditorActionListener caused crashes because i am trying to get data from database and show it in my EditText view on the activity starts
thanks all especially #isma3l

Recommendations on the use of an EditText

I need a way to take text from a "EditText" as soon as the user presses "Enter" and it should also call a method if it loses focus.
I created EditText like this:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button2"
android:ems="10" >
To receive key presses your EditText needs to have focus:
editText1.setFocusableInTouchMode(true);
editText1.requestFocus();
then set the onkeylistener():
editText1.setonKeyListener(new OnKeyListener(
#override onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_ENTER){
// what you need to do when Enter is pressed
return true;
}else return false;
}
For focus lost, you need to implement the View.onFocusChange interface and check that hasFocus is false
Use an EditText action listener. Refer to this question, it's probably the same: Handle Enter in EditText
For detecting loss in focus:
if(!(et.isFocused)){
//call your method here
}

Categories

Resources