Change custom spinner selector

Asked

Viewed 3,255 times

0

I created a custom spinner and a custom dropdown spinner, when using them, the selector is almost invisible and its color also does not help. I wanted my spinner selector to be bigger, it could be the default size and wanted another color, to get over the background of the layout. (This bold is my problem)

I tried passing a color reference to the attribute in xml android:dropDownSelector and yet it didn’t work. I’ll put pictures here of two spinners.

Spinner não selecionado

This image refers to a standard spinner. Created with this layout: android.R.layout.simple_spinner_item and then the dropdown layout configured this way: android.R.layout.simple_spinner_dropdown_item on the Adapter. In the image above the spinner was not touched by the user, the image below refers to the same spinner only with the difference that it is being touched by the user.

Spinner selecionado


Now if I happen to use a custom spinner and a custom dropdown for the Adapter, the selector is almost invisible as I said. Let’s see now images of how my custom spinner turned out. Custom Spinner não selecionado

The same custom spinner below only now selected. Custom Spinner selecionado

Below follows the code of my custom spinner and custom spinner dropdown(This is configured in Adapter)

custom_spinner_item.xml

<TextView
    android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#fff"/>

<TextView
    android:id="@+id/text2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#fff"/>

</LinearLayout>

custom_spinner_dropdown_item.xml

<LinearLayout
android:id="@+id/linearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#232323"
android:orientation="horizontal">

<TextView
    android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#fff"/>

<TextView
    android:id="@+id/text2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#fff"/>

</LinearLayout>

Once again remembering. I want the selector of mine custom spinner be larger and have a different color for about hanging out in the background. The size of the selector could be the size of the default selector. I’d also like to know why when I used one custom spinner the size of the dial has become almost invisible and not the original size.


Fragment layout:

<LinearLayout
android:id="@+id/layoutMain"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0F2028"
android:orientation="vertical"
android:padding="5dp">

<Switch
    android:id="@+id/switchButtonConcluidoIM"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:text="@string/concluido"
    android:textColor="#ffffffff"
    android:textOff="@string/nao"
    android:textOn="@string/sim"/>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TableRow
                android:id="@+id/tableRow"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textViewPrimario"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/primario"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#55b76b"
                    android:textStyle="bold"
                    />

                <Spinner
                    android:id="@+id/spinnerPrimario"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp">

                <TextView
                    android:id="@+id/textViewSecundario"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/secundario"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#55b76b"
                    android:textStyle="bold"/>

                <Spinner
                    android:id="@+id/spinnerSecundario"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <TextView
                    android:id="@+id/textViewLeitura"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/leitura"
                    android:textColor="#ffffff"/>

                <EditText
                    android:id="@+id/editTextLeitura"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="2"
                    android:background="@drawable/fields_border"
                    android:ems="10"
                    android:inputType="numberDecimal"/>

            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <TextView
                    android:id="@+id/textViewPolaridade"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/polaridade"
                    android:textColor="#ffffff"
                    />

                <EditText
                    android:id="@+id/editTextPolaridade"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="2"
                    android:background="@drawable/fields_border"
                    android:ems="10"
                    android:inputType="numberSigned"/>

            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <TextView
                    android:id="@+id/textViewObs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/observacao"
                    android:textColor="#ffffff"
                    />

                <EditText
                    android:id="@+id/editTextObs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="2"
                    android:background="@drawable/fields_border"
                    android:ems="10"/>
            </TableRow>

        </TableLayout>

    </TableLayout>
</ScrollView>

  • Lucas, take a look at this OS question http://stackoverflow.com/questions/13703233/style-android-spinner. There he wanted to style the dropdown items, but he also styled the "arrow". Maybe that’s a good place to start

  • @Wakim I’ll take a look and then I’ll give back.

  • Another tip, from an inspected one (with ddms) on the width of the dropdown items. Maybe the width of the items is causing the problem and pushing the arrow.

  • Is it? I put mine TextView of custom spinner as match_parent, I’ll try to change later to wrap_content and see if there are any changes to the size of the selector.

  • @Wakim the link didn’t help me... :( As for the change in the width of the TextViews, nothing happened, I did the tests and the selector remains small.

  • I could post the layout of Activity or Fragment?

  • @Wakim, I will edit my question with the layout of Fragment. As for the color of the selector, I managed to solve it. My problem is still in the custom layout, got the cool color the selector, but it is still small. If I managed to solve everything here I put the solution. I am navigating between the styles android API to see the components and make the changes.

  • @Wakim before I edit the answer with the code from fragment, you think it really necessary to put the code here?

  • Code is not required, only the same layout

  • Sorry, I messed up the code. Posting in 1...2...3...

  • Lucas, try to exchange fill_parent for match_parent. Fill is deprecated... Soon I take a closer look at the layout.

  • Android Studio that puts this F#@R@#(*U(#@ de fill_parent automatic so I forget to switch to match_parent.

  • Okay, I got the briefcase res and sent a replace of fill_parent for match_parent. All the files they had fill_parent were changed and yet the almost invisible selector problem in the custom layout persists. I’m glad there’s replace :)

  • @Wakim found the problem. o/ But I still don’t understand why. I tried to put mine spinner fora da tablerowe advinha? Taram!!! Lá estava o seletor grande. Por algum motivo ele dentro dotablerow` stays that way.

  • @Wakim managed to solve the problem. I will post the solution.

Show 10 more comments

1 answer

0


Like I said in my question, my problem was:

I wanted my spinner selector to be bigger, it could be the standard size and wanted another color, to get on the bottom of the layout.

Let’s go to the first case, the selector that was almost invisible being bigger. His problem is that by being in a TableRow the selector was that size. I did a test with the Spinner outside the TableRow and the selector became large, the default size. To solve this problem without having to change the TableRow which contained the Spinner, i entered the attribute android:layout_weight="1" and put its width to wrap_content.

Let’s go to the second case, the dial was dark, I needed it to be lighter. I had to apply a style for my Spinners. The code of style contains comments for understanding how to make the selector clearer or change the selector if you wish.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <!-- Estilo padrão para o spinner dropdown. -->
        <item name="android:dropDownSpinnerStyle">@style/CustomSpinnerStyle</item>
    </style>

    <!-- Aqui eu defino estilo para todos os spinners do meu app, que será estendido pelo estilo que está na tag "parent" abaixo. -->
    <style name="CustomSpinnerStyle" parent="@android:style/Widget.Holo.Spinner">
        <!-- Aqui eu poderia definir um custom spinner selector por exemplo. Mas preferi utilizar o padrão mesmo. <item name="android:background">@drawable/custom_spinner_selector</item> -->
    </style>

    <!--Este estilo aplica ao spinner o tema dark, ou seja, como o tema é DARK, as coisas terão as cores para se sobre sair ao escuro do tema.
        <style name="Widget.Holo.Spinner" parent="Widget.Spinner.DropDown">
            <item name="android:background">@android:drawable/spinner_background_holo_dark</item>
            <item name="android:dropDownSelector">@android:drawable/list_selector_holo_dark</item>
            <item name="android:popupBackground">@android:drawable/menu_dropdown_panel_holo_dark</item>
            <item name="android:dropDownVerticalOffset">0dip</item>
            <item name="android:dropDownHorizontalOffset">0dip</item>
            <item name="android:dropDownWidth">wrap_content</item>
            <item name="android:popupPromptView">@android:layout/simple_dropdown_hint</item>
            <item name="android:gravity">start|center_vertical</item>
            <item name="android:disableChildrenWhenDisabled">true</item>
        </style>
    -->

    <!-- Se eu tivesse um tema claro e quisesse aplicar um tema escuro para o spinner, eu deveria estender deste estilo e não o acima.
        <style name="Widget.Holo.Light.Spinner" parent="Widget.Holo.Spinner">
        <item name="android:background">@android:drawable/spinner_background_holo_light</item>
        <item name="android:dropDownSelector">@android:drawable/list_selector_holo_light</item>
        <item name="android:popupBackground">@android:drawable/menu_dropdown_panel_holo_light</item>
        <item name="android:dropDownVerticalOffset">0dip</item>
        <item name="android:dropDownHorizontalOffset">0dip</item>
        <item name="android:dropDownWidth">wrap_content</item>
        <item name="android:popupPromptView">@android:layout/simple_dropdown_hint</item>
    </style>
    -->

</resources>
  • Good afternoon. In which of these attributes did I get a Lucas Santos custom dropDownSelector? A png q I created for example. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.