My layout requires itself to adapt to the available space when the keyboard appears, so I have this set in my Manifest:
android:windowSoftInputMode="stateVisible|adjustResize"
How does WindowSoftInputMode work, though? How does it choose what to adjust (padding, margins, image size, etc.)?
Is it possible to not resize a specific view (like an ImageView) and instead take away more padding? Can I choose what it resizes (besides just adjustResize and adjustPan)?
Completely impractical to handle resizing UI elements yourself and I understand it is discouraged. You can use the below code to explicitly handle soft input:
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
Check out THIS on how to handle the keyboard...
Related
I have a android view layout structure as follows
<RelativeLayout>
<TextView>
<EditText>
</RelativeLayout>
The EditText, due to my limited layout skills, is sized as wrap_content and is significantly smaller than the parent RelativeLayout.
But when user touches on RelativeLayout, I would like the UI to effectively behave as if user just focused on the EditText instead.
It doesn't matter to me if cursor starts in the front, middle, or end. (Preferably at the end).
Is this something that i can achieve in the code or on the layout?
Thanks!
What you can do is to call the editText's onClick in the onClick method of the relative layout. This way all clicks on the relativeLayout go to the editText.
This should work see answer:
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusableInTouchMode(true);
editText.requestFocus();
//needed for some older devices.
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
I would do this programatically (in code), because it does not handle how the views look, but how they behave. Therefore, say in MVC, it belongs in the controller.
I have a layout like below :
<ScrollView ...>
<LinearLayout...>
<EditText
..../>
</LinearLayout>
</ScrollView>
As soon the fragment loads we set the focus of the EditText so that it displays Keyboard.
We have also set the background :
mParent = inflater.inflate(R.layout.sample_layout, container, false);
mParent.setBackgroundResource(R.drawable.sample_bg);
The issue is that when the keyboard is shown the background resizes or the keyboard pushes the background image up.
mEdTxt.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEdTxt, InputMethodManager.SHOW_IMPLICIT);
mEdTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
We have tried :
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
I have seen many posts revelant to this but didnt help me.
EDIT : Question is not Duplicate
How ?
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
The above code will disable the scroller.
We need the keyboard to be shown also the scroller to be present so that the user can scroll the page when the keyboard is shown.
I hope its clear.
Sounds like you have in the Activity node in your Manifest:
android:windowSoftInputMode="adjustResize"
If you don't want it to resize, remove this or change it to something different.
http://developer.android.com/guide/topics/manifest/activity-element.html
I have a layout which contains a ListView and a EditText below the ListView which is used for searching in list view. The problem is that when i close the soft keyboard after a search, it gives a jerk and shows white space while closing the soft keyboard. I have tried adjustPan, but it moves the whole layout up.
public static void hideKeyboard(Context context,View v)
{
InputMethodManager imm = (InputMethodManager)context.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Hope this helps!
I'm facing a strange problem.
When I add an edittext using the following piece of code:
edit = new EditText(context);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.layout(0, 0, (int)searchPane[W], (int)searchPane[H]);
edit.setBackgroundColor(Color.TRANSPARENT);
edit.setGravity(Gravity.CENTER_VERTICAL);
edit.addTextChangedListener(SearchBox.this);
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
and draw it using edit.draw(Canvas) in the onDraw method of View.
I'm calling edit.requestFocus() in onTouchMethod though it does not show the input keyboard on focus request. Any ideas how to make edittext accept text and show input keyboard?
Thanks in advance.
Your EditText isn't part of the View hierarchy as you've never added it as a child to your view group; as such it will not receive touch events (among other things) unless you implement that in your view group.
Add a call to addView(edit) and move edit.layout(0, 0, (int)searchPane[W], (int)searchPane[H]) into onLayout.
It probably won't be as simple as that, creating custom views is an advanced undertaking - are you sure what you're trying to do isn't possible using one (or combination) of the existing view layouts?
If not, you should read http://developer.android.com/training/custom-views/index.html
The only combination which worked for me :
edit.requestFocus()
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_FORCED);
Paired with adding input mode to the activity
<activity android:name="..."
android:windowSoftInputMode="adjustResize" />
I have a numberpicker with a plus and minus button and a edittext.
The user can click on the plus and minus button to increase / decrease or manually input when he clicks on the edittext.
But each time the user clicks a +/- button the text changes and the edittext receives focus.
This means the border changes color and the cursor shows up.
I want to avoid that, how can I do this?
I tried adding clearFocus() to the onTextChanged listener, and this works but the border still changes color for a moment.
In your XML file where you have your editText, you can set the focusable to false:
android:focusable="false"
To show the keyboard manually, use:
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
try{
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.SHOW_FORCED);
}
catch (Exception e)
{}
And put this on a listener.