-1

I want to check if soft keyboard is visible on screen. For that I found a code and tried it as given . But both activityRootView.getRootView().getHeight() and activityRootView.getHeight() returns zero . I need your expert view.

Code:

LayoutInflater inflator5=   
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View    row5 =     
inflator5.inflate(R.layout.order_confirmation_list2,null);



final LinearLayout activityRootView = (LinearLayout)row5. 
findViewById(R.id.orderconfirmRootId);


// calculate the difference between the root view height and the window view height
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

// if more than 100 pixels, its probably a keyboard...
if (heightDiff > 100) {

// keyboard is visible, do something here

Toast toast = Toast.makeText(context, "keyboard visible!!!"   , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

} else {

// keyboard is not visible, do something here
       }

Update to this question:

I followed many answers related to this type but but didnt found suitable.plz go through my newly added code.

How can i get the difference of height of a layout inside getview() method when soft keyboard is visible and hidden? I tried this inside ((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener in my code given below. But always get return zero by getHeight() method.
Your expert view always welcome.

NEW CODE:

@Override
public View getView(int i, final View view, final ViewGroup viewgroup) {
    row=view;
    holder=null;
    try
    {
        if(row == null)
        {

            Button RemoveOrderBtn;
            LayoutInflater inflator=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflator.inflate(R.layout.add_to_orderlist2,viewgroup , false);
            holder=new MyViewHolder(row);
            row.setTag(holder);
            Log.d("newrows", "new row");

        }
        else
        {

        holder= (MyViewHolder) row.getTag();
        Log.d("recycling", "Recycling stuff");

        }
    }
    catch(Exception e)
    {
        e.printStackTrace();

    }


try
{

SingleRow temp=list.get(i);
holder.Dno.setText(temp.Dno);
holder.Size.setText(temp.size);
holder.Mrp.setText(temp.Mrp);
holder.Color.setText(temp.color);
holder.SingleOrderTotal.setText(temp.val_total);
holder.P_Category.setText(temp.Pcategory);
holder.editTextQtyId.setText(temp.val_count);
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + "/App" +temp.imageName);
FileInputStream streamIn;
  try {
    streamIn = new FileInputStream(file);
    Bitmap bitmap = BitmapFactory.decodeStream(streamIn); 
    holder.Image.setImageBitmap(bitmap);
    streamIn.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
    }

catch(Exception e)
{
    e.printStackTrace();
}


 try
    {


     if(view!=null)
         {              
            ((EditText) view.findViewById(R.id.editTextQtyId)).setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {

                    ((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener(new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {

                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1,
                                int arg2, int arg3) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1,
                                int arg2, int arg3) {
                            // TODO Auto-generated method stub


                            LayoutInflater inflator5=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            View    row5 = inflator5.inflate(R.layout.order_confirmation_list2,null);
                            final LinearLayout layout = (LinearLayout)row5. findViewById(R.id.orderconfirmRootId);
                             Log.d("Log", "Height: " + layout.getHeight());
                             Toast toast = Toast.makeText(context, "Height= "+layout.getHeight() , Toast.LENGTH_SHORT);
                             toast.setGravity(Gravity.CENTER, 0, 0);
                             toast.show();

                        }

                    });

                    return false;
                }
            });
         }

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
return row;
}
user177913
  • 465
  • 2
  • 8
  • 19

1 Answers1

1

Do you trying to get height in onCreate() method?

The views are not built yet in onCreate(), onStart(), or onResume(). Since they technically don't exist (as far as the ViewGroup is concerned), their dimensions are 0.

You should use OnGlobalLayoutListener.

Something like this:

//parent
ViewTreeObserver vto = v.getViewTreeObserver();  
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
            @SuppressWarnings("deprecation")
            @Override  
            public void onGlobalLayout() {  
                if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) 
                    v.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                else
                    v.getViewTreeObserver().removeOnGlobalLayoutListener(this);


                View myView = v.findViewById(R.id.my_view);
                // here height is not 0
                int h = myView.getHeight();

            }  
        });