How to use two strings in a layout? - android

I try to make a simple app and I used two strings in strings.xml:
<string name="question">is the capital of:</string>
<string name="riga">Riga</string>
And I want to add them to one text view.
I want the output to be (Riga is the capital of: ).
How can I do this?
This is my layout:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/riga" + "#string/question"
/>

You cannot do this in XML. You could do it in Java, however. Assuming you're inside an Activity:
String riga = getString(R.string.riga);
String question = getString(R.string.question);
TextView tv = findViewById(/* your id here */);
tv.setText(riga + question);
Even better would be to use format arguments in one of your strings:
<string name="question">%1$s is the capital of:</string>
And then do this in your activity:
String riga = getString(R.string.riga);
String fullQuestion = getString(R.string.question, riga);
TextView tv = findViewById(/* your id here */);
tv.setText(fullQuestion);

Related

Can't get rupee symbol to show correctly from strings.xml

So this works fine:
strFoo = "\u20B9" + strBar
But this doesn't
strFoo = R.string.rupee_symbol.toString() + strBar //.toString() is required
//R.string.rupee_symbol.toString() evaluates to some random number 2131755148... which I believe is a character array...
strings.xml
<string name="rupee_symbol">\u20B9 </string>
I can't figure out why it would behave like that, it looks like the same thing...!
You should not concatenate strings with string resources instead, you can use place holder:
<string name="rupee_symbol">\u20B9%s</string>
And use:
strFoo = resources.getString(R.string.rupee_symbol, strBar)
use getString(R.string.rupee_symbol) instead R.string.rupee_symbol.toString()
For example-
String strBar = String.valueOf(100);
String strFoo = getString(R.string.rupee_symbol)+strBar;
textView.setText( strFoo);

can you add some static string to a resource string in a view in xml

I have this in strings.xml:
<string name="function_varname">F</string>
and I want to have a TextView with the Text "F = ". is that possible? (only using xml and not just making a 2nd string)
Something like that:
android:text="#string/function_varname"+" = "

Android get string name from resource file values.xml

I would like to get name from values resource file.
for example
values.xml
<string name="ind_ginger">Ginger</string>
<string name="ind_garlic">Garlic</string>
I am using them for the check boxes like
<CheckBox
android:id="#+id/c01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="#string/ind_garlic"
android:layout_alignTop="#+id/c02"
android:layout_alignRight="#+id/saveChanges"
android:layout_alignEnd="#+id/saveChanges" />
<CheckBox
android:id="#+id/c02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="81dp"
android:layout_marginStart="81dp"
android:checked="false"
android:text="#string/ind_ginger"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
In application I need access String Name ( Please note value)
for (CheckBox item : checkBoxList){
if(item.isChecked())
{
//String text=item.getText().toString();String viewID = getResources().getResourceName(item.getId()); // gets me the name
String name = getResources().getResourceEntryName(item.getId());
String tName =
//item.getText().toString();
// String id = item.getTag().toString();
Toast.makeText(getApplicationContext(), tName,Toast.LENGTH_SHORT).show();
Log.d(viewID, TAG);
}
}
Is parsing the XML only way?
Try
String ginger = getResources().getString(R.string.ind_ginger)
Easiest way to get the "KEY" name is as following:
Log.e("KEY_NAME", getResources().getResourceEntryName(R.string.app_name));
Here you will get "app_name" as result.
This can be also help in support multi-language support feature.
You can use this to fetch all the String Keys in string.xml
Field[] fields = R.string.class.getFields();
String[] allStringNames = new String[fields.length];
for (int i =0; i < fields.length; i++) {
allStringNames[i] = fields[i].getName();
Log.e("String Key Name",""+allStringNames[i]);
}
Hope this will help
Just Store you string items in (And its better if you store your string file in in strings.xml and not value.xml )
strings.xml
<string name="ind_ginger">Ginger</string>
<string name="ind_garlic">Garlic</string>
Firstly if you need ind_ginger you need to change your code from this
<string name="ind_ginger">Ginger</string>
to
<string name="ind_ginger">ind_ginger</string>
and
String ginger = getResources().getString(R.string.ind_ginger)
to get the ind_ginger.
But what I can see from your code is you are using .getResourceEntryName(item.getId()) for that you need to create a String arraylist which would have all the itemId from strings.xml and then you can use them with item.get(position).
Here position is the position of the item in your array list.

Parameter in strings.xml does't work

I don't understand this anymore.
I try to write a TextView
android:text="#string/dbVer"
define in strings.xml
<string name="dbVer">db %1$s</string>
and in Activity
int dbTag = Integer.parseInt(yearDay.format(new Date(new File(databasePath + "/ean_database.db").lastModified())));
String dbVer = String.format(getString(R.string.dbVer), dbTag );
The TextView is still showing: db %1$s
The nearest answer I found: Are parameters in strings.xml possible? is similar but in fact something is wrong for me.
It looks like you are getting the result "db %1$s" because you are creating a string and assigning that as its value in the strings.xml file between these ><. What are you trying to have it show instead?
android:text="#string/dbVer"
This refers to your format string and displays the raw format string you're seeing.
int dbTag = Integer.parseInt(yearDay.format(new Date(new File(databasePath + "/ean_database.db").lastModified())));
String dbVer = String.format(getString(R.string.dbVer), dbTag );
This creates a new string dbVer using the format string from resources.
What is missing is that you need to set this new string as your TextView's text:
TextView tv = (TextView)findViewById(R.id.your_textview_id); // assuming an activity
tv.setText(dbVer);

Android - string-array values that have HTML

If I have this string array:
<string-array name="htmlstrings">
<item><i>blah<br />blah</i></item>
<item><b>1st line<br />2nd line</b></item>
</string-array>
And I get it and set it as text:
String[] htmlstrings = getResources().getStringArray(R.array.htmlstrings);
textV.setText(Html.fromHtml(htmlstrings[1]));
As output I get:
1st line2nd line
But I want to get
1st line
2nd line
For regular strings (not string-array) I know I can get and display HTML with getText() like:
textV.setText(getText(R.string.onehtmlstring));
but I don't know what's the getText() equivalent for a string-array.
Use getTextArray() to make a CharSequence array, and set your TextView using that array.
CharSequence[] htmlchars = getResources().getTextArray(R.array.htmlstrings);
textV.setText(htmlchars[1]);
Have you try below code:-
Spanned sp = Html.fromHtml( getString(R.string.htmlsource));
tv.setText(sp);
or
Set TextView text from html-formatted string resource in XML
Try with CDATA attribute:
<string-array name="channel_link">
<item><![CDATA[https://news.google.com/news/feeds?pz=1&cf=all&ned=in&hl=en&output=rss]]></item>
</string-array>

Categories

Resources