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
Related
does anyone know what keycode I have to press for this Send/Search/Next button?
I'm trying to send a chat message and can't find a solution to this one.
Already tried keycode: 66 and 84.
keyboard view
For adding action on click of send/done button of soft keyboard, Need to add setOnEditorActionListener to edittext.
EditText code in xml :
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSend"
android:singleLine="true"/>
In Java class:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
String text = editText.getText().toString();
Log.e(TAG,"text-----"+text);
return true;
}
return false;
}
});
You can add any imeOptions to edittext according to your requirement.
For more info related to EditerInfo check official doc.
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
Here is my code:
<android.support.design.widget.TextInputLayout
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/some_layout">
<android.support.design.widget.TextInputEditText
android:id="#+id/myid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="#string/some_hint"
android:imeOptions="actionNext"
android:inputType="time"
android:maxLength="#integer/max_input_length"
android:maxLines="1"
android:singleLine="true"
android:textSize="15sp"/>
</android.support.design.widget.TextInputLayout>
and the Java code:
myField = (TextInputEditText) findViewById(R.id.myid);
myField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_NEXT) {
Log.d(TAG,"next");
//Do something
handled = true;
}
Log.d(TAG,"handled: "+handled);
return handled;
}
});`
Unfortunately when I press the next button on the keyboard nothing happens. The cursor doesn't jump to the next field.
I can't see what I am missing
use android:inputType="text" for your TextInputEditText
try calling view.requestFocus(); in your action .
myField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_NEXT) {
Log.d(TAG,"next");
//Do something
Log.d(TAG,"handled: "+handled);
view.requestFocus() ; //add focus to next view object
return true; //return true
}
Log.d(TAG,"handled: "+handled);
return false; //add return
}
});
As per doc
IME_ACTION_NEXT
Bits of IME_MASK_ACTION: the action key performs a "next" operation,
taking the user to the next field that will accept text.
So it means it will focus on next focusable object like edittext or auto complete text view. so if no other object can get focus it will not move focus.
Can someone please provide a solution to get a working listener for the soft-keyboard DONE button, and/or explain what I'm doing wrong in my current approach?
My Xml & Java Setup
In Xml, there's a simple EditText is set with android:imeOptions="actionDone"
In Java, it just has a basic TextView.OnEditorActionListener declared
So setOnEditorActionListener() is not getting called when there is a click from device soft-keyboard submit (aka DONE) button - the green arrow button below ; only the EditText field is cleared
But that listener DOES get called when clicking the computer Enter key (attached via ADB).
I would figure it should work for both those buttons .. is this not right?
XML Layout File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.demo.MainActivity">
<EditText
android:id="#+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:imeOptions="actionDone"
/>
</LinearLayout>
Java Activity File:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int i = 5; // added this to set a break point
return false;
}
});
}
}
Update for Solution
Thanks to #Apoorv Mehrotra's answer, it turns out that my EditText was missing this one attribute in order for the soft keyboard event to be recognized. Adding this to the above Xml solves the problem.
android:inputType="text"
I had the same problem, for me, adding android:singleLine="true" in EditText did the trick
try this code this is a code with 2 fields in which i have user time feature. in first one keyboard shows next button and in second one soft keypad shows done option and on done option i hide the soft keypad.
below is the xml file code.
<EditText
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="top|left"
android:imeOptions="actionNext"
android:inputType="text|textNoSuggestions"
android:text=""
android:visibility="visible" />
<EditText
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="top|left"
android:imeOptions="actionDone"
android:inputType="text|textNoSuggestions"
android:text=""
android:visibility="visible"
/>
now below is the java code: for hiding soft keypad on next and same can be performed for done option as well.
this code add in onCreate() of your class.
EditText first_txt = (EditText) findViewById(R.id.txt1);
EditText second_txt = (EditText) findViewById(R.id.txt2);
first_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
second_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
This code works for , hope it helps you out!
This is an old issue that seems not to be solved yet. There's a workaround for these cases. Simply take the EditText (or whatever View you might be needing this and assign it a onEditorActionListener, like this:
final EditText editor = (EditText) findViewById(R.id.myEditText);
editor.setOnEditorActionListener(EnterOnText);
Now define the EnterOnText implementation like this:
TextView.OnEditorActionListener EnterOnText = new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
final String msg = view.getText().toString();
if (!msg.isEmpty()) {
// Do whatever you need here
...
}
}
return true;
}
};
Now, simply change the layout's imeOptions attribute to:
android:imeOptions="actionGo"
That will, oppositely, probably make your emulator's enter key not work, but will (should) do on all physical devices.
((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
Try this,
((EditText) findViewById(R.id.comment_text)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null && (actionId == EditorInfo.IME_ACTION_GO
|| event.getKeyCode() == event.KEYCODE_ENTER))
{
//do whatever you want
}
}
});
<EditText
android:id="#+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:imeOptions="actionGo"
/>
How to replace the search icon with text "Search" on soft keyboard.
<EditText
android:imeOptions="actionSearch" />
Instead of using imeOptions use the following :-
<EditText android:imeOptions="actionSearch"
android:inputType="text"/>
For handling click listener do as follows :
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
} });
Answer taken from this stackoverflow question.
Try to set
android:imeActionLabel in XML or
in java code with .setImeActionLabel()
editText.setImeActionLabel(getString(R.string.xxx), EditorInfo.IME_ACTION_SEARCH);