'Concatenating strings in Textview (in the layout)
I had a ressource string : "@string/audit" for Audit, in my XML Text view I tried to add a prefix "-" before my string Audit to get something liket that : - Audit
<TextView
android:id="@+id/openActionMenu"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_marginLeft="35dp"
android:padding="2dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/TealDark"
android:textSize="14sp"
android:layout_gravity="center"
android:text="- @string/audit"/> // <- this line
how to acheive that?
P.S.: I don't need a programmatic way, I look for in XML
Solution 1:[1]
Short answer is, you can't in xml
So instead, in your strings.xml
<string name="dash_audit">- Audit</string>
Solution 2:[2]
If you don't want to duplicate strings nor write any java or kotlin code to resolve placeholders at run time, you could use this library I've created: https://github.com/LikeTheSalad/android-stem
Which will concat as many strings as you like at build time, and will generate new strings that you can use anywhere as with any other manually added string.
For your case, you could define your strings like so:
<string name="audit">Audit</string>
<string name="dash_audit">- ${audit}</string>
And after building your project, you'll get:
<!-- Auto generated during compilation -->
<string name="dash_audit">- Audit</string>
You'll still have to use a different string name (dash_audit) in your layouts though ?but at least you keep the "Audit" value in a single place, so whenever you decide to change it, you just do it once and then the tool will update the generated strings with the new values. More info on the repo's page.
Solution 3:[3]
There are a couple of issues with the code. The first is that when iterating through the values in the dictionary, it returns after checking the first one, so it will only ever try and substitute Q for 0 and 8 for S. The second is that you are attempting two methods of processing the characters in the string: iterative AND recursive. You don't need to iterate over the index i with a for loop and also use recursion.
Another issue (which isn't a problem in your use case but stops the algorithm being more generic) is that the algorithm attempts to do a substitution in every case where an ambiguous character is encountered, as well as iterating over each value in the dictionary you should also consider the case where the character is left unmodified.
The function can be changed to remove the outer for loop and iterate over the values in the dictionary, test each one (recursing over the remainder of the string) and only return if a match is found. A simple way to do this is to store the result in a string and only return it if it is not the empty string (since your function returns the empty string when no match is found). If all the values in the dictionary have been tried and no match has been found, then it tries recursing without modifying the string.
public string combinations2(string date, int startPosition, Dictionary<char, String[]> dictionary)
{
Console.WriteLine("Call function " + date + ", " + startPosition);
if (string.Join("", date).Equals("250415")) //need to calculate checksum
{
Console.WriteLine("Found: " + date);
return date;
}
if (startPosition >= date.Length)
{
Console.WriteLine("Not Found: ");
return "";
}
if (dictionary.ContainsKey(date.ToCharArray()[startPosition]))
{
foreach (var value in dictionary[date.ToCharArray()[startPosition]])
{
string result = combinations2(date.Remove(startPosition, 1).Insert(startPosition, value), startPosition + 1, dictionary);
if(result != "")
return result;
}
}
return combinations2(date, startPosition + 1, dictionary);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ElliotM |
| Solution 2 | |
| Solution 3 | samgak |
