How can I change popup item width size?
Just like you, I tried a lot to change the width of the popup menu but was unsuccessful. Somehow, I found a solution you might be looking for. First, instead of using popup menu, I used popup window.
Step1: Making a row layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@android:drawable/list_selector_background"
android:orientation="vertical"
android:padding="3dp">
<TextView
android:id="@+id/ItemA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="3dp"
android:text="A)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/ItemB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="B)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/ItemC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="C)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/ItemD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="D)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
</LinearLayout>
Step 2: Made a method to initialize popup window:
private PopupWindow initiatePopupWindow() {
try {
mInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.row, null);
//If you want to add any listeners to your textviews, these are two //textviews.
final TextView itema = (TextView) layout.findViewById(R.id.ItemA);
final TextView itemb = (TextView) layout.findViewById(R.id.ItemB);
layout.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
mDropdown = new PopupWindow(layout,FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,true);
Drawable background = getResources().getDrawable(android.R.drawable.editbox_dropdown_dark_frame);
mDropdown.setBackgroundDrawable(background);
mDropdown.showAsDropDown(pop, 5, 5);
} catch (Exception e) {
e.printStackTrace();
}
return mDropdown;
}
Step 3: Simply calling it in the onCreate():
public class MainActivity extends Activity {
ImageButton red, blue;
private PopupWindow mDropdown = null;
LayoutInflater mInflater;
Button pop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pop = (Button)findViewById(R.id.button1);
pop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
}
});
}
}
Hope this helps you..If it does, accept my answer..:)
Here's the screenshot:
http://imageshack.com/a/img585/7388/dxjo.png