EquiWare
EquiWare

Reputation: 127

Changing the popup menu size

Currently, I am attempting to creating a popup menu on an ImageButton which works, though I want the size (width) of the menu to be smaller as well to inline them side by side? But that is not the main issue it is the size(width) I would like to reduce either for a png or just the word itself. I'm hoping you guys could help me figure this out. I just spent 3days figuring out the menu. Also, I have tried adding some padding and the android: width/android:layout_width. None of these have worked and I'm hoping there is a way around this issue.

Main.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;



 public class MainActivity extends AppCompatActivity
{
public WebView webView;
public EditText search_bar;
private ImageButton options_menu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView);

    //Open in Equinox instead of Deafult Browser
    webView.setWebViewClient(new
            WebViewClient());

    //WebView - JavaScript-WebViewSettings-HomePage
    final WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.getSettings().setSupportZoom(true);
    webView.loadUrl("https://www.Google.ca");

    //OptionsBtn
    options_menu = (ImageButton) findViewById(R.id.options_menu);
    options_menu.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating the instance of PopupMenu
            PopupMenu popup = new PopupMenu(MainActivity.this, options_menu);
            //Inflating the Popup using xml file
            popup.getMenuInflater()
                    .inflate(R.menu.popup_menu, popup.getMenu());

            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.back:
                            if(webView.canGoBack()){
                                webView.goBack();
                            }
                            else{
                                webView.reload();
                            }
                            return true;
                        case R.id.forward:
                            if(webView.canGoForward()){
                                webView.goForward();
                            }
                            else{
                                webView.reload();
                            }
                            return true;
                        case R.id.refresh:
                            webView.reload();
                        default:
                            return false;
                    }
                }
            });

            popup.show(); //showing popup menu
        }
    }); //closing the setOnClickListener method

    //"Go" on Keyboard with search
    search_bar=(EditText)findViewById(R.id.search_bar);
    search_bar.setOnEditorActionListener(new OnEditorActionListener(){
        @Override
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
            if(arg1 == EditorInfo.IME_ACTION_GO)
            {
                if (search_bar.getText().toString().contains(".com")) {
                    webView.loadUrl("https://".concat(search_bar.getText().toString()));
                } else {
                    webView.loadUrl("https://www.google.ca/search?q=".concat(search_bar.getText().toString()));
                }
            }
            return false;
        }
    });
}
public void onBackPressed(){
    if (true){
        webView.goBack();
    }
    else{
        webView.reload();
    }

}
}

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">


<item
    android:id="@+id/back"
    android:title="@string/back" />
<item
    android:id="@+id/forward"
    android:title="@string/forward" />
<item
    android:id="@+id/refresh"
    android:title="@string/refresh" />

Upvotes: 1

Views: 6116

Answers (1)

UMESH0492
UMESH0492

Reputation: 1731

I can reduce the height and text size of the menu items with the help of below codes:

res/values/styles.xml:

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
 
        <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
        <item name="android:actionBarWidgetTheme">@style/PopupMenuTextView</item>
        <item name="android:popupMenuStyle">@style/PopupMenu</item>
 
        <!-- Change Overflow Menu ListView Item Height & Property -->
        <item name="android:listPreferredItemHeightSmall">10dp</item>
        <item name="android:listPreferredItemPaddingLeft">5dp</item>
        <item name="android:listPreferredItemPaddingRight">5dp</item>
    </style>
 
    <!-- Change Overflow Menu ListView Divider Property -->
    <style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
        <item name="android:divider">#FF0000</item>
        <item name="android:dividerHeight">2dp</item>
    </style>
 
    <!-- Change Overflow Menu ListView Text Size and Text Size -->
     <style name="PopupMenuTextView" parent="@style/android:Theme.Holo.Light">
        <item name="android:textColor">#00FF00</item>
        <item name="android:textSize">5sp</item>
    </style>
 
     <!-- Change Overflow Menu Background -->
     <style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">#888888</item>
    </style>
 
</resources>

Add the above codes in styles.xml(v11) and styles.xml(v14).

For More Detail:

Refer Action Bar Overflow Menu Customization

Upvotes: 1

Related Questions