Limit height of a Spinner from Layout

2

How can I limit the height of a Spinner from the layout where I have it? The problem is that on devices with smaller screens it has too much height , so I need to change it from layout

layout

        <Spinner
            android:id="@+id/spinnerzodiaco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:background="@drawable/borde_spinner" />

What I want to achieve is this:

That is smaller in height in different layout , I already know how is the theme of layout , layout-large ... But I do not know how I can modify the height of Spinner

EDITO1:

Doing so does not work, does not change the height:

clase

spin = (Spinner) findViewById(R.id.spinnerzodiaco);
    try {
        Field popup = Spinner.class.getDeclaredField("mPopup");
        popup.setAccessible(true);

        // Obtiene la variable mPopup y realiza cast a  ListPopupWindow
        android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spin);

        // Define la altura en dimens.xml
        popupWindow.setHeight(R.dimen.height_spinner);
    }
    catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
        Log.e("Exception", e.getMessage());
    }

dimens.xml

<resources>

    ...

    <dimen name="height_spinner">100dp</dimen>

</resources>

If I do so, yes:

    spin = (Spinner) findViewById(R.id.spinnerzodiaco);
    try {
        Field popup = Spinner.class.getDeclaredField("mPopup");
        popup.setAccessible(true);

        // Obtiene la variable mPopup y realiza cast a  ListPopupWindow
        android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spin);

        // Como ejemplo define la altura a 300 pixeles.
        popupWindow.setHeight(300);
    }
    catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
        Log.e("Exception", e.getMessage());
    }

But I need to have different measurements for each device so I need to use dimens.xml

    
asked by UserNameYo 13.03.2017 в 23:09
source

2 answers

1

You've tried using reflection :

  Spinner spinner = (Spinner) findViewById(R.id.spinner);
    try {
        Field popup = Spinner.class.getDeclaredField("mPopup");
        popup.setAccessible(true);

        // Obtiene la variable mPopup y realiza cast a  ListPopupWindow
        android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

        // Como ejemplo define la altura a 500 pixeles.
        popupWindow.setHeight(500);
    }
    catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
        Log.e("Exception", e.getMessage());
    }

The previous method works with pixels, if you use dp you can use the method I suggest in this answer .

// Como ejemplo define la altura a 100 dp.
popupWindow.setHeight(getPixelsfromDP(getApplicationContext(), 100));

Using the method:

public int getPixelsfromDP(Context ctx, float dps) {
      float scale = ctx.getResources().getDisplayMetrics().density;
      return (int) (dps * scale + 0.5f);
}
    
answered by 14.03.2017 в 05:49
0

On the one hand you can try different layouts for different monitor sizes by creating the corresponding res / layout-xxxx folders. But this does not always work.

A better option is to use RelativeLayout where you can position the elements with respect to others on the screen.

I think the best option is to use a ConstraintLayout that allows you to define guides where you can hook the elements by fixing their place on the screen without interesting their size.

This last option has a small learning curve but it works very well.

link

    
answered by 14.03.2017 в 00:21