How can I check if enter on my keyboard is clicked? - android

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"/>

Related

android edittext inputType:textpassword not working with intputType:actionDone on some devices

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

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

Set next EditText focused and editable on KEY_DOWN

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

Categories

Resources