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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sujay U N
  • 4,974
  • 11
  • 52
  • 88

2 Answers2

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.

AFin
  • 61
  • 2
  • 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. – Sujay U N Nov 09 '17 at 10:15
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"/>
Aks4125
  • 4,522
  • 4
  • 32
  • 48
  • 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. – Sujay U N Nov 13 '17 at 18:00