Issue with Soft Keyboard and Edit Text - android

I have an Edit Text at the bottom of my app. I want to achieve that always when it gets a Touch Event, the Soft Keyboard appears scrolling the whole window to the top, so that my EditText remains visible.
The problem is that sometimes the window does not scroll at all and the Edit Text remains hidden behind the Soft Keyboard. The weird thing is that, in those moments, if I press anything or interact with other elements in the window, apparently it refreshes or something like that and then it scrolls correctly to the top, allowing me to see the Edit Text.
I have try different things and this is my end code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<my.Custom_Edit_Text
android:id="#+id/my_custom_edit_text"
style="#style/my_custom_edit_text_Style"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:maxLength="#integer/data_title_max_length"
android:text="#string/my_custom_edit_text_default"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"
android:inputType="text"/>
This is my Custom_Edit_Text code, where I handle the onTouch event:
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
requestFocus();
}
return false;
}
});
I have also set this attribute in the Manifest file:
android:windowSoftInputMode="stateHidden|adjustPan"

use this
android:configChanges="keyboardHidden|orientation"
and
android:windowSoftInputMode="adjustPan"
Let me know using both this are helpful to you or not and also use scrollview in the layout where your virtual keyboard is creating problem it also help a bit
put your whole layout which is distroting in a scrollview

This post gave me the clue to solve my problem https://stackoverflow.com/a/5989385/1382250
I just tried to change my Edit Text for a Text View and now it works perfectly (scrolls the window in all cases).

Related

Scrollable layout when keyboard pops up, but without focus change

I have a UX problem with my layout. I want that is possible that the user can scroll over the compleat layout without a focus change when the keyboard popes up.
This is what the Layout looks like when the activity starts
This is what happens when the user clicks to the Big Text EditText
This is what I want the layout to look like, the positions should stand in the same way as before, but it should be possible to scroll to the bottom with the keyboard open
The rootView of my layout is
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
I also try to add android:windowSoftInputMode="adjustResize" and android:windowSoftInputMode="adjustPan" the the activity in the Manifest.xml but it does not solve my problem.

ClearFocus sets focus on the first view in LinearLayout

When clearFocus() on a LinearLayout is called, it sets focus on the first view in the Linearlayout. I am reading the android View document and it says
Note: When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. 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.
enter link description here
So, I checked IsInTouchMode on the LinearLayout before calling clearFocus, but IsInTouchMode is true.
I am wondering what is affecting this behaviour.
Use android:descendantFocusability="beforeDescendants" so that focus will be first on parent layout and then on their child/s.
Like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" />
Let me know if it worked or not.

Keyboard overlaps Custom edit text

I have a an EditText with a drawable background (A rectangle shape with corners and a color, nothing fancy)
Case 1 When the EditText gets focus on click, view shifts above but the soft keyboard still partially overlaps it so that the hint is visible properly but the background is trimmed.
Case 2 (Inside a fragment, coz this issue does not occur in an Activity) If I press next from an edit text above the target EditText the background is again trimmed and the keyboard is just touching the bottom of the hint.
I have enclosed it inside a ScrollView, tried android:windowSoftInputMode="stateAlwaysHidden|adjustResize" (with different combinations too) but nothing seems to work.
Is there a way to override the y-axis by which the view is pushed when soft keyboard opens?
Half of the job is done by Scrollview itself,
Try to writing these line in you onFocusChanged method of edittext
editText.requestLayout();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
and your .xml would be like -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="1">
.... your editetexts here...
</ScrollView>
</LinearLayout>

How do I display the soft keyboard when a view gains focus on android?

When my activity starts, my edittext automatically gains focus, but the soft keyboard does not appear. Furthermore when I programatically call .requestFocus() on a view, it gains focus but again the soft keyboard does not appear. An example of a view is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:id="#+id/editTextTransactionName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/edittext_description"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="actionNext"
android:inputType="text"/>
</LinearLayout>
The soft keyboard only seems to appear if I click. Is there a reason for this to be the default behaviour? When opening an activity used as a form I would intuitively expect the keyboard to appear so you can enter data immediately.
Consider reading this:
Android Actionbar Tabs and Keyboard Focus
requestFocus is extremely unreliable.
Apparently it's not a default behaviour.
If you really want keyboard to appear automatically, simulate a "tap" inside your EditText, here's what worked for me (this is safer than calling showSoftInput because of unreliable behavior of requestFocus, plus you don't need to micromanage the keyboard):
EditText tv = (EditText)findViewById(R.id.editText);
tv.post(new Runnable() {
#Override
public void run() {
Log.d("RUN", "requesting focus in runnable");
tv.requestFocusFromTouch();
tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0));
tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0));
}
});
I think the reason behind keyboard not opening up is that the user has to have a chance to see an entire window before deciding where to start editing first.

Android : clear focus on edittext when activity starts

I've some settings page in my app. Once the activity gets starts directly it focus to edittext and i used following code to clear foucs.
<RelativeLayout
android:id="#+id/RequestFocusLayout"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
and in java code
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
The above code is working fine when activity starts at first time and if same activity starts again, automatically edittext get focus.
Can anyone help me to solve this issue.
Actually, the first focusable view in the activity receives initial focus. If that happens to be your EditText, it will be initially focused.
If you don't want that, here's your options:
Focus another view
Programmatically identify what view you do want to give initial focus to.
#Override
protected void onStart() {
super.onStart();
findViewById( R.id.yourOtherViewId ).requestFocus();
}
Make an earlier view in your layout focusable
If you rather it appears as though "no view has initial focus" you could make the parent view group focusable. In the following example, I make my LinearLayout focusable by setting android:focusableInTouchMode="true":
<LinearLayout
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
...
If Come back from other activity edittext get focused.
put these line onStart() or on onReusme()
RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
focuslayout.requestFocus();
If your EditText is a child of your RelativeLayout you can use android:descendantFocusability to request the focus:
<RelativeLayout
android:id="#+id/RequestFocusLayout"
android:focusable="true"
android:layout_width="0px"
android:layout_height="0px"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" />
I used the solution shown up in this page but it didn't work. Add this attribute to your activity tag into AndroidManifest:
android:windowSoftInputMode="stateHidden"
It works perfectly.
Me realy help only android:focusableInTouchMode="true" in my parent view group.
Put the code to remove the focus in your onStart() method and it should work fine.
In the layout XML file, specify an imeOption on your EditText:
android:imeOptions="actionGo"
Next, add an action listener to your EditText in the Activity's java file
mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
Where mYourEditText is an EditText object
If on starting the Activity it focuses on EditText on the screen.
use
android:focusableInTouchMode="true"
in the parent containing that EditText. Like if EditText is inside a RelativeLayout use this line in the RelativeLayout.

Categories

Resources