Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change popup item width size?

Tags:

android

xml

popup

I use popup menu with custom items.Problems like this

Red lines represents to default width size.

enter image description here

but i want custom smaller width size of items(represents red lines).

enter image description here

public void onClick(View v) {  
    //Creating the instance of PopupMenu  
    PopupMenu popup = new PopupMenu(MainLayout.this, mSikBtn);  
    popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
        public boolean onMenuItemClick(MenuItem item) {  
            
            return true;  
         }  
    });  

    popup.show();//showing popup menu  
    }  
});

Layout xml file

<ImageView
        android:id="@+id/popupBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" 
        android:src="@drawable/selector_sik"
        android:layout_weight="1"
        android:layout_margin="10dip"
        />

popup_menu.xml file

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item  
        android:id="@+id/one"  
        android:title="A)."/>  
      ...
        
    <item  
        android:id="@+id/three"  
        android:title="E)."/>  

</menu>

Similar question: stackoverflow.com I need to custom popup menu

like image 622
Asil ARSLAN Avatar asked Jan 30 '14 23:01

Asil ARSLAN


Video Answer


1 Answers

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

like image 119
mike20132013 Avatar answered Oct 02 '22 21:10

mike20132013