How can I set the focus (and display the keyboard) on my EditText programmatically
Try this:
EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
http://developer.android.com/reference/android/view/View.html#requestFocus()
How to set focus to editText when fragment start?
editText.requestFocus() will put focus to your View
if it is focusable . But I guess you want to show keyboard when it is focused. If I am right then the following code might work for you.
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
The code is from this post.
You can also check Android Developers for details.
How to set focus on the last character of EditText on Android
After the call to requestFocus()
, you can set the position of the cursor at the end of the EditText as follows:
EditText editText = (EditText) findViewById(R.id.textId);
int pos = editText.getText().length();
editText.setSelection(pos);
set cursor focus on other edittext in android after done on keyboard button
You can try this one:
edtwo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
edtwo.setText("");
edone.setText("");
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
edone.requestFocus();
return true;
}
});
Set focus to the EditText upon clicking a button
This code for OnFocusChangeListener
has to be modified little bit, for anonymous classes you need to have final reference of any object which you wish to modify, since you want to modify EditText
final int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
Now for OnClickListener
do like this
for (int i=0;i<btnEditId.length;i++)
{
final int counter=i;
int button=btnEditId[i];
findViewById(button).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// HERE v refers to button itself on which you clicked,
// you need to update get edit text so
// based on ith position accessing the same edit as the button correspond too
EditText edt=etColorId[counter];
edt.setEnabled(true);
edt.requestFocus();
// and you are DONE!!!
}
});
}
UPDATE:
new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{ // lost focus
v.setEnabled(false);
}
else
{
//you are already enabling on button click
}
}
});
How to programmatically set and remove Focus on edit text on click of a button
I think you confused setting focus and showing and hiding keyboard so try my answer:
case R.id.add_timeline_status_IBTN:
time_statusTV.setFocusable(true);
time_statusTV.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput( time_statusTV, 0);
and for clearing it:
imm.hideSoftInputFromWindow(your edittext.getWindowToken(), 0);
so your code must be something like:
time_statusTV = (EditText) rootView.findViewById(R.id.time_statusTV);
time_statusTV.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
imm.hideSoftInputFromWindow( time_statusTV.getWindowToken(), 0);
time_statusTV.clearFocus();
}
return false;
}
});
Related Topics
Delete Alarm from Alarmmanager Using Cancel() - Android
Android Setonclicklistener Method - How Does It Work
Resizing Layouts Programmatically (As Animation)
Ontaskremoved() Not Getting Called in Huawei and Xiaomi Devices
Restore Logcat Window Within Android Studio
Activity Layout: Fragment Class: VS Android:Name Attributes
How to Check Which Notifications Are Active in Status Bar in Android Dev
Change the Text Color of Numberpicker
Android: Programmatically Detect If Device Has Hardware Menu Button
Android: Implementing Progressbar and "Loading..." for Endless List Like Android Market
Creating Custom Lockscreen in Android
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
How to Save Image in Android Gallery
Android Java.Lang.Noclassdeffounderror
How to Create a Custom Notification Layout in Android
Convert Magnetic Field X, Y, Z Values from Device into Global Reference Frame