Android: softkeyboard not showing up - android

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>

Related

disable popout keyboard android-studio

I have an EdiText in my project developed on android studio .
android:id="#+id/Number"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
I need to find a way to disable the keyboard when I click on the edit text, meaning that when I click on the EditText the keyboard should not open .
Just disable it.
EditText et = (EditText) findViewById(R.id.edittext);
et.setEnabled(false);
OR
Add android:inputType="none" in your xml
Do it this way
final EditText editText = findViewById(R.id.Number);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
here you go.
public static void disableSoftInputFromAppearing(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
}
In your code you need to do it programmatically this way: declare a global variable for InputMethodManager:
private InputMethodManager im ;
In onCreate() method define it:
im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);
Set the onClickListener to that edit text inside onCreate():
youredittext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
im.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
}
});
This will work.
If you want to prevent user to edit text then you may use android:enabled="false" property in edittext

Edit text focus issue

I have a activity , in a activity one edit text having some hint text.
when i start activity by default focus gone on edit text and hint invisible.
please tell me how to make hint visible
Thank you
Add in the page an empty view:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width = "0dp"
android:layout_height = "0dp"/>
This view will gain the focus and prevent EditText from getting it
Add this line in onCreate before setContentView()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
also if you have requestFocus in xml part then remove it
After setContentView():
KeyListener keyListener = new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
setInput(YOUR_EDIT_TEXT, keyListener, imm);
setInput method:
private void setInput(EditText _editText, KeyListener keyListener, InputMethodManager imm){
_editText.setKeyListener(keyListener);
if (_editText.isFocusable()) {
_editText.setFocusable(true);
_editText.setFocusableInTouchMode(true);
_editText.requestFocus();
}
_editText.setSelection(_editText.getText().length());
imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_editText, InputMethodManager.SHOW_IMPLICIT);
}

Is there any way to prevent a keyboard to show up in android

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

How to Hide keyboard when activity starts

I have an activity with lots of edittext. whenever I load that activity, the keyboard appears and eats half of the screen which makes that activity's look bad. So is there any way to hide keyboard when I load that activity.
in your onCreate() use this..
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Add this two line in your activity's XML file in the RootLayout i.e. either relative or linear(whatever you have taken) :
android:focusableInTouchMode="true"
Add this line in activity manifests file
android:windowSoftInputMode="stateHidden"
In your AndroidManifest.xml add the attribute android:windowSoftInputMode:
<activity android:name="your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
You can do this using intputmethodmangare... using the following code..
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Put this code on the onCrete function:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
view.clearFocus();
}}, 50);
where view is your EditText
The runnable is because the code might be executed before the editText is rendered.
I created a method which I call in all the required Activity classes in the onCreate event. Worked for me in all scenarios.
public class ClassLib {
public static void hideKeyboard(Activity activity) {
//Hide keyboard
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}

Soft keyboard not showing up at program startup

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

Categories

Resources