I want to set the width of the popup at runtime according to the size of the title in android

I tried a lot of things, but did not get any solution, I set the text at runtime, and I need the width of the popup according to the size of the text 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); 
+7
source share
1 answer

Using:

 popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); 

plus, if applicable:

 popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 

However, make sure that you do not use fill / match_parent in the popup_layout content, because that doesn't make sense (i.e. a popup menu to wrap its contents, and the content says that I made me as big as my parent ) Submit your popup_layout XML for a more accurate answer.

+17
source

All Articles