android


Android Spinner - How to position dropdown arrow as close to text as possible when options have different length?


The options in my spinner has different length and currently the dropdown arrow is positioned far to the right based on the longest option, as shown in the screenshot below.
Is it possible to move the dropdown arrow so that it is dynamically positioned based on currently selected option?
Especially when the first option is just 'All', it looks weird when the dropdown arrow is so far away to the right.
Referring to Google Translate App where dropdown arrow is always positioned next to its text:
Firstly set the background of the Spinner to #null to remove the default triangle:
<Spinner
android:id="#+id/spinner_main"
android:spinnerMode="dropdown"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
Then create a layout resource spinner_item_main.xml with only a TextView which we can set a drawable by its right side (you can download the arrow picture from here):
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="left"
android:textColor="#color/colorWhite"
android:drawableRight="#drawable/ic_arrow_drop_down_white_24dp"
/>
Finally Set this layout resource when you initialize the Spinner, You can also provide a resource as the dropdown view (as what I have done):
(I use kotlin)
spinner_main.adapter = ArrayAdapter<String>(this,
R.layout.spinner_item_main, objects).apply {
setDropDownViewResource(R.layout.spinner_dropdown_view_main)
}
Make it!
View this in My APP
1) Set the background of spinner to #null
<Spinner
android:id="#+id/spinner"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
2) In the spinner adapter layout, create an Imageview to the right of Textview or whatever content is in there
change the visibility of the imageview to "gone"
3)Override the onItemSelected method of the spinner in your activity,
in the onItemSelected method, call the Imageview
ImageView downArrow = (ImageView) view.findViewById(R.id.down_arrow);
change its visibility to "visible"

Related Links

Stuck while installing sample helloAndroid application in emulator
How to make words in checkbox text to be link
About connecting web database with Android application
can we make the whole application zoomable in android ?
Getting issue to set the color on list view item
Android - Is it possible to have dynamically moving clickable objects in a view
Make ball bounce and eventually come to rest
fbShare in android
How to play an encrypted video file in Android
Allocate longitude and latitude of an object
Don't receive nested XML nodes from SOAP Web Service using ksoap2
Authentication in KSOAP2 request to .NET webservice in Android
Autoclick search button (android)
Issues with custom layout for spinner for Android
Using Ant to build project and run JUnit Test Android app but Ant Junit Test fails
Uploading the whole sqlite database to google app engine

Categories

HOME
service-worker
asp.net-web-api
delphi-7
cheerio
developer-tools
c++11
codenvy
rdbms
object
3d
jwplayer
rendering
aws-cli
missing-data
jtextpane
pkcs#11
character
line
libc
event-sourcing
package.json
undertow
babel
inno-download-plugin
surveymonkey
outliers
entity-relationship-model
android-things
ceylon
quantlib
comparable
sqsh
constexpr
custom-controls
utorrent
erb
reactjs.net
playframework-2.1
metawidget
apache-stanbol
laravel-elixir
flask-restful
proget
twilio-click-to-call
edit
filehandle
quicktime
seq
flyout
gameplay-kit
rxtx
centrifuge
neon
omniauth-facebook
lua-telegram-bot
tracker-enabled-dbcontext
findfirst
halcon
false-sharing
java-money
django-urls
xvim
rpart
sony-lifelog-api
graphics2d
ms-access-2000
mraid
fail2ban
cryptographichashfunction
document-repository
beatsmusic
sql-scripts
rickshaw
device-tree
psr-4
cbind
mdichild
cuba
lnk
bin
unit-of-work
rolify
nebula
facebook-timeline
insert-id
joomla3.1
qi4j
correlated-subquery
usertype
webdev.webserver
socketserver
ajax-polling
pos-for-.net
nstokenfield
sifr
django-piston
silverlight-oob
dojo-dnd
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App