Add action to "Enter" button on Android keyboard - android

I would like to know how can I add an event/action for pressing the "Enter" button on the keyboard.
"The action would be the same as pressing the Button that I created on my application"

You can use imeOptions attribute to specify an action on your EditText
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/password_hint"
android:inputType="textPassword"
android:imeOptions="actionLogin" />
and then create a listener on your activity/fragment for that action
EditText password = (EditText) findViewById(R.id.password);
password.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_LOGIN) {
login();
}
return false;
}});

Related

Custom ime action in dialog

I have this EditText :
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:inputType="textCapSentences"
android:gravity="center"
android:imeActionId="#+id/action_done"
android:imeOptions="actionDone">
<requestFocus/>
</EditText>
and this is the code inside my class that extends Dialog :
name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d(TAG, "name action performed");
if (actionId == R.id.action_done) {
ok.performClick();
return true;
}
return false;
}
});
I am looking to perform a custom action when the editor button is clicked, namely call the OnClick() method of the ok button.
However, onEditorAction() never gets called.
What am I doing wrong?
Where is your editor button?
onEditorAction() would be called when enter button is pressed on the keyboard

How to pass control from one textview to other after pressing the enter button in keyboard?

I have two textViews shown below after pressing enter button in first textview the cursor should goes to second textview. How?
<AutoCompleteTextView
android:id="#+id/txt_login_username"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_above="#+id/pengala_logo"
android:layout_alignLeft="#+id/txt_login_pwd"
android:ems="10"
android:hint="Please enter Email"
android:inputType="textAutoComplete"
android:textColorHint="#ffffff"
android:textSize="20sp" />
<requestFocus />
<EditText
android:id="#+id/txt_login_pwd"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/btn_login_submit"
android:layout_alignTop="#+id/text"
android:ems="10"
android:hint="Please enter Password"
android:inputType="textPassword"
android:textColorHint="#ffffff"
android:textSize="20sp" />
try this, EditBox have the requestFocus() you can use this while clicking Button.
EditText.requestFocus();
I think it should work
EditText editText1=(EditText)findViewById(R.id.text1);
EditText editTtext2=(EditText)findViewById(R.id.text2);
editText1.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
editTtext2.requestFocus();
}
return true;
}
});
Make editText1 single line true.
Looking at this question, you can simply use the
android:imeOptions="actionNext" option on your txt_login_username to change the 'enter' key to go to the 'next' input. You may need to specify android:singleLine="true", as this will not work on a multi-line input.
Documentation can be found here.
final EditText editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v , int keyCode , KeyEvent event) {
EditText editText2 = (EditText) findViewById(R.id.editText2);
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_A) {
Selection.setSelection((Editable) editText2.getText(),editText.getSelectionStart());
editText2.requestFocus();
}
return true;
}
});

Next edittext when enter clicked

I am trying to implement a button that does not move to next edittext when clicked. So here is my code:
<EditText
android:id="#+id/address_edittext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionNone"
android:inputType="textCapWords"
android:singleLine="true" />
When I click this button some actions are done by code, but incorrectly jumps to other edittext.
How could I fix that?
Use TextView.OnEditorActionListener here is LINK
see this example
editText = (EditText) findViewById(R.id.emai_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// your action...........
}
return false;
}
});
just you need to add textMultiLine in addition to textCapWords
<EditText
android:id="#+id/editText_itemDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textCapWords"
/>

Controlling Ok SoftKeyboard buttons Android

I set an Ok key on soft android keyboard when i click on the edittext shown below:
<EditText
android:id="#+id/rlMP3SeekContainer"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/text_rougea"
android:singleLine="true"
android:imeOptions="actionDone"
android:ems="10"
android:hint="#string/hint_deezer_search"
android:paddingLeft="#dimen/eight_dp"
android:paddingRight="#dimen/eight_dp"
android:textColor="#color/black"
android:textColorHint="#color/gray_text"
android:textSize="#dimen/twelve_sp" />
When the keyboard appear i want the user when clicking on the ok button do something. but how can i override the ok button on the keyboard to do whatever i want
You need to implement the OnEditorActionListener:
yourEditText.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
/* your code here */
}
}
});

Android 4.0.3 switching Enter button with Search button on soft keyboard

I'm trying to get this working: in my XML in EditText I have:
android:imeActionLabel="Search"
android:imeOptions="actionSearch"
But it doesn't work. In code
search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Doesn't work either. Any clues?
try this
in your xml file
<EditText
android:id="#+id/rechercheTextView"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:imeOptions="actionSearch"
android:inputType="text"
/>
in your Activity
EditText rechTxt = (EditText) findViewById(R.id.rechercheTextView);
rechTxt.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(getApplicationContext(),"search",Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});

Categories

Resources