Search code examples
androidandroid-layoutandroid-spinner

How to show just 3 characters in an Android Spinner?


I'm working on an android project and I made an Spinner for week days, now I want to show just the abbreviation of any day selected in the Spinner (mon, tue, wed, etc.), but full name of days in the dropdown (Monday, Tuesday, Wednesday, etc.). How should I do it? I already made the simplest Spinner with default xml layouts.


Solution

  • Note: This may not compile it's just a "pseudo" to give you an idea for an approach.

    You create a normal Spinner (you could also create a CustomView and then put the logic there, but I'll go for the fastest one)

    <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    

    You can create a string-array with the days as

    <string-array name="Weekdays"> 
            <item>Monday</item> 
            <item>Tuesday</item> 
            <item>Wednesday</item> 
            <item>Thursday </item> 
            <item>Friday</item> 
            <item>Saturday</item> 
            <item>Sunday</item> 
    </string-array> 
    
    <string-array name="WeekdaysAbbreviations">
            <item>Mon</item>
            <item>Tue</item>
            <item>Wed</item>
            <item>Thu</item>
            <item>Fri</item>
            <item>Sat</item>
            <item>Sun</item>
        </string-array>
    

    Then you get the weekdays as follows

    val weekdays = resources.getStringArray(R.array.Weekdays)
    

    Then a test would be :

    
            val weekDays = resources.getStringArray(R.array.Weekdays)
            val abbreviationWeekDays = resources.getStringArray(R.array.WeekdaysAbbreviations)
            //find the spinner
            val spinner = findViewById<AppCompatSpinner>(R.id.spinner)
            //create an adapter with the weekdays
            val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, weekDays)
            //set the adapter
            spinner.adapter = adapter
            spinner.onItemSelectedListener = object :
                AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>,
                    view: View, position: Int, id: Long
                ) {
                    //find the first TextView of the Adapter
                    val selectedText = parent.getChildAt(FIRST_POSITION) as TextView
                    //As the abbreviationWeekDays and WeekDays have the same size, you can get the position selected on weebDays and then print what you have on abbreviationdays
                    //Another option would be selectedText.text = weekDays[position].substring(0,4)
                    selectedText.text = abbreviationWeekDays[position]
                }
    
                override fun onNothingSelected(parent: AdapterView<*>) {
                    //Nothing selected \o/
                }
            }
    

    Output :

    enter image description here enter image description here

    Also I've created a Sample repository on my Github account