Implicit Submit after hitting Done on the keyboard at the last EditText
Try this:
In your layout put/edit this:
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone" />
In your activity put this (e. g. in onCreate):
// your text box
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit_btn.performClick();
return true;
}
return false;
}
});
Where submit_btn
is your submit button with your onclick handler attached.
Android - Handle Enter in an EditText
I am wondering if there is a way to
handle the user pressing Enter while
typing in an EditText, something like
the onSubmit HTML event.
Yes.
Also wondering if there is a way to
manipulate the virtual keyboard in
such a way that the "Done" button is
labeled something else (for example
"Go") and performs a certain action
when clicked (again, like onSubmit).
Also yes.
You will want to look at the android:imeActionId
and android:imeOptions
attributes, plus the setOnEditorActionListener()
method, all on TextView
.
For changing the text of the "Done" button to a custom string, use:
mEditText.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER);
android: Softkeyboard perform action when Done key is pressed
Try this
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
insert the data in database when keyboard's done button is click in android
make one
class DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper{
SQLiteDatabase dbms1;
DatabaseHelper dbHelper;
Cursor records;
public DatabaseHelper(Context context){
super(context,"UserDatabase", null,1);
Log.i("Information", "Database Created");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void createTable(){
try{
dbms1=getWritableDatabase();
String sql1="create table if not exists user(_id INTEGER PRIMARY KEY AUTOINCREMENT, name text not null,emailid text,gender text,username text,password text,birthdate text,phonenumber text,admin boolean,image BLOB);";
Log.i("haiyang:createDB=", sql1);
dbms1.execSQL(sql1);
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
}
public void closeDatabase() {
// TODO Auto-generated method stub
try{
dbms1.close();
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
}
public boolean inserRecord(String name,String emailid,String gender,String username,String password,String birthdate,String phonenumber,boolean admin,Drawable drawableResource) {
// TODO Auto-generated method stub
Bitmap image = ((BitmapDrawable)drawableResource).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, out);
ContentValues record=new ContentValues();
record.put("name",name);
record.put("emailid",emailid);
record.put("gender",gender);
record.put("username",username);
record.put("password",password);
record.put("birthdate",birthdate);
record.put("phonenumber",phonenumber);
record.put("admin",admin);
record.put("image",out.toByteArray());
try{
dbms1=this.getWritableDatabase();
dbms1.insert("user", null,record);
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
return true;
}
}
after you have to create object of
DatabaseHelper dbHelper=new DatabaseHelper(this);
dbHelper.createTable();
now call insert
method of DatabaseHelper class
dbHelper.insertRecord(....arguments....);
where you have written Toast
Android - Handle Enter in an EditText
I am wondering if there is a way to
handle the user pressing Enter while
typing in an EditText, something like
the onSubmit HTML event.
Yes.
Also wondering if there is a way to
manipulate the virtual keyboard in
such a way that the "Done" button is
labeled something else (for example
"Go") and performs a certain action
when clicked (again, like onSubmit).
Also yes.
You will want to look at the android:imeActionId
and android:imeOptions
attributes, plus the setOnEditorActionListener()
method, all on TextView
.
For changing the text of the "Done" button to a custom string, use:
mEditText.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER);
How to show soft-keyboard when edittext is focused
To force the soft keyboard to appear, you can use
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
And for removing the focus on EditText
, sadly you need to have a dummy View
to grab focus.
To close it you can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This works for using it in a dialog
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
Related Topics
Android: Looking for a Drawarc() Method with Inner & Outer Radius
Multiple Table SQLite Db Adapter(S) in Android
How to Get a Fragment to Remove Itself, I.E. Its Equivalent of Finish()
How to Stop an Animation (Cancel() Does Not Work)
Nested Fragments Disappear During Transition Animation
Android Navigation Drawer on Top Actionbar
Custom Translucent Android Actionbar
Why So Complex to Set Style from Code in Android
Differencebetween Sendstickybroadcast and Sendbroadcast in Android
How to Prevent a Scrollview from Scrolling to a Webview After Data Is Loaded
Waiting for Target Device to Come Online
How to Hide One Item in an Android Spinner
Java.Lang.Noclassdeffounderror: Failed Resolution Of: Landroid/Support/V4/Os/Buildcompat
How to Update UI from Asynctask
Garbage Collection Causes:Mediaplayer Finalized Without Being Released