Scorpion
Scorpion

Reputation: 6891

Keyboard hides edittext inside scrollview

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/btnAddRow"
        android:layout_below="@id/llInventoryViewHeader"
        android:isScrollContainer="true"
        android:scrollbars="vertical" >

        <LinearLayout
            android:id="@+id/llDynamicView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

Code which set rows in linear layout :-

/**
     * Set Edit text
     */
    private void setUsedList() {

        for (final InventoryResource inventoryResource : mCurrentUsedResourceList) {
            final LinearLayout LL = new LinearLayout(InventoryActivity.this);
            LL.setOrientation(LinearLayout.HORIZONTAL);

            final LayoutParams LLParams = new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

            LL.setWeightSum(10f);
            LL.setLayoutParams(LLParams);

            // ResourceName Params
            final LinearLayout.LayoutParams resourceViewParams = new LinearLayout.LayoutParams(
                    0, LinearLayout.LayoutParams.WRAP_CONTENT);
            resourceViewParams.weight = 6f;
            resourceViewParams.setMargins(5, 5, 5, 5);

            // Resource Edittext
            final EditText edtTextResourceName = new EditText(
                    InventoryActivity.this);
            edtTextResourceName.setGravity(Gravity.CENTER);
            edtTextResourceName.setLayoutParams(resourceViewParams);
            edtTextResourceName.setInputType(InputType.TYPE_CLASS_TEXT);
            edtTextResourceName.setTextColor(Color.BLACK);
            edtTextResourceName.setTextSize(16f);
            edtTextResourceName.setBackground(getResources().getDrawable(
                    R.drawable.box_edt_values));

            // Amount Params
            final LinearLayout.LayoutParams amtViewParams = new LinearLayout.LayoutParams(
                    0, LinearLayout.LayoutParams.WRAP_CONTENT);
            amtViewParams.weight = 2f;
            amtViewParams.setMargins(5, 5, 5, 5);

            final EditText edtTextConstructorAmt = new EditText(
                    InventoryActivity.this);
            edtTextConstructorAmt.setGravity(Gravity.CENTER);
            edtTextConstructorAmt.setInputType(InputType.TYPE_CLASS_PHONE);
            edtTextConstructorAmt.setLayoutParams(amtViewParams);
            edtTextConstructorAmt.setTextColor(Color.BLACK);
            edtTextConstructorAmt.setTextSize(16f);
            edtTextConstructorAmt.setBackground(getResources().getDrawable(
                    R.drawable.box_edt_values));


            final EditText edtTextInspectorAmt = new EditText(
                    InventoryActivity.this);
            edtTextInspectorAmt.setInputType(InputType.TYPE_CLASS_PHONE);
            edtTextInspectorAmt.setGravity(Gravity.CENTER);
            edtTextInspectorAmt.setLayoutParams(amtViewParams);
            edtTextInspectorAmt.setTextColor(Color.BLACK);
            edtTextInspectorAmt.setTextSize(16f);
            edtTextInspectorAmt.setBackground(getResources().getDrawable(
                    R.drawable.box_edt_values));

            final InventoryPojo pojo = new InventoryPojo();
            pojo.id = inventoryResource.getOrderNum();
            mOrderNumber += 1;
            pojo.edtResourceName = edtTextResourceName;
            pojo.edtConstructoreAmt = edtTextConstructorAmt;
            pojo.edtInspectoreAmt = edtTextInspectorAmt;
            mUsedList.add(pojo);

            if (mPreference.getString(Preferences.LAN_CULTURE,
                    Constants.CULTURE_HEBREW).equalsIgnoreCase(
                    Constants.CULTURE_ENGLISH)) {
                LL.addView(edtTextResourceName);
                LL.addView(edtTextConstructorAmt);
                LL.addView(edtTextInspectorAmt);
                mLLDetails.addView(LL);
                mLLDetails.invalidate();
            } else {
                LL.addView(edtTextInspectorAmt);
                LL.addView(edtTextConstructorAmt);
                LL.addView(edtTextResourceName);
                mLLDetails.addView(LL);
                mLLDetails.invalidate();
            }
        }
    }

Code :-

parent = (RelativeLayout)findViewById(R.id.rlParent);

parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            public void onGlobalLayout(){
                  int heightDiff = parent.getRootView().getHeight()- parent.getHeight();
                  // IF height diff is more then 150, consider keyboard as visible.
                  if(heightDiff > 150){
                      // Its keyboard mostly
                      parent.setPadding(0, 0, 0, heightDiff);
                  }
                  else if(heightDiff < 150){
                       // Keyboard goes away, readjust
                      parent.setPadding(0, 0, 0, 0);
                  }
               }
         });

I am having scroll view inside which dynamically rows are being added. The problem i am facing is that if my view is having 10 rows then when i start typing it won't scroll upto end. For e.g. in case of 10 rows I am able to scroll up to 7 row and then other 3 rows are not visible and have to close the keyboard by pressing back and then i can add the value to rest 3 row.

I have added inputMode to adjustPan in my manifest for the activity and also added android:isScrollContainer="true" but still its not working.

Anyone having any idea how to resolve it.

Upvotes: 0

Views: 2866

Answers (3)

Techfist
Techfist

Reputation: 4344

Okay, try this it might save your cause,

  1. first in your main XML which holds this Scroll view give identefier to its root i.e its parent

  2. Second, android does provide you an API which tell you about dimensions of its view which are assigned to them right before they are drawn, this you can read through ViewTreeObservers

  3. use this code to check when keyboard is inflated, when its inflated, you can assign the height differece as padding bottom to your parent view, and when keyboard goes away just reset the padding set. this will make sure you can scroll all view which are hidden beneath inflated keyboad.

    parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
        public void onGlobalLayout(){
            int heightDiff = parent.getRootView().getHeight()- parent.getHeight();
            // IF height diff is more then 150, consider keyboard as visible.
            if(heightDiff > 150){
                // Its keyboard mostly
                parent.setPadding(0, 0, 0, heightDiff);
            }
            else if(heightDiff < 150){
                // Keyboard goes away, readjust
                parent.setPadding(0, 0, 0, 0);
            }
       }
    });
    

4 make sure you have this parameter defined in you activity in manifiest file

android:windowSoftInputMode="adjustResize"

Upvotes: 2

Jatin
Jatin

Reputation: 1670

Your scrollview seems to be fine. For doing scroll you just have to add single line inside your activity of manifiest file.

android:windowSoftInputMode="adjustResize"

Upvotes: 0

Sipty
Sipty

Reputation: 1154

You might want to play around with the windowSoftInputMode. And here's an interesting discussion that helped me solve my similar problem.

Hope this helps you!

Upvotes: 1

Related Questions