4

I have coded below general, everytime usable functions to display and dismiss keyboard:

|==| Display and Dismiss keyboard Programmatically:

InputMethodManager iptKeybodMgrVar; void keybodDspFnc(EditText txtEdtVyuVar) { txtEdtVyuVar.requestFocus(); if (iptKeybodMgrVar == null) { iptKeybodMgrVar = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); } iptKeybodMgrVar.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } void keybodDsmFnc(EditText txtEdtVyuVar) { iptKeybodMgrVar.hideSoftInputFromWindow(txtEdtVyuVar.getWindowToken(), 0); } 

But problem is, When the Edit Text is below Keyboard, The activity does not shift upwards. It shifts only when a user starts typing.

So how do I make it shift upwards as soon as keyboard appears?

I went thru all links below and nothing helped me:

Android: How do I prevent the soft keyboard from pushing my view up?

adjustPan not preventing keyboard from covering EditText

Android : Soft Keyboard covering edit text up (adjustresize or adjustpan doesnt work)

https://readyandroid.wordpress.com/android-soft-keyboard-covers-edittext-field-overscroll-to-soft-keyboard/

I tried and didn't work:

android:windowSoftInputMode="adjustPan|stateAlwaysHidden" android:windowSoftInputMode="adjustResize|stateAlwaysHidden" android:windowSoftInputMode="adjustResize|adjustPan" 

Also tried this codes:

(So, Not duplicate of :) Move layouts up when soft keyboard is shown?

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); //This Code brings up the keyboard at starting of the activity and pushes all Edit Text up. So not useful getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

I can add ScrollView but it seems like just workaround and doesn't seem a solution.

Feel there should be a solution programmatically like setting something InputMethodManager.

10
  • 1
    can you put your xml code ? Commented Oct 28, 2017 at 6:20
  • 1
    I am adding Relative Layout and add edit text programmatically and dynamicaly. So no XML. Commented Oct 28, 2017 at 6:22
  • 1
    the entire xml ? Commented Oct 28, 2017 at 6:23
  • 1
    Possible duplicate of Move layouts up when soft keyboard is shown? Commented Oct 28, 2017 at 6:27
  • 1
    @MehulKanzariya : I tried all in your link and dint help me. I edit my question accordingly. Commented Oct 28, 2017 at 13:06

2 Answers 2

0

You need to wrap your EditText inside LinearLayout, then wrap it with ScrollView. It little bit hard if you did via code, so my suggestion is change your xml layout.

Sign up to request clarification or add additional context in comments.

1 Comment

As I hav mentioned in Question, ScrollView seems to be a workaround but not a soltion for the issue. Adding scrollview is causing other layout problems. My Edit Text are inside relative layout and can be moved around the screen.
0

Your root layout

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/root" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!--extra views--> <!--add below layout programmatically--> <!--<include layout="@layout/layout_dynamic"/>--> </ScrollView> </RelativeLayout> 

Dynamic layout

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dynamicLinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> </LinearLayout> 

add view dynamically (no need to specify behaviour)

private void addEditText(){ RelativeLayout mRootLayout = (RelativeLayout) findViewById(R.id.root); LayoutInflater inflater = LayoutInflater.from(mActivity); View newView = inflater.inflate(R.layout.layout_dynamic,mRootLayout); EditText editText = (EditText) newView.findViewById(R.id.editText); mRootLayout.addView(newView); } 

Manifest file

 <activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" android:windowSoftInputMode="adjustPan"/> 

1 Comment

Your code didn't help me. Reason 1) ScrollView can host only on subview. 2) Edit text shud be inside relative layout since it can be movable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.