-2

I am working on project . I need the width & Height of a LinearLayout from Activity using programming code. This Linear Layout has fixed width and Height . But when i use the following ..i am getting Nullpointer Exception

LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
Log.e("getWidth",""+viewGroup.getWidth());
Log.e("getHeight",""+viewGroup.getHeight());

I need the width and height of that layout from activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="150dp"
android:layout_height="252dp"
android:background="#303030"
android:orientation="vertical" >
</LinearLayout>

Here is the Java code file

public class MainActivity extends Activity {

//The "x" and "y" position of the "Show Button" on screen.
Point p;
Button btn_show;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

  btn_show = (Button) findViewById(R.id.show_popup);

}



@Override
protected void onResume() {
    super.onResume();
     btn_show.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View arg0) {

           //Open popup window
           if (p != null)
           showPopup( p);
         }
       });
}



// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
@Override
public void onWindowFocusChanged(boolean hasFocus) {

   int[] location = new int[2];
   Button button = (Button) findViewById(R.id.show_popup);

   // Get the x, y location and store it in the location[] array
   // location[0] = x, location[1] = y.
   button.getLocationOnScreen(location);

   //Initialize the Point with x, and y positions
   p = new Point();
   p.x = location[0];
   p.y = location[1];
}

// The method that displays the popup.
private void showPopup( Point p) {
   int popupWidth = 200;
   int popupHeight = 380;

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popup);
   LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.a, viewGroup);
   Log.e("getWidth",""+viewGroup.getWidth());
   Log.e("getHeight",""+viewGroup.getHeight());

   // Creating the PopupWindow
   final PopupWindow popup = new PopupWindow(getApplicationContext());
   popup.setContentView(layout);
   popup.setWidth(viewGroup.getWidth());
   popup.setHeight(viewGroup.getHeight());
   popup.setFocusable(true);

   // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
   int OFFSET_X = 30;
   int OFFSET_Y = 30;

   // Clear the default translucent background
   popup.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

   // Getting a reference to Close button, and close the popup when clicked.
  // Button close = (Button) layout.findViewById(R.id.close);
  /* close.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
       popup.dismiss();
     }
   });*/
}
}

4 Answers4

0

It may be that this ViewGroup hasn't been created yet.

Check where you're trying to get the width and height of this object is actually being called after display objects such as views etc are being created. You can debug this by either placing breakpoints in the different loading methods such as onCreate, onResume or by placing NSLog's in them instead.

StuStirling
  • 15,601
  • 23
  • 93
  • 150
0

Only once the method View.onSizeChanged() has been called for the first can you reliably use the getHeight() and getWidth() methods. This means you will have to change the logic of your app to take into account this fact.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • where i use this method? – Yajneshwar Mandal Aug 28 '13 at 11:34
  • I'm not sure exactly how to do this but I have only created custom Views and not custom Layouts but Layouts are descendents of View. What I think you will have to do is create a Java class of the same name as your layout which is a descendent of the one of the layout classes. You will then need to override the onSizeChanged() method and create the pop-up in that method as the size will be known. You then have to alter your XML layout to use your custom layout instead of the standard Android ones. See http://developer.android.com/reference/android/view/ViewGroup.html – D-Dᴙum Aug 28 '13 at 11:55
0

You are inflating the layout just to get the width and height, aren't you? If so you don't need the viewGroup. Assuming R.layout.popup_layout points to a LinearLayout that has fixed dimensions:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = layoutInflater.inflate(R.layout.popup_layout, null);
LayoutParams params = layout.getLayoutParams();
Log.e("getWidth",""+params.width);
Log.e("getHeight",""+params.height);

After that you can set your popup:

final PopupWindow popup = new PopupWindow(getApplicationContext());
popup.setContentView(layout);
popup.setWidth(params.width);
popup.setHeight(params.height);
popup.setFocusable(true);
prc
  • 860
  • 7
  • 16
0

to wait for the views to be attached and placed in the layout you should useViewTreeObserver.OnGlobalLayoutListener.

you can register it like this:

v.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {

 public void onGlobalLayout() {

          //TODO: do your stuff here

          //if you change something in the layout you have to add this
          //line to avoid loops
          v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  }

});

in that callback you can be sure to have the views with a consistence position and dimension.

Mario Lenci
  • 10,422
  • 5
  • 39
  • 50