7

i tried many things but didn't get any solution, i m setting text at runtime and i need the width of popup according to text size LinearLayout viewGroup = (LinearLayout) ((Activity) context) .findViewById(R.id.popup);

        LayoutInflater layoutInflater = (LayoutInflater) context

        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
        title_tv = (TextView) layout.findViewById(R.id.popup_txt);

        title_tv.setText(title_P);


         PopupWindow popup = new PopupWindow(context);
final float SCALE = layout.getResources().getDisplayMetrics().density;
int mode = MeasureSpec.getMode(View.MeasureSpec.UNSPECIFIED);
        int measuredWidth = MeasureSpec.getSize(View.MeasureSpec.UNSPECIFIED);
 popup.setWidth(measuredWidth);
         popup.setHeight(60);


        popup.setContentView(layout);
        //popup.setWindowLayoutMode();

        popup.setFocusable(true);
nmw
  • 6,664
  • 3
  • 31
  • 32
che_chouhan
  • 95
  • 1
  • 1
  • 8
  • I think you are looking for this http://stackoverflow.com/questions/3630086/how-to-get-string-width-on-android – Suman Jan 04 '13 at 11:44
  • thanks, i tried it but same problem, i m getting text width by this formula & i m passing it to popup width as well but size is still not according to text.. – che_chouhan Jan 04 '13 at 12:13

1 Answers1

17

Use:

popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

plus, if applicable:

popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

However, make sure you're not using fill/match_parent in the contents of popup_layout, because that wouldn't make sense (i.e. popup set to wrap its content, and the content says make me as big as my parent). Post your popup_layout XML for a more exact answer.

nmw
  • 6,664
  • 3
  • 31
  • 32
  • @nmw i have tried this but popup window going outside of screen instead of upside when space is not available. i have different size of content so i can't give static width and height.. is there any solution? – Sanket Kachhela Apr 22 '15 at 09:38
  • @Sanket you got any solution? – Ram Nov 28 '16 at 07:56