Custom ime action in dialog - android

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

Related

onBackPressed doesn't called when I'm came back from a dialog fragment and a edit text gets focus

I'm getting a problem in a UI form that I'm building in Android.
In that form I have some edit text in which the user has to touch them to open a dialog fragment. In the dialog fragment, user can set a value and then, this value is shown on the edittext touched. The problem is the following: when user close the dialog fragment and the edittext touched gets focus, if the user press the back button to go out, the onBackPressed() method is not being called.
I must clarify that the edittexts that open a dialog fragment doesn't show keyboard because the user can't write on them. I don't want to use textviews.
Here I show you part of the layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
....
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_input_when"
android:layout_marginTop="30dp">
<EditText
android:hint="#string/meeting_when"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edit_text_when"
android:textSize="18sp"
android:inputType="text|date"
android:textIsSelectable="true"
android:focusable="true"
android:drawableLeft="#drawable/ic_black_18dp"
android:drawableStart="#drawable/ic_black_18dp"
android:drawablePadding="10dp"
android:onClick="onEditTextWhenClicked"
android:nextFocusForward="#+id/edit_text_time"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text_input_time"
android:layout_marginTop="30dp">
<EditText
android:hint="#string/meeting_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edit_text_time"
android:textSize="18sp"
android:inputType="time"
android:textIsSelectable="true"
android:nextFocusForward="#+id/edit_text_place"
android:focusable="true"
android:drawableLeft="#drawable/ic__black_18dp"
android:drawableStart="#drawable/ic__black_18dp"
android:drawablePadding="10dp"
android:onClick="onEditTextTimeClicked" />
</android.support.design.widget.TextInputLayout>
....
....
</LinearLayout>
So, for example, if the user touch the "when" edit text, a datepicker dialog is open:
when the user set the date, the dialog fragment is closed and the value is setted on the edit text
And now, if the user press the back button, it doesn't work.
In the activity I have
#Override
public void onBackPressed(){
if ( !areAllFieldEmpty() ) {
showAlertCloseDialog();
}else
super.onBackPressed();
}
But these method it doesn't be called.
.
I don't have any idea about how to solve it. Please help me. If you need more information, let me know it. Thanks.
Register a callback in your fragment to be invoked when a hardware key is pressed in your view:
if (mLayout != null) {
mLayout.setFocusableInTouchMode(true);
}
mLayout.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().onBackPressed();
return true;
}
return false;
}
});
If it's the back key, go back and consume the event returning true, return false otherwise.
If you have troubles with back and the keyboard override onKeyPreIme() extending your EditText:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// User has pressed Back key. So hide the keyboard
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
// Hide your view as you do it in your activity
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
// Eat the event
return true;
}
return false;
}

Add action to "Enter" button on Android keyboard

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;
}});

Handle Enter button soft keyboard

I'm trying to handle the Enter button on Android soft keyboard. I tried what it's said in this post also from StackOverflow, but with no luck.
I have two EditText fields: one that asks for user name and the other for the user email. When I complete the username, I want to perform a Next action and go down to the next EditText.
This is my .xml file:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/text_user_name" android:layout_marginTop="20dp"
android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="text"
android:nextFocusDown="#+id/text_user_email"
android:hint="name"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#id/text_user_email"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:imeOptions="actionDone"
android:hint="***#email.com"/>
This is my OnEditorActionListener:
return 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 (!finished)
{
// Check user information
if (CheckUserInfo()) finished = true;
}
return true;
}
else
if (actionId == EditorInfo.IME_ACTION_NEXT && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Toast.makeText(getApplicationContext(), "go a line down", Toast.LENGTH_SHORT).show();
return true;
}
else
{
return false;
}
}
};
I tried also to change the visible text for the Enter button, to look like a Next for the first EditText and as a Done for the final EditText like this:
textUserName.setImeActionLabel("Next", KeyEvent.KEYCODE_ENTER);
....
textUserEmail.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER);
But this part is not working either.
Can anybody help me understand why I can't make anything work? :_( Thanks!
You can use
android:imeActionLabel="your text here"
android:imeOptions="actionNext"
for next imeoption
and
android:imeActionLabel="your text here"
android:imeOptions="actionSend"
for the done button
you can do this way too
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == KeyEvent.KEYCODE_ENTER) {
doSomeThing();
}
return false;
}
});

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 */
}
}
});

Categories

Resources