How to limit the height of Spinner drop down view in Android

后端 未结 6 784
盖世英雄少女心
盖世英雄少女心 2020-11-28 04:34

Please suggest any approach which i use to create it .

Query : I am creating 2-Spinner view , where i have to add Country/Cities li

相关标签:
6条回答
  • 2020-11-28 05:08

    As of 2020 I would go with: Exposed Dropdown Menus and use under AutoCompleteTextView

    android:dropDownHeight="300dp"
    

    If you don't know what is this all about start exploring: Menu-Display & Menu-Documentation

    0 讨论(0)
  • 2020-11-28 05:11

    You can use this awesome library MaterialSpinner that will do all the hard work for you.

    Download: implementation 'com.jaredrummler:material-spinner:1.3.1'

    fileName.xml :

    <com.jaredrummler.materialspinner.MaterialSpinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:ms_dropdown_max_height="400dp"/>
    

    set height using app:ms_dropdown_max_height="400dp"

    0 讨论(0)
  • 2020-11-28 05:16

    You can use Reflection.

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        try {
            Field popup = Spinner.class.getDeclaredField("mPopup");
            popup.setAccessible(true);
    
            // Get private mPopup member variable and try cast to ListPopupWindow
            android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
    
            // Set popupWindow height to 500px
            popupWindow.setHeight(500);
        }
        catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
            // silently fail...
        }
    
    0 讨论(0)
  • 2020-11-28 05:27

    for that i have created my own , PopUpWindow as suggested by @theLittleNaruto , in the comment section .

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
    
        <Button 
            android:layout_marginTop="80dp"
            android:id="@+id/btn"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="Country"
            android:layout_gravity="center_vertical|center_horizontal"/>
    </LinearLayout>
    

    popup_example.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >
    
        <ListView 
            android:id="@+id/lstview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    
    </LinearLayout>
    

    showpopup_1.java

    package com.example.spinnerworking;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ListView;
    import android.widget.PopupWindow;
    import android.widget.TextView;
    import android.widget.PopupWindow.OnDismissListener;
    import android.widget.Toast;
    
    public class showpopup_1 extends Activity {
    
        boolean click = true ;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
            final LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final Button b = (Button) findViewById(R.id.btn);
            final View pview = inflater.inflate(R.layout.popup_example,
                    (ViewGroup) findViewById(R.layout.main));
            final PopupWindow pw = new PopupWindow(pview);
            Log.i("hello", "hello") ;
    
            b.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (click) {
                        // if onclick written here, it gives null pointer exception.
                        // if onclick is written here it gives runtime exception.
                        pw.showAtLocation(v, Gravity.CENTER_HORIZONTAL, 0, 0);
                        pw.update(8, 0, 150, 200);
                        String[] array = new String[] { "tushar", "pandey",
                                "almora" };
    
                        ListView lst = (ListView) pview.findViewById(R.id.lstview);
                        adapterHello adapter = new adapterHello(showpopup_1.this);
                        lst.setAdapter(adapter);
                        lst.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int arg2, long arg3) {
                                Toast.makeText(showpopup_1.this, "pandey",
                                        Toast.LENGTH_SHORT).show();
                            }
    
                        });
                        click = false ;
                    }
                    else
                    {
                        pw.dismiss();
                        click = true;
                    }
    
                }
            });
        }
    }
    
    class adapterHello extends BaseAdapter {
        String array[] = new String[] { "tushar", "pandey", "almora", "hello",
                "tushar", "pandey", "almora", "hello", "tushar", "pandey",
                "almora", "hello" };
    
        showpopup_1 context;
    
        public adapterHello(showpopup_1 context) {
            this.context = context;
        }
    
        public int getCount() {
            // TODO Auto-generated method stub
            return array.length;
        }
    
        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView text = new TextView(context);
            text.setHeight(30);
            text.setPadding(10, 8, 0, 0);
            text.setTextColor(Color.BLACK);
            text.setText(array[position]);
            text.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.i("clicked", "tushar");
                }
            });
            return text;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 05:29
    1. add android:popupBackground="#00000000" to the Spinner
    2. in the Adapter

    getDropDownView();
    parentParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, (int) Utils.convertDpToPx(350));
    parentParams.gravity = Gravity.BOTTOM;
    parent.setLayoutParams(parentParams);
    
    1. you can move the popup by adding android:dropDownVerticalOffset="60dp"
    0 讨论(0)
  • 2020-11-28 05:30

    You can also affect drop down view location and size by subclassing Spinner and overriding its getWindowVisibleDisplayFrame(Rect outRect) which is used by android.widget.PopupWindow for calculations. Just set outRect to limit the area where drop down view can be displayed.

    This approach is of course not suitable for all scenarios since sometimes you want to place drop down view so it doesn't obscure another view or by some other condition known only "outside the instance".

    In my case, I needed to apply FLAG_LAYOUT_NO_LIMITS flag to my activity window which caused the outRect to be huge and therefore part of the drop down view got sometimes hidden behind navigation bar. In order to restore original behavior I used the following override:

    @Override
    public void getWindowVisibleDisplayFrame(Rect outRect) {
        WindowManager wm = (WindowManager) getContext.getSystemService(Context.WINDOW_SERVICE);
        Display d = wm.getDefaultDisplay();
        d.getRectSize(outRect);
        outRect.set(outRect.left, <STATUS BAR HEIGHT>, outRect.right, outRect.bottom);
    }
    
    0 讨论(0)
提交回复
热议问题