Controlling Ok SoftKeyboard buttons Android - 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 */
}
}
});

Related

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

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 handle an enter pressed key android

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>

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

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