I am implementing a custom view and want to define an array of strings in attrs.xml. Currently I am doing it as follows:
In attrs.xml file I have defined:
<attr name="twCells" format="string"/>
In activity_main.xml I am providing following data:
app:twCells="A1,A7,G1,G7"
And finally in MyCustomView.java file I am reading twCell attribute as following:
private List<String> twCells = null;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BoardView, 0, 0);
twCells = Arrays.asList(a.getString(R.styleable.BoardView_twCells).split(","));
My question: Is there any better way available to do this?
If you agree to move your strings resources into the strings.xml file, you can do like this:
in strings.xml
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
and to get a string:
String planet = getResources().getStringArray(R.array.planets_array)[i];
Related
I have array in file res/values/ids.xml
<array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
...
</array>
I tried to get these ids, but it didn't work(((
val typedArray = resources.obtainTypedArray(viewSetId)
val idSet = IntArray(typedArray.length())
for (i in 0 until typedArray.length()) {
idSet[i] = typedArray.getInt(i, 0)
}
typedArray.recycle()
All idSet's elements equals 0.
I also tried store integer array in res/values/integers.xml
<integer-array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
...
</integer-array>
And then get these ids
val idSet = resources.getIntArray(R.array.first_set)
But result is the same((
IntArray Xml should be like this Format
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
</integer-array>
</resources>
in the path xml resource in res/values/integers.xml
Access the xml data in the activity like this way
val arrayValues = resources.getIntArray(R.array.first_set)
Use #+id to ensure they are created by the time the array is generated:
<integer-array name="first_set">
<item>#+id/layout1</item>
<item>#+id/layout2</item>
<item>#+id/layout3</item>
...
</integer-array>
I have declared following declare-stylables in attr.xml:
<declare-styleable name="SideSpinnerAttrs">
<attr name="stringValues" format="reference" />
<attr name="iconIDs" format="reference"/>
</declare-styleable>
Array of resource icons in array.xml:
<integer-array name="spinnerIcons">
<item>#drawable/ic_attachment_black_24dp</item>
<item>#drawable/ic_audiotrack_black_24dp</item>
<item>#drawable/ic_slideshow_black_24dp</item>
</integer-array>
I would like to call and set those icons from array to an imageView:
private void readSpinnerIcons(Context context, AttributeSet attrs) {
TypedArray icons=context.obtainStyledAttributes(attrs,R.styleable.SideSpinnerAttrs);
int id=icons.getResourceId(R.styleable.SideSpinnerAttrs_iconIDs,0);
int[] i=getResources().getIntArray(id);
spinner_icon.setBackgroundResource(i[0]);
}
But, array "int[] i", is empty. Why?
For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="IntArray">
<item>2</item>
<item>8</item>
<item>10</item>
<item>16</item>
</integer-array>
</resources>
You can use this
Resources r = getResources();
int[] bases = r.getIntArray(R.array.IntArray);
The problem is this line:
int id=icons.getResourceId(R.styleable.SideSpinnerAttrs_iconIDs,0);
The first argument is not the Styleable resource id, but rather the index of the TypedArray containing the resource id. Since you're not providing a valid index, id will always be the default value you're using as the 2nd argument, which means your int[] array i will always be empty.
Also, make sure you always call recycle() once you're done using a TypedArray. Use the following:
private void readSpinnerIcons(Context context) {
TypedArray icons = context.obtainStyledAttributes(new int[] {R.styleable.SideSpinnerAttrs});
int id = icons.getResourceId(0, 0);
int[] i = getResources().getIntArray(id);
spinner_icon.setBackgroundResource(i[0]);
icons.recycle();
}
So I have many strings in strings.xml, they are recorded in a format of:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
Is there a method to do so?
Try this:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);
It would be worth to use resource arrays. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
and you can access, iterate as follows:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
I have this file array.xml and I want to get the item value from an array.
How can I do that? I have tried with getInt but that returns 0. All help is welcome.
<resources>
<array name="firstAd">
<item>border_top_id_1v</item>
<item>R.id.dugme_1v</item>
<item>R.id.rent_or_buy_1v</item>
<item>R.id.currency_1v</item>
<item>R.id.price_1v</item>
<item>R.id.name_1v</item>
<item>R.id.address_1v</item>
</array>
</resources>
First, change each item to #id/object instead of R.id.object, then change the tag from array to integer-array, and move the code to your 'integer.xml' resource file.
integer.xml:
<resources>
<integer-array name="firstAd">
<item>#id/dugme_1v</item>
<item>#id/rent_or_buy_1v</item>
<item>#id/currency_1v</item>
<item>#id/price_1v</item>
<item>#id/name_1v</item>
<item>#id/address_1v</item>
</integer-array>
</resources>
Then, programmatically use a TypedArray, like so:
TypedArray firstAd = getResources().obtainTypedArray(R.array.firstAd);
int resourceId = firstAd.getResourceId(index, defValue);
try #id/yourIDHere
<item>border_top_id_1v</item>
<item>#id/dugme/1v</item>
<item>#id/rent_or_buy_1v</item>
<item>#id/currency_1v</item>
I have a string-array in strings.xml. How can I underline the string in item?
For string we do like this..
<string name="clickable_string">This is a <u>clickable string</u></string>
I tried something like this.
<string-array name="products">
<item>this is a <u> clickable</u> string </item>
<item> <u> clickable</u> string </item>
<item>this is a <u> clickable</u> string </item>
</string-array>
Its not working. How can I do that?
Thank you
Try this way it will work
<string-array name="description">
<item> <Data> <![CDATA[ Check this <u>Redirect to Next Activity</u> ]]></Data> </item>
</string-array>
In java Code use this :
ArrayList<String> title_list = new ArrayList<String>();
String[] description_Array = getResources().getStringArray(R.array.description);
String categoryAndDesc = null;
for(String cad : description_Array) {
categoryAndDesc = cad;
title_list.add(categoryAndDesc);
}
CharSequence sequence = Html.fromHtml(categoryAndDesc);
seperator_view.setText(strBuilder);
seperator_view.setMovementMethod(LinkMovementMethod.getInstance());
Try this:
<string name="clickable_string">This is a <u>clickable string</u></string>
(i.e) Replace the < and > with <; and >; in your strings.xml for HTML formatted content.
And in your code use:
String text = context.getString(R.string.clickable_string);
myTextView.setText(Html.fromHtml(text));