I have a cordova android app.
I'm trying to prevent native android keyboard to show when user clicks on html input, or input gets focus programmatically.
It there for android something like:
keyboard.addEventListener('will-show',
functon(event){
event.disableKeyboardShow();
});
Thanks in advance!
I don't know if there is an event like you pointed. But you can prevent keyboard to showup automatically by doing this:
1) Insert on your onCreate():
InputMethodManager imm = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = this.getCurrentFocus();
if (view == null) {
view = new View(this);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
2) Then you assure your EditText can get focus when needed by adding android:focusable="true" and android:focusableInTouchMode="true" elements in the parent layout of EditText like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout" android:focusable="true" android:focusableInTouchMode="true">
If you don't want your EditText to get focus anyway, put false at elements on step 2 and insert another one: android:descendantFocusability="blocksDescendants"
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 tried to remove focus from empty editText but it isn't working correctly.
I called clearFocus() on edittext ,and then I placed break point at my onFocusChanged() function call.
Here is what happened:
onFocusChanged() called 4 times with the focused parameters values false,true,false,true.
What I thought was that onFocusChanged() must be called only once (with focused = false)
Sorry for my bad English. Any help would be appreciated.
Thanks
In xml, make parent layout
android:focusable="true"
android:focusableInTouchMode="true"
and then call clearFocus on edit text and then call parent request focus
mFragmentBase.editText.clearFocus();
mFragmentBase.parentLayout.requestFocus();
This is happening because your EditText is the first focusable view.
From the docs,
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
You can try setting a dummy focusable view above the EditText to clear the focus from it.
Instead of clearing the focus from EditText create an empty view at the top of the layout
<LinearLayout
android:id="#+id/focusableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"/>
and declare a utility function to hide the keyboard like this
fun hideKeyboard(activity: Activity) {
val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity.currentFocus?.windowToken, 0)
}
Finally when you need to clear focus just call
focusableLayout.requestFocus()
hideKeyboard(currentActivity)
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'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 textview.On clicking the texstview the edit text comes up with focus. But to get the numpad another click is required.
I want that when i click on TextView,the edit text should come up with the numpad togeter withoutanother click on the editText.Not through xml.But manually
In the xml i have already given it as android:inputType="number"
(the text view is made invisible on click)
The on click method----
case R.id.txtPhoneNo:
hintPhoneNum.setVisibility(View.VISIBLE);
phoneNum.setVisibility(View.GONE);
edtPhoneNum.setVisibility(View.VISIBLE);
edtPhoneNum.requestFocus();
edtPhoneNum.setText("");
WHen the editText gets focus how to get the numpad forcefully?
The correct answer is
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
for your EditText add the following XML:
android:inputType="number"
EDIT
Try requesting focus just after your textView was set invisible:
yourTextView.setVisibility(View.INVISIBLE); //or View.GONE
yourEditText.requestFocus();
This should display the keypad immediately
when TextView is shown or touched first it gets focus and when you click on it again it performs onClick() so you have to handle Foucus of TextView to achieve what you want.