Not sure if it's relevant but I'm using LG G3 with Android 4.4.2
I have a fragment where I want the keyboard to show, so I did the following:
txtContact = (EditText) rootView.findViewById(R.id.txtContact);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtContact.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
and the layout is this, including requestFocus for the relevant field:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:lines="10"
android:layout_margin="10dp"
android:id="#+id/txtContact">
<requestFocus />
</EditText>
When opening this fragment, the cursor is where it should be but the keyboard is not there, Only when tapping inside the field, they appear.
The other problem is the opposite of the first one. On another fragment, I want to hide the keyboard when the fragment opens, no matter what. The reason for this, is that the user can open this fragment after closing a different one, where the keyboard was shown so I want to make sure it closes, notice that I don't know if the keyboard is shown, I want it to remain close if it was or to be closed if it was open. I only have a ListView in this fragment, so I did the following:
lv = (ListView) rootView.findViewById(android.R.id.list);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(lv.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
but it refuses to make any effect on the keyboard and it remains open.
What am I doing wrong?
Try to call the code in onResume() or post() it as a Runnable.
If your Fragment is a DialogFragment, calling getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); in onCreateView() should be sufficient.
Related
This question already has answers here:
How to hide Soft Keyboard when activity starts
(23 answers)
Closed 4 years ago.
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:drawableLeft="#drawable/search"
android:background="#drawable/search_bar"
android:paddingLeft="15dp"
android:drawablePadding="10dp"
android:inputType="text"
android:id="#+id/srchbar"
android:onClick="searchBarClick">
</EditText>
public void searchBarClick(View view){
}
I have an edit text box and onClick function.Please tell how to show keyboard ONLY when user click on edittext box to insert data.In normal condition,keyboard appears as i run my application but i only want it to be shown when user click on edittext box.
add this below code in the root element of your layout.
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
editText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}, 50);
sometime edittext might get focus after manually hide soft-keyboard.
so, delay some time and then after hide it forcefully in onResume() method of your activity or fragment
I use an external library called DragListView here. This DragListView extends a FrameLayout and it contains a RecyclerView inside. The structure is like:
DragListView extends FrameLayout {
RecyclerView mRecyclerView;
}
The items populated into RecyclerView contains an EditText. With the EditText near the top of the screen (which is not covered if the soft keyboard appears) works just fine. But the one which is covered by the soft keyboard will gain focus then lose focus at once when called myEditText.requestFocus(); then the keyboard covers that EditText.
Here is the layout of my activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.ui.activities.ChecklistsActivity">
<include layout="#layout/custom_action_bar"
android:id="#+id/action_bar"/>
<com.woxthebox.draglistview.DragListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:id="#+id/recycler_view_checklists"
/>
</LinearLayout>
android:layout_gravity="top" works for me once with my old structure where the DragListView is a nested child inside a RecyclerView (I set the top gravity for the RecyclerView), but it's not working anymore in this case.
I cannot use AdjustPan because it will push my action bar away (but it works, sadly). Mine app is not in fullscreen (still able to see the status bar, I assume it's not fullscreen, correct me if I'm wrong please). I tried a few things but nothing work.
I called myEditText.setFocusable(true); and myEditText.setFocusableInTouchMode(true); just before myEditText.requestFocus();.
I put android:descendantFocusability="afterDescendants" for the DragListView or the root LinearLayout.
I tried different ways of calling the keyboard to popup like
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
or with a KeyboardUtils which received the Activity for getCurrentFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
And one more thing just in case. I don't know if there's anything related to my "custom" keyboard with edtSubItemName.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER); because I want a multiline EditText with "Done" action key. The xml of the EditText item is here:
<EditText
android:id="#+id/edit_text_sub_checklist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/height_checklist_setting"
android:layout_marginRight="#dimen/height_checklist_setting"
android:inputType="text"
android:imeOptions="actionDone"
android:maxLength="256"
android:textSize="#dimen/text_size_default"
android:visibility="invisible" />
The android:visibility="invisible" is because there's a button to press to make it visible.
I'm getting mad with it for a few days. Was I missing something? Feel free to ask for more information.
Thank you all for your valuable time.
I solved it by setting layoutManager.setStackFromEnd(true); for the layout manager before setting myDragListView.setLayoutManager(layoutManager);
The list then start at the last item, so I just call myDragListView.scrollToPosition(0); after setAdapter to make it back to the top like normal. The interesting thing is that the position of the items is still the same (still from top to bottom) so there's nothing much to do.
Hope this helps someone.
I need the soft keyboard to be shown on a screen whithout an EditText - just a keyboard.
I have an idea about a game which would be controlled by a soft keyboard, but what I've found on stackoverflow - doesn't work as it should.
here's my game.xml - it's a layout to main Activity of this app :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity" >
<com.vladdrummer.textmaster.Game
android:id="#+id/game"
android:layout_width="wrap_content"
android:windowSoftInputMode="adjustResize"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
here's how it's called in Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);//this suppose to force keyboard to be on screen
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);//чтоб экран не гас
setContentView(R.layout.game);
this works only in portrait/vertical orientation and don't work in landscape/horizontal.
(In case, my Game class is extending View class and I'm working with canvas)
But I need this to be shown in horizontal mode.
there were another way to keep keyboard onscreen :
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
but that doesn't do a thing
so,please, I need a soft keyboard to be sticked on a screen, in landscape mode
Try execute the following code in a seperate theread.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(im != null) im.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
I am doing Project of Remote Administration
I am getting screen of Remote PC on mobile screen but to send keyboard events I need an invisible edittext and a button which enable and disable keyboard
if i remove edittext invisibility it works but edittext is shown on screen i don't wannt that
here is code
<EditText
android:id="#+id/KeyBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:focusable="true"
android:inputType="textVisiblePassword"
android:text=""
android:visibility="invisible" >
</EditText>
Show and hide Keyboard by setting the focus on a hidden text field
public void keyClickHandler(View v) {
EditText editText = (EditText) findViewById(R.id.KeyBoard);
editText.requestFocus();
InputMethodManager inputMgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (keyboard) {
inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
keyboard = false;
} else {
inputMgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
keyboard = true;
}
Log.d("SET", "Foucs");
}
This method is called on button click
If I remove android:visibility="invisible" from edittext then it works
You mean to hide it all?
you can use:
editText.setVisibility(View.GONE);
or
editText.setVisibility(View.INVISIBLE);
EDIT
Try this one:
editText.setBackgroundColor(color.transparent);
Another easy way to 'hide' your EditText would be to just make its Height/width to 0dp, as below, so that it is not visible to the users.
android:layout_width="match_parent"
android:layout_height="0dp"
I have a LinearLayout composed by a ListView(which loads from an AsynTask) and an EditText(so I can filter that list). Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Filtrar por nombre..." >
</EditText>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The problem is that when the activity starts, the keyboard is shown up and I only want to show it when the EditText is clicked for a search.
I've tried several methods such as writing the following in the manifest
android:windowSoftInputMode="stateHidden"
and including this in the class
InputMethodManager iMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The only thing I've achieved is to not show the keyboard but not showing the loaded stuff of the list(just the EditText and the rest of the screen plain black), and once I click on the EditText, they appear both keyboard and the stuff of the listView.
Any posible solution for this?
EDIT:
I've tried the following solutions(thanks to #Sush, #boztalay and #Agata Sworowska), but none did the trick.
-Placed in my layout:
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
-And placed in my onCreate() method:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
Any other posibility?
NEW EDIT:
I've noticed that when it doesn´t focus on the EditText, the listView doesn´t load until I focus back the EditText, llike if it was waiting for it. I've tried to set up the EditText after the code that loads the ListView but the problem persists.
This sounds like an issue with focus. What's happening is when the app is started, the EditText gets focus, and the keyboard pops up. You should try to make it so that the ListView gets focus. This way, the keyboard won't pop up until the user gives the EditText focus when they click on it.
In your Java, you should get an instance of the ListView, then call a couple of functions:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
If you put that in your onCreate(), focus will be given to the ListView, and they keyboard won't come up until the user clicks the EditText.
See this SO question for more details.
You can just add below two attributes to your LinearLayout.
android:focusable="true"
android:focusableInTouchMode="true"
Try adding in Your layout, that contains EditText this:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
I hope it helps.
http://www.mysamplecode.com/2012/02/android-edittext-hide-soft-keyboard.html
This is a solution I found for the issue here. Do not understand why listivew should do this, but the workaround helps.