Android detect Done key press for OnScreen Keyboard
Yes, it is possible:
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
Note that you will have to import the following libraries:
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
Android Use Done button on Keyboard to click button
You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:
max.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});
Android trigger Done on KEYCODE_DONE?
One note regarding this item. I found the problem per se is not the KEYCODE_DONE button, as it works with specific programs, but the capability to make Google HTML browser react to it.
Otherwise, the following works perfectly fine:
sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
Catch DONE key press from soft keyboard
I got it sorted, this code runs smoothly and it recognizes key press of DONE button from soft keyboard.
Layout file:
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="NEXT"
android:textSize="10sp" />
</RelativeLayout>
.java file:
package com.example.drywallcalculator102v;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class FirstActivity extends Activity {
private EditText editText;
private Button btnNext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_firstactivity);
btnNext = (Button) findViewById(R.id.button1);
btnNext.setEnabled(false);
editText = (EditText) findViewById(R.id.editText2);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
btnNext.setEnabled(true);
}
return false;
}
});
}
}
Android - Get keyboard key press
For handling hardware keys and Back key you could use dispatchKeyEvent(KeyEvent event)
in your Activity
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
UPD: unfortunately you can't handle soft keyboard events (see Handle single key events), unless you develop your own custom keyboard (follow the link to learn how Creating input method).
How do I detect if software keyboard is visible on Android Device or not?
There is no direct way - see http://groups.google.com/group/android-platform/browse_thread/thread/1728f26f2334c060/5e4910f0d9eb898a where Dianne Hackborn from the Android team has replied. However, you can detect it indirectly by checking if the window size changed in #onMeasure. See How to check visibility of software keyboard in Android?.
Related Topics
The Process of the Service Is Killed After the Application Is Removed from the Application Tray
Listfragment Onlistitemclick Not Being Called
Android: Asynctask to Make an Http Get Request
How to Use and Style New Alertdialog from Appcompat 22.1 and Above
How to Add Multiple Widgets in the Same App
Findclass from Any Thread in Android Jni
Android: Crop an Image After Taking It with Camera with a Fixed Aspect Ratio
Set Android Ip,Dns,Gateway Setting Programmatically
How to Wrap the Height of a Viewpager to the Height of Its Current Fragment
Android: How to Share Image with Text on Facebook via Intent
Transparent Alertdialog Has Black Background
How to Handle Multiple Countdown Timers in Listview
Sqlite Returned an Error Code of 14
What's the Difference Between Setwebviewclient VS. Setwebchromeclient
Upload a File Through an Http Form, via Multipartentitybuilder, with a Progress Bar
How to Automatically Restart a Service Even If User Force Close It