I want to make a dynamic size ListPopupView . It may have a different number of points, but it is known which of them is the longest (in the text). Let it be a concrete example.

 String[] types = new String[]{"radix", "fructus", "flores", "herba"}; popupAdapter = new ArrayAdapter<>(context, R.layout.popup_item, types); selectTypeBTN.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { selectTypeLPV.setAdapter(popupAdapter); selectTypeLPV.setWidth(???); selectTypeLPV.setHeight(???); selectTypeLPV.setAnchorView(view); selectTypeLPV.setModal(true); selectTypeLPV.show(); } }); 

if interested - popup_item.xml

 <?xml version="1.0" encoding="utf-8"?> <com.iam.herbaldairy.widget.text.Text xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dp" android:background="@color/white" android:textColor="@color/black" android:id="@+id/popup_text" android:textSize="14sp"/> 

Text - an ordinary TextView with its own Typeface Now you need to actually calculate the sizes of the ListPopupItem

 final Text text = (Text) ((AppCompatActivity) context).getLayoutInflater().inflate(R.layout.popup_item, null); text.setText("fructus"); text.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); Log.d("len", text.getMeasuredWidth() + ""); Log.d("len", text.getMeasuredHeight() + ""); 

Then put these dimensions into the appropriate ListPopupView setters ListPopupView

 selectTypeLPV.setWidth(text.getMeasuredWidth() + text.getPaddingLeft() + text.getPaddingRight()); selectTypeLPV.setHeight(types.length * (text.getMeasuredHeight() + text.getPaddingTop() + text.getPaddingBottom())); 

It seems everything, we launch, we check and we receive the same result on different screens enter image description here

Those. the length is calculated shorter than necessary, the height is greater than necessary, hence the question - what did I miss and what was superfluous?

  • What is this ListPopupView , your own widget? Why not use the usual PopupMenu , which itself calculates the desired size? - pavlofff
  • @pavlofff developer .
  • @pavlofff PopupMenu is a vertical alignment solution, but on the horizontal it is wider than I would like. - iamthevoid

0