4

I have a small question.

My string.xml

<resources>
    <!-- Months -->
    <string name="january">January</string>
    <string name="february">February</string>
    <string name="march">March</string>
    <string name="april">April</string>
    <string name="may">May</string>
    <string name="june">June</string>
    <string name="july">July</string>
    <string name="august">August</string>
    <string name="september">September</string>
    <string name="october">October</string>
    <string name="november">November</string>
    <string name="december">December</string>
</resources>

And my months.xml

<resources>
    <string-array name="months">
        <item>@string/january</item>
        <item>@string/february</item>
        <item>@string/march</item>
        <item>@string/april</item>
        <item>@string/may</item>
        <item>@string/june</item>
        <item>@string/july</item>
        <item>@string/august</item>
        <item>@string/september</item>
        <item>@string/october</item>
        <item>@string/november</item>
        <item>@string/december</item>
    </string-array>
</resources>

I set the months.xml for my spinner.

<Spinner
    android:id="@+id/sn_months"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:textAlignment="center"
    android:entries="@array/months"/>

Now, i want to get the string's names of the items like january, february,...NOT January, February,... in the spinner's onItemSelected using the item's position. How can i do that?

3
  • stackoverflow.com/questions/7256514/… Commented Oct 13, 2016 at 13:18
  • If I understand correctly, you want the name of resource used such as january from @string/january. Not possible. Sorry. Commented Oct 13, 2016 at 13:24
  • Thanks @EugenPechanec. I will find another way. Commented Oct 13, 2016 at 13:27

3 Answers 3

0
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
   String yearnam=parent.getItemAtPosition(pos).toString()toLowerCase();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of relying on string value for comparison, you should use spinner.getSelectedItemPosition() as it will return the selected item's index value.

Comments

0

1) Change your input like you want.

<resources>
<!-- Months -->
<string name="january">january</string>
<string name="february">february</string>
<string name="march">march</string>
<string name="april">april</string>
<string name="may">may</string>
<string name="june">june</string>
<string name="july">july</string>
<string name="august">august</string>
<string name="september">september</string>
<string name="october">october</string>
<string name="november">november</string>
<string name="december">december</string>

Or

2) Create a custom View by extending Spinner change the attributre loading behaviour

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.