3

Hi im having a issue with text size not displaying correctly on my activity, the drop down it self is large(bottom picture) which is how i want it however when the drop down is not open it has small text(top picture). here is the xml layout the spinner pulls from

simple_spinner_dropdown_item.xml <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50sp" android:gravity="left" android:textColor="#000000" android:padding="5dip" android:textStyle="bold"/> 

and here is the xml of the menu in which the spinner is displayed on

main_menu.xml <Spinner android:id="@+id/spinnerMainMenu" android:layout_width="784dp" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.072" tools:layout_editor_absoluteX="8dp" /> 

and finally the code for how it is displayed

MainMenu.java final Spinner mainMenuDrpDwn = findViewById(R.id.spinnerMainMenu); String[] machines = new String[]{"215 DT", "420 DT", "215 CANNAGIN"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, machines); adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item); mainMenuDrpDwn.setAdapter(adapter); 

enter image description here

enter image description here

3 Answers 3

2

For this let us understand the Adapter concept in a bit detail, the adapter which binds the spinner's layout files and the data to the spinner takes in two layout files. One is the layout resource which you passed in during the initialization of the ArrayAdapter i.e.,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, machines); 

This is the layout resource for the spinner when it is in not expanded state i.e. spinner items are shown. The other is the dropdown resource which is the layout of the drop down items of the spinners i.e. how each item in the spinner will look when it is expanded, it is set as follows:

adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item); 

Now the problem is that you are passing in a custom dropdown resource so you are getting the dropdown to look as desired but you are passing in the standard layout file for the view i.e. android.R.layout.simple_spinner_dropdown_item. So to resolve this if you want you can pass in the same dropdown resource for the normal view too (if you wish to do so) else you can create another layout with the desired properties and pass that it as done in the following code:

custom_drop_down_spinner_item

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" style="?android:attr/spinnerDropDownItemStyle" android:singleLine="false" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:ellipsize="marquee" android:textSize="25sp" android:background="@android:color/white" android:textColor="@color/colorAccent" android:fontFamily="sans-serif-smallcaps"/> 

custom_spinner_item

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text2" style="?android:attr/spinnerItemStyle" android:background="@android:color/holo_blue_light" android:singleLine="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textAlignment="inherit" android:gravity="center" android:textSize="50sp" android:textColor="@color/colorPrimary"/> 

setting it to the spinner:

ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.educational_qualification_orig_array, R.layout.custom_spinner_item); arrayAdapter.setDropDownViewResource(R.layout.custom_drop_down_spinner_item); spinner.setAdapter(arrayAdapter); 

The resulting spinner item in unexpanded state would look like

Custom spinner item

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, for me since iam using a sting as the text that gets displayed i had to edit your adapter code to fit. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, machines)
1

Use the same layout file in your adapter.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_dropdown_item, machines); 

It will only work if your TextView id is text1 on your simple_spinner_dropdown_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50sp" android:gravity="left" android:textColor="#000000" android:padding="5dip" android:textStyle="bold"/> 

Comments

0

Expanding on dr3k's answer, I found this in the source:

http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/res/res/values/attrs.xml

4742 <declare-styleable name="Spinner"> ... 4763 <!-- Reference to a layout to use for displaying a prompt in the dropdown for 4764 spinnerMode="dropdown". This layout must contain a TextView with the id 4765 {@code @android:id/text1} to be populated with the prompt text. --> 4766 <attr name="popupPromptView" format="reference" /> ... 4772 </declare-styleable> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.