How to remove Spinner's ellipsize or '…' at the end?


How to remove Spinner's ellipsize or '…' at the end?



I made a few Spinners and the length(width) of Spinner is shorter than the name of some items.



For example, after choosing an item.



[South K...▼]


[South K...▼]



But what I want is this:



[South Kore▼]


[South Kore▼]



(But not [South Kore▼a] nor overlapped.)


[South Kore▼a]



It seems like Spinner in Layout(XML) doesn't have any attributes like ellipsize. And I couldn't find any information online.



Is there any way to solve this problem?



My resources are here:



1. Country Name Array in [strings.xml]


1. Country Name Array in [strings.xml]


<string-array name="countries_array">
<item>Argentina</item>
<item>Australia</item>
<item>Austria</item>
<item>Bangladesh</item>
<item>Belgium</item>
<item>Brazil</item>
<item>Bulgaria</item>
<item>Cambodia</item>
<item>Canada</item>
<item>Chile</item>
<item>China</item>
<item>Colombia</item>
<item>Czech Republic</item>
<item>Denmark</item>
<item>Egypt</item>
<item>Estonia</item>
<item>Fiji</item>
<item>Finland</item>
<item>France</item>
<item>Germany</item>
<item>Greece</item>
<item>Guam</item>
<item>Hong Kong</item>
<item>Hungary</item>
<item>Iceland</item>
<item>India</item>
<item>Indonesia</item>
<item>Iran</item>
<item>Iraq</item>
<item>Ireland</item>
<item>Israel</item>
<item>Italy</item>
<item>Jamaica</item>
<item>Japan</item>
<item>Kazakhstan</item>
<item>Laos</item>
<item>Luxembourg</item>
<item>Macau</item>
<item>Malaysia</item>
<item>Mexico</item>
<item>Monaco</item>
<item>Mongolia</item>
<item>Morocco</item>
<item>Mozambique</item>
<item>Myanmar</item>
<item>Nepal</item>
<item>Netherlands</item>
<item>New Zealand</item>
<item>Nigeria</item>
<item>North Korea</item>
<item>Norway</item>
<item>Pakistan</item>
<item>Peru</item>
<item>Philippines</item>
<item>Poland</item>
<item>Portugal</item>
<item>Romania</item>
<item>Russia</item>
<item>Senegal</item>
<item>Singapore</item>
<item>South Africa</item>
<item>South Korea</item>
<item>Spain</item>
<item>Sweden</item>
<item>Switzerland</item>
<item>Taiwan</item>
<item>Thailand</item>
<item>Tunisia</item>
<item>Turkey</item>
<item>Ukraine</item>
<item>United Arab Emirates</item>
<item>United Kingdom</item>
<item>United States</item>
<item>Uzbekistan</item>
<item>Vietnam</item>
</string-array>



2. Spinner in the Layout


2. Spinner in the Layout


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/themeColor"
tools:context="org.koreanlab.origol.activity.NavigationActivity">

<LinearLayout
android:id="@+id/searchbar_linearlayout_searchfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="horizontal"
android:weightSum="10"
android:background="@color/white"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center_vertical"
android:weightSum="10">

<Spinner
android:id="@+id/category_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
>
</Spinner>
<Spinner
android:id="@+id/subcategory_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
>
</Spinner>
<EditText
android:id="@+id/iamlookingfor_edittext_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="@string/iamlookingfor"
android:inputType="text"
android:maxLength="30"
android:maxLines="1" />

<Spinner
android:id="@+id/country_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:spinnerMode="dropdown"
android:prompt="@string/country_spinner_title"
>

</Spinner>

<Spinner
android:id="@+id/city_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:spinnerMode="dropdown"
android:prompt="@string/city_spinner_title">

</Spinner>
</LinearLayout>
</LinearLayout>

<ListView
android:id="@+id/article_listview_searchfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/searchbar_linearlayout_searchfragment"
android:scrollbarStyle="outsideOverlay" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/writebutton_searchfragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:tint="@color/white"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
app:backgroundTint="@color/blueMiddle"
app:srcCompat="@drawable/baseline_brush_black_48" />
</RelativeLayout>



3. Java


3. Java


ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.categories_array));
ArrayAdapter<String> countryAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.countries_array));

categorySP.setOnItemSelectedListener(this);
countrySP.setOnItemSelectedListener(this);

categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

categorySP.setAdapter(categoryAdapter);
countrySP.setAdapter(countryAdapter);





Can you share the spinner adapter and xml?
– Udit
Jun 30 at 5:56





updated the resources. @Udit
– Chanjung Kim
Jun 30 at 6:10




3 Answers
3



Provide custom layout for Spinner:


ArrayAdapter adapter= new ArrayAdapter(mOwnerActivity, R.layout.simple_list_item_1, new ArrayList<>());



Layout: R.layout.simple_list_item_1 -->


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />



set adapter to your spinner:


spinner.setAdapter(adapter);



Hope it will help you.





What about style and margin?
– Chanjung Kim
Jun 30 at 6:09


style


margin





just remove it apply your own style
– Shiv Jalkote
Jun 30 at 6:10





Could you please give me additional information about ? It seems it has margin-left and margin-right. How can I remove them? It looks like [South Kor ▼ ] now. I want like [South Korea▼] without any spaces.
– Chanjung Kim
Jun 30 at 6:44







just remove the padding of textview and adjust textview according to your need.
– Shiv Jalkote
Jun 30 at 6:49





That's the result of giving 0dp to its padding. I guess the margin belongs to and even though I manipulate the TextView, It's not affected.
– Chanjung Kim
Jun 30 at 7:00



0dp




You to have no ellipse



use:
android:ellipsize = "none"


android:ellipsize = "none"





It doesn't have ellipsize attribute.
– Chanjung Kim
Jun 30 at 6:02


ellipsize





I guess @Shomu meant custom spinner layout. custom spinner layout is made of TextView. So, it has ellipsize attribute and I can customize it.
– Chanjung Kim
Jun 30 at 6:47


custom spinner layout


custom spinner layout


TextView


ellipsize



When you create Adapter, you need to input three parameters.



Context, Layout, ArrayList


Context


Layout


ArrayList



So, If you change the Layout(Customize). You can solve this problem.


Layout



This should change:


ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.categories_array));



To:


ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(getContext(), R.layout.custom_spinner_item, getResources().getStringArray(R.array.categories_array));



NOTE: It is not android.R.layout.xxx. but R.layout.xxx WITHOUT android.



And create layout like this(under layout):


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spinner_margin"
android:textColor="@color/black"
android:maxLines="1"
/>



You can change the @dimen and @color as your taste.


@dimen


@color



What I did is:



colors.xml


colors.xml


<color name="black">#000</color>



dimen.xml


dimen.xml


<dimen name="spinner_margin">0dp</dimen>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV