I've got a layout with two editexts. I need to set the second one focused and editable when the enter key is hit on the first one. I've got code for setting focus but I can't start typing when the second one gets the focus.
PS I also need edittext to be exactly one line height without possibility of making addtional rows. It works as well.
pss android 2.3
XML code:
<EditText
android:id="#+id/email"
style="#style/profileLoginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="#string/email" android:ems="10" android:lines="1" android:maxLines="1" android:ellipsize="end">
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
style="#style/profileLoginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPassword" android:hint="#string/password">
Java code :
((EditText) findViewById(R.id.email)).setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
((EditText) findViewById(R.id.password)).requestFocus();
return true;
default:
break;
}
}
return false;
}
});
You're overthinking this. You can specify the order of the EditText inputs right in the XML without doing anything in Java.
Use the android:nextFocusDown param to specify which field you'd like to get focus when the user hits the enter key.
android:inputType="text"
android:nextFocusDown="#+id/..."
worked for me
android:nextFocusDown
Worked fine for me
But removed the following Method only then its work:
editText1.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
editText2.requestFocus();
return false;
}
return false;
}
});
Besides adding the + in front of id:
android:inputType="text"
android:nextFocusDown="#+id/..."
I also had to add:
android:imeOptions="actionNext"
It seems to be bugged when running this code on the emulator. However, it works on phones
Related
This Question is no duplicate.
I want to check if enter on my keyboard is clicked.
This code is the most helpful for me:
edittext.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Toast.makeText(getActivity(), "it works", Toast
.LENGTH_SHORT).show();
return false;
}
});
But it only works with the keyboard of my pc and not on my phone.
I don't know weather it's important, but I am using a Fragment.
Here is my XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:id="#+id/edittext"
android:imeOptions="actionDone"
android:padding="10dp"/>
I looked at ALL These acticles and I tried every code, but nothing worked.
EDIT
I read a comment and so I want to add:
I am talking About Software Keyboard.
Is dependence on android:imeOptions="" of your EditText.
Example, android:imeOptions="actionDone":
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
UPD:
Add android:inputType="text" to your EditText in xml file
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:id="#+id/edittext"
android:inputType="text"
android:imeOptions="actionDone"
android:padding="10dp"/>
here' the code
I have two edit Text in a row in my lay out,and when I touch next on keyboard it does not change the focus.
the setOnEditorActionListener does not work till i add extra editText
<EditText
android:id="#+id/firstText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
/>
<EditText
android:id="#+id/secondText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
/>
code:
firstText.setOnNextActionListener(new CallBack<KeyEvent>() {
#Override
public void call(KeyEvent data) {
secondText.requestEditTextFocus();
}
});
Give one value into edittext in xml file ..
android:singleLine="true"
You can try these
android:nextFocusDown="#+id/secondText"
android:nextFocusLeft="#+id/secondText"
android:nextFocusRight="#+id/secondText"
android:nextFocusUp="#+id/secondText"
Show below three ways, you can got you want Using any one of them
inputType ="actionNext" attribute
or
android:imeOptions="actionNext"
or
android:singleLine="true" and
android:nextFocusDown="#+id/secondText"
on your .xml you can do this.
If you want to do it programmatically in your activity or fragment then try below:-
firstText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if (actionId == EditorInfo.IME_ACTION_NEXT) {
secondText.requestEditTextFocus();
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
I have tested with set Programmtically with changes secondText.requestEditTextFocus(); to secondText.requestFocus(); and its working fine.
Please show below Screenshotes
I am not new to the use of EditText's inputType but now I am having issue when setting android:inputType="textPassword" beside using android:imeOptions="actionDone"
Device used is: LG G4 Android v6.0
Here is my code:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_password"
style="#style/TextInputLayoutLoginTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_padding_2"
app:passwordToggleDrawable="#drawable/ic_password_visibility_18dp_selector"
app:passwordToggleTint="#color/white">
<com.cashu.app.ui.widgets.CustomEditText
android:id="#+id/et_activity_login_password"
style="#style/EditTextPasswordTheme"
android:layout_width="#dimen/edittext_width"
android:layout_height="wrap_content"
android:ems="12"
android:gravity="center_vertical"
android:hint="#string/activity_consumer_login_password_hint"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="#color/white"
android:textSize="#dimen/font_size_small" />
</android.support.design.widget.TextInputLayout>
My code for this EidtText:
mEtPassword.setOnEditorActionListener(new EditText.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) > 0 || actionId == EditorInfo.IME_NULL) {
login();
return true;
}
return false;
}
});
mEtPassword.addTextChangedListener(new LoginTextWatcher(mEtPassword));
This is not working on my LG G4's device as well the onEditorAction method is not either called !
I tried so many searches and uses other people solutions but it seems a manufacturer issue !
Finally, I figured it out.
I added this xml property to my EditText:
android:singleLine="true"
I know its deprecated, but android:maxLines="1" not working although its the replacement for singleLine.
I also removed this:
android:imeOptions="actionDone"
Thank you guys for trying to help me.
try this to detect enter key
mEtPassword.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN)
// return false;
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//your necessary codes...
}
return false;
}
});
Try using this method
edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());
I am developing an app for an on-line store. One of the main feature is the search functionality. As I need to support API 8 and above instead of the SearchView I have used an EditText like below to implement the search view.
<EditText android:id="#+id/search_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/search_button"
android:background="#color/white"
android:ems="20"
android:imeOptions="actionDone"
android:maxLines="1"
android:textColor="#color/black"
android:textColorHint="#color/ebuy_color_second_strip"
android:textSize="18sp" >
</EditText>
Now I want to handle the enter key of the keyboard, in a way than when the user press enter, the search activity begins. The code that I have used is like below:
search = (EditText) findViewById(R.id.search_edit);
search.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
String query_text = search.getText().toString().trim();
try {
query_text = URLEncoder.encode(query_text, "utf-8");
} catch (UnsupportedEncodingException e) {
}
String full_query = query_text;
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
Intent bussiness = new Intent(EbuyHomeScreen.this,
EbuySearchActivity.class);
Bundle basket_buss_category = new Bundle();
basket_buss_category.putString(EbuyUtils.TAG_CATEGORY_CODE, full_query);
bussiness.putExtras(basket_buss_category);
startActivity(bussiness);
return true;
}
return false;
}
});
This way works perfectly good with android keyboard and some other keyboards, but when I use swipe or swift keyboard and other personalized keyboards that are downloaded from Google Play Store this doesn't work , and instead of beginning the activity jumps in a new line. Can somebody help me solve this kind of issue.
Try setOnEditorActionListener on your edittext.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//your code here
return true;
}
return false;
}
});
You need to set imeAction in your edittext's xml file:
<EditText
android:id="#+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="USERNAME"
android:inputType="text"
android:imeOptions="actionSearch" >
<requestFocus />
</EditText>
In edittext, after typing 'Enter' key, system make a new line inside it. I'd like to focus on next edittext, no new line. how to code? my code in xml is below
<EditText
android:id="#+id/txtNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblNPCode"
android:layout_below="#+id/lblNPCode"
android:layout_centerHorizontal="true"/>
<EditText
android:id="#+id/txtCNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblCNPCode"
android:layout_below="#+id/lblCNPCode"
android:layout_centerHorizontal="true"/>
I also caputer key code in setOnKeyListener
tCNPCode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == 66) {
Toast.makeText(S_PCode.this, "Enter Key", Toast.LENGTH_LONG).show();
//tNPCode.setFocusable(true);
}
return false;
}
});
You don't even have to use OnKeyListener. Simply add line android:singleLine="true" to your EditText. After this operation EditText behaves exactly like you want. So, in your particular case:
<EditText
android:id="#+id/txtNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblNPCode"
android:layout_below="#+id/lblNPCode"
android:layout_centerHorizontal="true"
android:singleLine="true"
/>
<EditText
android:id="#+id/txtCNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblCNPCode"
android:layout_below="#+id/lblCNPCode"
android:layout_centerHorizontal="true"
/>
solves the problem.
You have to use the requestFocus method. In your case it will be something like:
tCNPCode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER) {
txtCNPCode.requestFocus();
}
return false;
}
});
The best way is to use one of the options for inputType, for example: android:inputType="textPersonName"
This will achieve the wanted effect. Check out the other options too, it'll save you some time in future :)
You shouldn't use android:singleLine="true", since it's deprecated. Here's what the documentations says about it:
This attribute is deprecated and is replaced by the textMultiLine
flag in the inputType attribute. Use caution when altering existing
layouts, as the default value of singeLine is false (multi-line
mode), but if you specify any value for inputType, the default is
single-line mode. (If both singleLine and inputType attributes are
found, the inputType flags will override the value of singleLine.).
Consider IME options, good description here http://androidcookbook.com/Recipe.seam;jsessionid=C824437B48F346E23FBE5828969029B4?recipeId=422 and documentation here http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions.
The only thing you need is add attribute android:imeOptions="actionNext" on each EditText. Keyboard appears with a Next key where Enter used to be and allows to navigate to the next EditText.
But order of "actionNext" is from top to bottom. For example, if you have horizontal layout:
<LinearLayout android:orientation="horizontal" >
<EditText android:id="#+id/et1" android:imeOptions="actionNext" />
<EditText android:id="#+id/et2" android:imeOptions="actionNext" />
</LinearLayout>
et2 never get focus. You should fix it in the code:
et1.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
switch (actionId)
{
case EditorInfo.IME_ACTION_NEXT:
boolean result = false;
TextView v1 = (TextView) v.focusSearch(View.FOCUS_RIGHT);
if (v1 != null)
result = v1.requestFocus(View.FOCUS_RIGHT);
else if (!result)
{
v1 = (TextView) v.focusSearch(View.FOCUS_DOWN);
if (v1 != null)
result = v1.requestFocus(View.FOCUS_DOWN);
}
if (!result)
v.onEditorAction(actionId);
break;
default:
v.onEditorAction(actionId);
break;
}
return true;
}
});
Original link: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/OykOG6XtE8w
Use Single Line of Code (Java)
tCNPCode.setSingleLine(true);
Kotlin
tCNPCode.isSingleLine = true
It will navigate to the next item on Enter press
Just set the maximum number of line to the EditText box in xml layout file:
android:maxLength="1"