How to hide keyboard in a edittext + listview layout - android

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.

Related

Can't show soft keyboard when EditText at the bottom of recyclerView is clicked

Here's my problem: I have an RecyclerView which contains a lot of editText.
When I click on the editText at the end of the recycler my soft keyboard shows and then hide instantly. (I think it may be because the soft keyboard would hide the editText).
I set
android:windowSoftInputMode="adjustResize"
In my activity and it didn't solve my problem.
I also tried to
recyclerView.scrollToPosition(recyclerView.size() - 1);
But it didn't work either.
Here's the xml code of my recyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/message_editor_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey_lightest"
android:divider="#null"
android:dividerHeight="0dp"
android:scrollbars="none"
/>
Thanks a lot for your help.

Android - AdjustResize won't work with external library that extends FrameLayout

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.

Showing soft keyboard when click on grid view cell

I have a grid view and want to show a keyboard when the user clicks on a cell. The input of the keyboard will then appear in the cell. I've tried the code below which I got from Android: show soft keyboard automatically when focus is on an EditText and other similar questions. I can't get the keyboard to show up, though.
Any ideas on this?
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(gridView, InputMethodManager.SHOW_IMPLICIT);
}
});
I have tried InputMethodManager.SHOW_FORCED but that didn't help either.
Thanks
EDIT
The cell layout for the grid is below. I changed it to an EditText but still no keyboard. Having it as EditText doesn't look too good for me from a UI point of view. Ideally, I was the TextView and then perhaps when the user clicks it becomes an EditText so the user can enter something.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<EditText
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/celllabel"
android:textSize="15px" >
</EditText>
My idea is that it stays as a TextView. The keyboard opens and when the user clicks one letter the keyboard closes and that a letter appears in the GridCell. Is my idea possible?
Thanks
One suggestion:
Create a custom EditText, you can hide the cursor if you want (make it looks like a TextView).
Optimize your cell layout by removing the LinearLayout
Code:
<?xml version="1.0" encoding="utf-8"?>
<YourCustomEditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_item_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#+id/celllabel"
android:maxLength="1"
android:textSize="15px" >
</YourCustomEditText>
Use TextWatcher or focus interface to control the show/hide of the keyboard.
Hope this help!

Android: Prevent focus from jumping when hiding view

I have an Android activity that contains multiple EditText views.
If I programmatically hide the currently focused EditText, then the focus will automatically jump to the next EditText.
Is there anyway to disable this behavior so that the focus is cleared when the EditText is hidden rather than automatically jumping to the next EditText?
Thanks.
You need to provide Focus to a parent view above your EditText just like the sample code below
<RelativeLayout
android:id="#+id/search_edit_text_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText...
Do try this to avoid providing focus directly to the next EditText

How to prevent lifting listview when keyboard goes up

This is code MainActivity java and main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#75F575">
<LinearLayout
android:clickable="true"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#C69817"
android:id="#+id/secondLayout">
<ListView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/mainListView1"/>
</LinearLayout>
</LinearLayout>
Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.mainListView1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 ,
new String [] {"item1","item2"});
lv.setAdapter(a);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
When keyboard is shown, listview lifting up.How to prevent this.
Without listview - all normal(layout NOT go up).
Thank you.
UPDATE QUESTION with answer to FOliveira.
Unfortunately i can't remove java code (setSoftInputMode) in my real app. Java code must be and layout with listview must NOT GO UP. Try to remove listview, and you will see the layout not moving absolutely, Why layout with listview is moving? How prevent this according my conditions?
You need to add android:windowSoftInputMode="adjustPan" to your tag in the AndroidManifest.xml file.
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
If this option does not fit your needs, you can allways check Android documention about soft input mode
I'm using android:windowSoftInputMode="stateVisible|adjustNothing" , but this only for API higher 10(unfortunately).
Update:
After adding to listview this attributes:
android:focusableInTouchMode="false"
android:isScrollContainer="false"
I'm completely solved my problem.

Categories

Resources