Use string array to populate Spinner - android

I'm in the rookie leagues when it comes to Android apps and am looking to populate a Spinner with an Array or strings (it's a converter app) below is an extract from my XML file and I'm looking to populate the the Spinner:
......
<string name="TemperatureString">Temperature</string>
<string name="WeightString">Weight</string>
<string name="VolumeString">Volume</string>
<string name="SpeedString">Speed</string>
<string name="LengthString">Length</string>
<string name="AreaString">Area</string>
<string name="EnergyString">Energy</string>
<string name="PresureString">Presure</string>
<string name="MemoryString">Memory</string>
<string-array name="Convert_Type">
<item>#string/TemperatureString</item>
<item>#string/WeightString</item>
<item>#string/VolumeString</item>
<item>#string/SpeedString</item>
<item>#string/LengthString</item>
<item>#string/AreaString</item>
<item>#string/EnergyString</item>
<item>#string/PresureString</item>
<item>#string/MemoryString</item>
</string-array>
From this, I'm trying to populate my spinner (#+id/MainSpinner) - I'm not sure what I'm doing here but for the activity_main.xml I have the following:
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>
I know there's a way of doing this via Java but I'm even worse at Java!
For this reason, I'd like to keep this within xml if possible.
Also, If someone can want's to point me towards links to bring on my Java and xml skills that would be great - I've started with Udacity and have found them good but there's a lot to take in for a non-IT graduate (I work in finance but find this kinda thing really interesting!)
Thanks in advance!

Simplest way to bind ListView and Spinner control with String Array is
android:entries = "#array/nameofarray"
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"
android:entries="#array/Convert_Type"/>
If you want to change the theme of each item of Spinner then put below style into res/values/styles.xml
<style name="ItemTextAppearance">
<item name="android:textColor">#f00</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">monospace</item>
</style>
and the set
android:theme="#style/ItemTextAppearance"
of spinner.

Use entries attribute in the spinner tag
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/MainSpinner"
android:entries="#array/Convert_Type"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>

Related

disable spinner item in XML

I Have a simple spinner and would disable an element.
Could I disable elements from the xml files?
Or i need to code something in JAVA?
<Spinner
android:id="#+id/stato"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:entries="#array/feedbacktypelist"
android:layout_below="#+id/textview">
</Spinner>
arrays.xml
<resources>
<string-array name="feedbacktypelist">
<item>#string/stato1</item>
<item>#string/stato2</item>
<item>#string/stato3</item>
<item>#string/stato4</item>
<item>#string/stato5</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="stato1">ITALIA</string>
<string name="stato2">SPAGNA</string>
<string name="stato3">GERMANIA</string>
<string name="stato4">REPUBBLICA CECA</string>
<string name="stato5">INGHILTERRA</string>
</resources>
You can do two things, comment out the line in the resource array via highlighting the row and doing ctrl+/
or
You can remove the android:entries="#array/feedbacktypelist" line and add the list yourself but skipping the entry you don't want (most common me thinks) see how here How to create Spinner-list using CustomAdapter in android

Adding an image to a string with Android SDK

I am a super newbie to Android Development and wanted to start slow with an Icon Theme for 3rd party launchers. I was wondering if there is any way to add an image (from inside my "drawable" folder) to a string inside "strings.xml"
This is what I have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme properties -->
<bool name="enableIconPack">true</bool>
<bool name="enableDockPack">false</bool>
<string name="authorName">Clay Cauley</string>
<string name="developerName">Clay Cauley</string>
<string name="authorLink">MY SITE</string>
<string name="theme_title">MY TITLE</string>
<string name="theme_description">This is some text explaining who created the theme and why, etc ... This is also where I would like my logo to appear.</string>
</resources>
My problem lies in the last string, "theme_description" --- If it's possible I wanted to get my logo in there so people see it when on this screen:
Hopefully wanting to get it where the "Hey" currently is.
I've tried 2-3 different approaches but they were all guesses and none worked so I thought I would try here. Any help is greatly appreciated, even if it's telling me it isn't possible :)
This is my "main.xml"
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="65dp"
android:gravity="center"
android:horizontalSpacing="1dp"
android:listSelector="#000000"
android:background="#000000"
android:numColumns="3"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="-1dp" >
</GridView>
Thanks!
What you probably want to do is to add an image to a TextView. There are attributes for that: "android:drawableLeft", "android:drawableRight", "android:drawableStart", etc.
<TextView android:drawableLeft="#drawable/my_drawable" .../>
If you want to define it in a resource file, then do it in styles.xml:
<style name="my_text_view_with_drawable">
<item name="android:drawableLeft">#drawable/my_drawable</item>
</style>
<TextView style="#style/my_text_view_with_drawable" .../>
I think the original poster https://stackoverflow.com/users/1344453/clay-cauley is asking how would you specify an image to be inserted along with the text from a string in string.xml
Since you can specify links and email inside a string, it would be nice to reference a drawable.
Asked differently how would you do something a la html img tag, something like this :)
<img src="#drawable/icon" >
I too am interested in this possibility.
I know how to dedicate the layout with an ImageView and load a bitmap of choice via code.
I think the problem is trying to use TextView (it probably is not designed to display images, how about if the layout has a WebView control? Could a string resource reference a bitmap in the drawables, in local storage?
Thank you.
You do this with a kind of layout, like linearlayout or relativelayout.

Android Spinner Option Value Just like Select Tag

Is it Possible I can create a spinner with options and values.
<select name=test>
<option value="1">Baran</option>
<option value="2">Khan</option>
</select>
with the spinner XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
</resources>
How can I accomplish such target. As I need to pass Ids to the Server.
You have to manage two list and both are dynamic as you want.
Step to achieve :
Create two ArrayList<String>.Depend on your data type here i make as String Array.
Add value to ArrayList.
Create custom adapter and pass two list adapter in that and get value according that.
Add list to Spinner Adapter. Get the index or position of
the Spinner.
Follow same index to get value from second list value.
Send that value to server.
Task over
See Demo Example that will Guide you to make easy.
Enjoy !!!
Not the best but one approach would be to create another string array with the ids:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
<string-array name="testIDS">
<item>1</item>
<item>2</item>
</string-array>
</resources>
Now when item i is selected from array test you can get the id from item i in array testIDS.

How to set style for spinner programmatically?

I have spinner with a style selection (I set the spinner style in strings.xml) and it's works well if I set the style in main.xml. But I want to know how to set the style programmatically in which the style already defined in strings.xml.
Refer my code below
main.xml:
<Spinner
android:id="#+id/PassengersSpinner1"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_span="3"
android:layout_marginTop="6dp"
style="#style/SpinnerStyle" />
strings.xml:
<style
name="SpinnerStyle"
parent="#android:style/Widget.Spinner">
<item name="android:background">#android:drawable/btn_default</item>
</style>
Now, I want to know that how can I set this strings.xml programmatically without main.xml?
From all my reading, you cannot do it, because the style has to be set before the view is created.
You can do it if you create the view in code (since then you can set the style before creating the view).
If you want to do that, a good example is here. Both the question and the answer give you methods to achieve your goal.

Android element ID naming

So I have a string in my strings.xml file declared like so:
<string name="welcome">Please hit the menu to begin</string>
And I have a TextView in my main.xml that uses it like so:
<TextView
android:id="#string/welcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome"
/>
Now, is that the proper way to give a TextView an ID? It seems strange to use a string resource as an ID like that.
Now, is that the proper way to give a TextView an ID?
No. Use android:id="#+id/whatever".
To add an id directly to a textview you must append a + sign
android:id="#+id/welcome"
alternatively you can have an id set up in a resource file
<resources>
<item name="welcome" type="id"/>
</resources>
android:id="#id/welcome"
http://developer.android.com/guide/topics/ui/declaring-layout.html
is the manual page related to this topic

Categories

Resources