Showing soft keyboard when click on grid view cell - android

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!

Related

AppCompatEditText focus Issue

I have long form (LinearLayout as root) which consist of EditTexts and TextView.
Now Issue i am facing is : After clicking the Edit Text, if i clicking on textviews then the focus of an edit text is not clearing. Hence, screen scroll to that edit text due to focus.
I have tried solution - android:focusableInTouchMode="true" in parent layout but no luck.
I have attached an image showing an issue. I have clicked on consulting doctor field which is a TextView but focus is till on email Edit text.
Please help me with some solution. Thanks.
After clicking the Edit Text, if i clicking on textviews then the focus of an edit text is not clearing. Hence, screen scroll to that edit text due to focus.
Wrap your EditText inside FrameLayout as shown below
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
public static void hideSoftKeyboard (View view)
{
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
//Call this method on textview click

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.

windowSoftInputMode="adjustResize" causes problems with custom keyboard layout

I'm developing a custom keyboard and would like to add a TextView above the keyboard to show what the user already has typed or suggestions for words he could want to type.
To do that I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="#layout/preview"
android:keyBackground="#drawable/key_background"
android:background="#color/color_primary"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/keyboard"
android:padding="8dp"
android:gravity="center"
android:background="#color/color_primary_dark"
android:textColor="#android:color/white"
android:text="some sample text"
/>
</RelativeLayout>
and the following code at the InputMethodService:
public class FancyInputMethodService extends InputMethodService {
#Override
public View onCreateInputView() {
final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.keyboard_layout, null);
final KeyboardView keyboardView = (KeyboardView) layout.findViewById(R.id.keyboard);
final Keyboard keyboard = new Keyboard(this, R.xml.qwerty);
keyboardView.setKeyboard(keyboard);
return layout;
}
}
At a normal EditText at the top of the screen the keyboard looks fine and works well:
But if the EditText is in an Activity which uses the flag android:windowSoftInputMode="adjustResize" in the manifest, the keyboard view seems to cover the actual view instead of being transparent.
The left image shows the actual view with the soft keyboard closed, the image in the middle shows the weird behavior when the keyboard is open and the image on the right shows the default keyboard with the behavior I would expect.
I already tried to set the layouts background to transparent, but that didn't help.
The problem appears at several apps, e.g. WhatsApp, Hangouts, Facebook etc...Am I missing something or what's wrong?
tl;dr
You are not using InputMethodService as intended, use the CandidateView framework instead.
Full answer:
Your keyboard layout should not include the text suggestions.
Override the InputMethodService#onCreateCandidatesView. It should look like this:
public View onCreateCandidatesView() {
mYourView = getLayoutInflater().inflate(R.layout.your_view, null);
return mYourView;
}
3. When you want to show/hide your candidate view use setCandidatesViewShown(boolean show)

In android, how to drop down edit text fields when a button is pressed

I have a button Add new address and when it is pressed, I want to show EditText fields to collect the new address details. Is there any layout to do that. Or hiding the text fields when the Button is unpressed, is that the only way to do this?
Define the edit box in a layout as below -
<LinearLayout
android:id="#+id/exp_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/exp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="10dp"
/>
</LinearLayout>
And now use the layout id to get the view like below.
LinearLayout l=(LinearLayout)findViewById(R.id.exp_linear_layout);
And just toggle the visibility on button click event -
l.setVisibility(View.GONE) and vice versa.
I hope it will help u.
There is no built in framework to do it. You can do this by setting View.SetVisibility() to visible or gone. Initially the button is visible but textfield is invisible. When user click on the button, you can set this button visibility invisible or gone and visible the text fields.

How to hide keyboard in a edittext + listview layout

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.

Categories

Resources