i want to get array from myapp.R.array.*.
means if i have this array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name ="processName">
<item >solve</item>
<item >simplify</item>
<item >plot</item>
<item ></item>
</string-array>
</resources>
then i must have in variable : ["solve","simplify","plot","",]
Taking from #Raghunandan's comment, you can use the getStringArray() method.
http://developer.android.com/guide/topics/resources/string-resource.html
String[] processNames = getResources().getStringArray(R.array.processName);
You can then traverse the array as normal, or put it into an adapter if you are trying to display the options in a spinner or list view.
Be sure to note that you need a context in order to use this method. So, if you are not inside of an Activity, you'll need to use a passed reference to a context.
String[] processNames = context.getResources().getStringArray(R.array.processName);
Related
Hi i want to create a viewpager so i follow this tutorial : https://blog.mindorks.com/android-viewpager-in-kotlin. But when i try to set up the array of color in res.colors :
<array name=”bg_color”>
<item name=”bg_screen1">#f64c73</item>
<item name=”bg_screen2">#20d2bb</item>
<item name=”bg_screen3">#3395ff</item>
<item name=”bg_screen4">#c873f4</item>
</array>
This doesn't compile, i got Attribute value expected on = of the first line. And i got type attribute should be define on all other lines. I assume that i should define a type of the array, but which type should i use for colors? and how to declare it ?
EDIT
To use it you have to declare and IntArray then select the good position :
val bg_color = resources.getIntArray(R.array.bg_color)
webView.setBackgroundColor(bg_color[pos])
Exemple for a web view where pos is an int
I think you copy paste code from that tutorial and because of that issue happens for " " .
Please add below code .
<array name="bg_color">
<item name="bg_screen1">#f64c73</item>
<item name="bg_screen2">#20d2bb</item>
<item name="bg_screen3">#3395ff</item>
<item name="bg_screen4">#c873f4</item>
</array>
Don't give a name to each row, it's an array, not a map:
<string-array name=”bg_color”>
<item>#f64c73</item>
<item>#20d2bb</item>
<item>#3395ff</item>
<item>#c873f4</item>
</string-array>
I know it is possible to create custom UI element (by way of View or specific UI element extension). But is it possible to define new properties or attributes to newly created UI elements (I mean not inherited, but brand new to define some specific behavior I am not able to handle with default propertis or attributes)
e.g. element my custom element:
<com.tryout.myCustomElement
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Element..."
android:myCustomValue=<someValue>
/>
So is it possible to define MyCustomValue?
Thx
Yes. Short guide:
Create an attribute XML
Create a new XML file inside /res/values/attrs.xml, with the attribute and its type
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="MyCustomElement">
<attr name="distanceExample" format="dimension"/>
</declare-styleable>
</resources>
Basically you have to set up one <declare-styleable /> for your view that contains all your custom attributes (here just one). I never found a full list of possible types, so you need to look at the source for one I guess. Types that I know are reference (to another resource), color, boolean, dimension, float, integer and string. They are pretty self-explanatory
Use the attributes in your layout
That works the same way you did above, with one exception. Your custom attribute needs its own XML namespace.
<com.example.yourpackage.MyCustomElement
xmlns:customNS="http://schemas.android.com/apk/res/com.example.yourpackage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Element..."
customNS:distanceExample="12dp"
/>
Pretty straight forward.
Make use of the values you get passed
Modify the constructor of your custom view to parse the values.
public MyCustomElement(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomElement, 0, 0);
try {
distanceExample = ta.getDimension(R.styleable.MyCustomElement_distanceExample, 100.0f);
} finally {
ta.recycle();
}
// ...
}
distanceExample is a private member variable in this example. TypedArray has lots of other things to parse other types of values.
And that's it. Use the parsed value in your View to modify it, e.g. use it in onDraw() to change the look accordingly.
In your res/values folder create attr.xml. There you can define your attribues:
<declare-styleable name="">
<attr name="myCustomValue" format="integer/boolean/whatever" />
</declare-styleable>
When you then want to use it in your layout file you have to add
xmlns:customname="http://schemas.android.com/apk/res/your.package.name"
and then you can use the value with customname:myCustomValue=""
Yes , you can.Just use <resource> tag.
like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
link from official website
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"/>
This is my menu_items.xml whose elements are shown in a listview. They are Black coloured by default. I want them to be white. How can in change the color of these string-array items?
Below is my menu_items.xml code.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="menu_items" >
<item >Demo Mode</item>
<item >Inbox</item>
<item >Sent Items</item>
<item >Filing History</item>
</string-array>
</resources>
Have your string items as :
<string-array name="menu_items" >
<item >Demo Mode</item>
<item >Inbox</item>
<item >Sent Items</item>
<item >Filing History</item>
</string-array>
<string-array name="menu_items_labels" >
<item ><FONT COLOR="#006600"><b>Demo Mode</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Inbox</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Sent Items</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Filing History</b></FONT></item>
</string-array>
In your spinner code - use:
// For setting the html code "menu_items_labels" with font color white
Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setAdapter(
new ArrayAdapter<CharSequence>(this,
R.layout.multiline_spinner_dropdown_item,
myActivity.this.getResources().getTextArray(R.array.menu_items_labels)));
To retrieve the String value (Without HTML Formatting),
strTemp = getResources().getStringArray(R.array.menu_items)[spinner.getSelectedItemPosition()];
You can not directly tell the (array) resource which color it should have. You either can define an app-wide theme/style to use for list items or create an Adapter with custom view, in your case an ArrayAdapter will fit best.
Hot to create an ArrayAdapter with a custom view.
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.