I've browsed similar questions and followed the suggestions there, but for the love of god, I can't get this to work, and it's driving me crazy. So here's the deal:
I have an editText, which needs to requestFocus at program startup, and pop the soft keyboard. If I put "android:windowSoftInputMode="stateVisible" in the Manifest, the keboard shows every time the activity starts. I only want it to show once with onCreate(), and when the user specifically clicks on the editText. My code for this is below:
EditText argument;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_buttons);
argument = (EditText) findViewById(R.id.editText_argument);
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(argument, InputMethodManager.SHOW_FORCED);
Q1) This code doesn't work. What am I doing wrong?
Q2) You see that I declared "EditText argument" outside of onCreate(), as I'd like to use this in the rest of the activity, not just within onCreate(). Is this good programming practice?
Q3) Then, when the user clicks done on the soft keyboard, I'd like this EditText to lose focus, i.e. the cursor should disappear. I understand that I need to have a dummy View to do this, but I still don't exactly understand how to switch focus to the dummy. How would I go about doing that?
Thanks so much in advance!
A1) You're missing a editText.requestFocus().
Refer: Soft Keyboard shows up on EditText focus ONLY once should help for dismissing soft keyboard.
A2) Yes, that's fine. Most of the UI elments should be declared at the class level scope and initialized in onCreate()
A3) A1's reference link should help you here.
Happy Coding!
EDIT:
onCreate():
EditText argument;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_buttons);
argument = (EditText) findViewById(R.id.editText_argument);
showKeyboard():
argument.requestFocus();
argument.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(argument, 0);
}
},200);
dismissKeyboard():
argument.requestFocus();
argument.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(argument.getWindowToken(), 0);
}
},200);
Related
i am sing keyboard view to select. After selecting the keyboard, how to display the selected keyboard text in textview android?
i can display only language but how to display the Title and summary for change keyboard?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String localeString = ims.getLocale();
Locale locale = new Locale(localeString);
String currentLanguage = locale.getDisplayLanguage();
Toast.makeText(MainActivity.this,currentLanguage,Toast.LENGTH_SHORT)
.show();
//Title and summary for change keyboard need to display
}
});
}
}
there are two ways to handle this, the simplest way is use a non-size-edittext
don't hide the text view, if it hide then edtTxt.getText().toString() gets empty always
<EditText
android:id="#+id/edtTxt"
android:layout_width="0px"
android:layout_height="0px" />
So that user can't see that. and on click of button
edtTxt.requestFocus();
edtTxt.setText("");
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(edtTxt.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED,
0);
Now edtTxt.getText().toString() giving text.
Without an EditText you're going to have a hard time.
An InputMethod needs to be connected to a view. Whatever view you use, you need to override onCreateInputConnection to return a custom InputConnection object that at a minimum implements commitText (for word input), deleteSurroundingText (for deletes), and sendKeyEvent (for keyboards that assume you're in dumb mode), and all of the completion functions. Input connections are complicated things and you'll screw up 3rd party keyboards like Swiftkey and Swype if you don't get it right. I really don't suggest doing this.
If you want to do this your best chance of getting it right is to claim your window is a TYPE_NULL input type. Most keyboards will dumb themselves down and assume you only accept the simplest commands in that mode. But you can't count on it.
I'd look at the InputConnection returned by the EditText class and copy as much of it as possible.
My activity has a single text field, which is editable, I want it so that when the activity is started the keyboard doesn't automatically start up, it should come up only when the user clicks on the editTiext field.
Any help?
In your manifest file add this line android:windowSoftInputMode="stateHidden"to your activity!
It seems like when your activity starts your TextView (since you said text field I suppose you have a TextView but the property exists on other views as well) receives automatically focus. Try looking at the TextView properties to find one that is about the object receiving focus.
public static void hideKeyboard(Context mContext){
//Hide a keypad write down on onCreate
((Activity) mContext).getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
public static void showKeyboard(Context mContext,EditText edittext){
//Show a Keyboard when you click on Edittext
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
}
I have 2 EditTexts in the MainActivity Layout. If i run the application normally the 1st EditText gets focused but the softkeyboard is not openned.
but when i used this:
public class TestingActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et1 = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
}
}
expecting the 2nd EditText will get focus and softkeyboard will be openned.
I only get focus, but the softkeyboard is openned only when i click on the EditText.
Thank You
Try specifying the android:windowSoftInputMode attribute in your AndroidManifest.xml file for your activity.
For example:
<activity android:name=".TestingActivity" android:windowSoftInputMode="stateVisible|adjustResize" />
You probably don't need any of the code that uses InputMethodManager in your Activity.
I notice that one reason for the keyboard not showing up is selecting an inputtype not supported by the specific Android device. For instance InputType.TYPE_NUMBER_VARIATION_NORMAL will not work on my Asus Transformer (no keyboard shows up), while InputType.TYPE_CLASS_NUMBER will work just fine.
et2.clearFocus();
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
I meet the problem on Android N platform and resolve it by refocusing the editview.
I don`t know the real reason why the editview should be cleared first,but it works fine for me.
Sometimes you will need to post-delay showing keyboard command, so in my case, i did the following
editText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}, 300);
For getting the focus to particular edittext just add the tag inside your edit text.
<EditText
android:id="#+id/etBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="enter into editbox"
>
<requestFocus/>
</EditText>
I want to immediately highlight give focus to a particular edit box when the activity loads and bring up the softkeyboard. how can I do this? Also should there be anything in onStart()?
You can call : requestFocus() on the View after you do findViewByID()
You can do that in onStart - I see now reason for it not to work.
Check here too :
http://developer.android.com/reference/android/view/View.html
In your layout put </requestFocus> tag inside EditText.
In onStart() call
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Ideally this should work.
<EditText
android:id="#+id/Abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
But there have been some known issues of the keyboard not popping up. See this.
You can also do this using Runnable:
public class MyActivity extends Activity {
private Handler mHandler= new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get text
mHandler.post(new Runnable() {
public void run() {
text.requestFocus();
}
});
}
}
Using the getSystemService(...), one can obtain, in this case, the Context.INPUT_METHOD_SERVICE, have a look at this code sample below for illustration:
InputMethodManager imm = (InputMethodManager)context.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext, 0, null);
Sorry for asking another time help on this matter, but all others posts didn't help.
Here's the scenario: I have a Acivity ('A') that incorporates a Layout with a fragment inside. This fragment is swapped on user input. One of this fragments has a edittext inside, which I want to get focus on creation AND show the damn soft keyboard. So, in the onCreateView() of the fragment I use:
mEt = (EditText) v.findViewById(R.id.et);
mEt.setImeOptions(EditorInfo.IME_ACTION_DONE);
mEt.requestFocus();
So, it works the first time, but if the fragment is replaced and re-created later, it gets the focus but the keyboard does not appear.
I tried to hide keyboard before the fragment is destroyed via:
InputMethodManager keyboard = (InputMethodManager)
ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(et.getWindowToken(), 0);
or to explicit show the keyboard via:
InputMethodManager keyboard = (InputMethodManager)
ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(et, 0);
but (as you can imagine by the fact I'm posting here :) ), the problem stay.
I also desperatly thought about a activity/fragment problem and used same techniques with listeners on the activity, without luck.
Quite frustrated, please help :)
I just solved this problem. We had an activity that swapped out multiple Fragments with text fields that needed focus and the keyboard.
There are two ways to solve this, both of which I played with. This is the method I finally went with.
#Override
private View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
if (savedInstanceState != null && !savedInstanceState.isEmpty()){
msDialogMessage = savedInstanceState.getString(STATE_DAILOG_MSG);
} else{
Utils.setKeyboardFocus(mEditTextUserName);
}
...
}
/**
* Used to set focus and show keyboard (if needed) for a specified text field
* #author Ty Smith
* #param primaryTextField
*/
public static void setKeyboardFocus(final EditText primaryTextField) {
(new Handler()).postDelayed(new Runnable() {
public void run() {
primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 100);
}
Although if your fragments don't play nice and the lifecycle methods aren't calling right, you might consider my other way.
I won't post code, but just put the grab focus method in a custom listener and call it from the activity when you put the fragment to the front.