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

Is there a way to find out the startActivity intent flags an activity was started with?
internal app can not import com.android.internal.policy.impl?
How to make call backs between Fragments of the same Ativity
Sony SmartWatch - what is the host application (AHA)
how to Get data from SQLite database to intent through “onItemClick” on item in ListView Android
Android Listview using Google maps
this code create folder in SD card but not save image in that folder
Google inApp onPurchaseStatechange
Run specify app which installed in phone
Async download of images to gridview from MediaStore
Glassfish server throwing exception when sending JSONObject through android
loss of information when passing a parcelable object to an intent
How can select random data from database and display in textview?
How to create .so (Shared Object library file) from .c (C file) for Arm Processor?
Where to check for orientation change in an android fragment
How to customize Card.io scannner UI?

Categories

HOME
service-worker
notepad++
deep-learning
fuelux
c++11
github-for-windows
datastore
gradient
soa
web-hosting
visual-studio-emulator
babeljs
python-multithreading
text-mining
siddhi
altera
android-7.0-nougat
jersey-1.0
shortest-path
production
systemtime
froala
siri
maze
surveymonkey
opentk
stackexchange
jasmine-node
apktool
deltaspike
autolisp
hard-drive
activexobject
baidu-map
yowsup
video-capture
swagger-php
nohup
demo
numberpicker
xv6
tcc
signal-strength
vugen
corruption
carrot2
compiler-design
java-6
opengl-4
d3-force-directed
chromium-os
node-request
flex-monkey
latin
uicolor
nikeplus-api
avspeechsynthesizer
tracker-enabled-dbcontext
boost-serialization
kiosk
jnlua
opl
tfs-workitem
fiware-monitoring
text-search
evolus-pencil
mod-proxy
zscript
haskell-warp
typeconverter
openbabel
cocos3d
raygun
gii
xml-dsig
tinyxml
notorm
breakout
code-duplication
nevron
joomla-template
pagedown
mysql-error-2013
chart-director
wcf-configuration
rmiregistry
cuda-gdb
isa-swizzling
timthumb
django-paypal
collectionviewsource
non-clustered-index
openfaces
opml
nstokenfield
blender-2.49
qtabbar
skype4java
interop-domino
fault-tolerance
dsoframer

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