I have some app with EditText.
<EditText
android:id="#+id/et_game_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:imeOptions="actionDone"
android:singleLine="true" >
<requestFocus />
</EditText>
When I click Done button on the keyboard, the one hides, but I want to remain keyboard after clicking. Keyboard must be hide only pressing Back button. I tried to add InputMethodManager.SHOW_FORCED to continue showing keyboard, but this doesn't work.
editTextWord.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
someCode();
inputManager.hideSoftInputFromWindow(
getCurrentFocus().getWindowToken(),
InputMethodManager.SHOW_FORCED);
}
return false;
}
});
How can I do this?
Related
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>
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;
}
});
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 */
}
}
});
I have to different EditText like these :
EditText1 has the inputType "textCapWords"
EditText2 has the inputType "number"
When I launch my activity, I instantly request focus on the EditText1. The only way to do so that is working is to add this on the onCreate :
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
EDITTEXT1.requestFocus();
When the user clicks on "Return" on his soft Keyboard, I want the EDITTEXT2 to obtain focus. So I added this :
EDITTEXT1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 66) { // CODE FOR RETURN
EDITTEXT2.requestFocus();
}
return false;
}
});
But when I do so, nothing happens. All my EditText are set FocusableInTouchMode and Focusable :
<EditText
android:id="#+id/EDITTEXT1"
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="2"
android:background="#color/White"
android:ems="10"
android:hint="My First EditText"
android:inputType="textCapWords"
android:padding="2dip"
android:singleLine="true"
android:textColor="#color/Black"
android:textSize="12dip" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/EDITTEXT2"
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="2"
android:background="#color/White"
android:ems="10"
android:hint="My Second EditText"
android:inputType="textCapWords"
android:padding="2dip"
android:singleLine="true"
android:textColor="#color/Black"
android:textSize="12dip" >
</EditText>
Do you have any idea on how I could focus the EDITTEXT2 after pressing OK on EDITTEXT1 ?
Thanks in advance!
Thanks a lot for your answer. I finally found the easiest way to answer my own question :
Add :
android:nextFocusRight="#+id/EDITTEXT2"
To the properties of EDITTEXT1.
Nothing else is needed in order to go to the EDITTEXT2 after clicking "Enter" on EDITTEXT1.
you should create your own EditText which extended from EditText firstly, then override the mothod dispatchKeyEventPreIme like this:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
return super.dispatchKeyEventPreIme(event);
} else if (event.getAction() == KeyEvent.ACTION_UP) {
hideInputMethod();
//change your focus at here
return true;
}
}
return super.dispatchKeyEventPreIme(event);
}
protected void hideInputMethod() {
InputMethodManager imm = (InputMethodManager)getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != imm) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
By the way, it not a good way to code like that if (keyCode == 66) { // CODE FOR RETURN, and also 66 is not the value of back, it's key enter.
public static final int KEYCODE_ENTER = 66;
you'd better write KeyEvent.KEYCODE_BACK or KeyEvent.KEYCODE_ENTER instead.
I wish this could help you.
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;
}
});