Change text view text based on phone language without using string resources - java

How can I change the text within my textview based on the language set on the device without using string resources? I don't want to use string resources. As an example could someone please tell me what code I can use if the device language is in French and whether or not I'd need to use else if or else for including other languages.
TextView txt = (TextView)v.findViewById(R.id.WCBank_textView1);
txt.setText(Html.fromHtml("<font color='#E32017'>Red apples,</font>" +
"<font color='#FFD300'> yellow bananas,</font>" +
"<font color='#00782A'> green grapes</font>"
));

I still prefer string resources for i18n (not for the individual words, but for the whole expression). That said, you could try something like this:
if (Locale.getDefault().equals(Locale.FRENCH) || Locale.getDefault().equals(Locale.FRANCE)) {
// do the french stuff...
txt.setText(...);
} else if ...

Related

How to change the color of specific strings in TextView

I have a scrollable TextView that has texts like this
...
Tom: sometext
Jack: anothertext
Sam: something
...
So I get this data as a JSON object, then I seperate the keys and values, then adding to a string. Then I set the text of TextView as this final string.
What I want is, I would like to make different colors for Tom, Jack, Sam. I have found HTML library but all solutions changes the whole TextView. I want to change specific parts of the text and since I get a JSON data first and I'm adding this JSON values to a string part by part, I thought I can do something like this string += <HTML color change> JSON["key"] + JSON["anotherKey"].
Is it possible to change specific parts of the text of a TextView object?
Take a look at spannable Strings. You will probably end up with something like this.
val spannable = SpannableStringBuilder("Text " + JSON["key"])
spannable.setSpan(
ForegroundColorSpan(Color.RED),
5, // start
5 + JSON["key"].length, // end
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
See https://developer.android.com/guide/topics/text/spans for more info regarding spans.
Note: this example is in Kotlin but it's pretty much the same in Java.
You can change text color in TextView using below code snippets.
val blackColor= ContextCompat.getColor(context, R.color.black)
val redColor= ContextCompat.getColor(context, R.color.red)
textView.text = HtmlCompat.fromHtml("<font color=\"$blackColor\">Welcome to </font> <font color=\"$redColor\">Stack Overflow </font>", HtmlCompat.FROM_HTML_MODE_LEGACY)
Above code is in kotlin , its same as Java you can convert to Java.
Hope it will work for you !

JasperReports export to Excel uses only last set background color

Im pretty pretty new to Dynamic-Jasper, but due to work i had to add a new feature to our already implemented solution.
My Problem
The Goal is to add a Column to a report that consists only out of a background-color based on some Information. I managed to do that, but while testing I stumbled upon a Problem. While all my Columns in the html and pdf view had the right color, the Excel one only colored the fields in the last Color.
While debugging i noticed, that the same colored Fields had the same templateId, but while all Views run through mostly the same Code the Excel one showed different behavior and had the same ID in all fields.
My Code where I manipulate the template
for(JRPrintElement elemt : jasperPrint.getPages().get(0).getElements()) {
if(elemt instanceof JRTemplatePrintText) {
JRTemplatePrintText text = (JRTemplatePrintText) elemt;
(...)
if (text.getFullText().startsWith("COLOR_IDENTIFIER")) {
String marker = text.getFullText().substring(text.getFullText().indexOf('#') + 1);
text.setText("ID = " + ((JRTemplatePrintText) elemt).getTemplate().getId());
int rgb = TypeConverter.string2int(Integer.parseInt(marker, 16) + "", 0);
((JRTemplatePrintText) elemt).getTemplate().setBackcolor(new Color(rgb));
}
}
}
The html view
The Excel view
Temporary Conclusion
The same styles uses the same Objects in the background and the JR-Excel export messes something up by assigning the same Object to all the Fields that I manipulated there. If anyone knows of a misstake by me or potential Solutions to change something different to result the same thing please let me know.
Something different I tried earlier, was trying to set the field in an evaluate Method that was called by Jasper. In that method we assign the textvalue of each field. It contained a map with JRFillFields, but unfortunatelly the Map-Implementation denied access to them and just retuned the Value of those. The map was provided by dj and couldn't be switched with a different one.
Edit
We are using JasperReports 6.7.1
I found a Solution, where I replaced each template with a new one that was supposed to look exactly alike. That way every Field has its own ID guaranteed and its not up to chance, how JasperReports handles its Data internaly.
JRTemplateElement custom =
new JRTemplateText(((JRTemplatePrintText) elemt).getTemplate().getOrigin(),
((JRTemplatePrintText) elemt).getTemplate().getDefaultStyleProvider());
custom.setBackcolor(new Color(rgb));
custom.setStyle(((JRTemplatePrintText) elemt).getTemplate().getStyle());
((JRTemplatePrintText) elemt).setTemplate(custom);

How to fix Translation not working with TextView.setText()?

I'm localizing my application and currently facing a problem. I have a textview which shows when the application was last updated abd I have a string resource "Last Updated: " for that. I used
lastUpdate.setText(R.string.last_updated + dateString);
My code works perfect in English language but in case of other languages the Textview acts strange and shows numbers in place of the text.
For example: When I set the language to Hindi the text Last Updated gets changed to 2131689604. How can this issue be overcome?
Get string by using this
lastUpdate.setText(getResources().getString(R.string.last_updated) + dateString);

how to set specific language for a word in android string resources file

I am working with an Android app, which shows some text in Bengali.
This sentence is in the app resources string file:
<resources>
<string name="sentence">১৯৭৫ সালে বিল গেটস(Bill Gates) এবং পল এলেন(Paul Allen) একসাথে "মাইক্রোসফট"("MicroSoft") নামক কোম্পানি প্রতিষ্ঠা করেন, যেটা পরবর্তীতে পৃথিবীর সবচেয়ে বড় পিসি কোম্পানির মর্যাদা পায়।</string>"
</resources>
I want to show this whole sentence in Bengali, so I set typeface to my textview and I can use different Bengali fonts, but the problem is I need to show names (Bill Gates, Paul Allen and Microsoft) in English instead of Bengali font. How can I do it?
You can use HTML commands in your text view and then apply different typefaces to different parts of this TextView.
You can do this:
textView.setText(Html.fromHtml("<font size=... color=... face=...>" + getString(R.string.sentence) + "</font>"))
This would set the typeface of the whole sentence to the defined one. To do this only on parts, save the String resource to a local variable, and then apply the different Html commands to the desired parts.
Also see here and for the allowed Html tags here

How to enter left parentheses into a text box

I have a requirement to pass the text "English (United States)" into a text box. But when I am using the below command:
driver.findElement(By.id("defaultLang")).sendKeys("English (United States)");
It is entering only "English United States) into the text box. the Open Parentheses is missing when WebDriver is writing the text into the textbox.
By using this you can enter the left parenthesis in text box.
String a = (Keys.chord(Keys.SHIFT,"9"));
String b = "AAA";
String c = "BBB)";
driver.findElement(By.id("ID")).sendKeys(b+a+c);
Resultant O/P is : AAA(BBB)
You should use escape to send special characteres in selenium web driver,
I think you can try something like this :
driver.findElement(By.id("defaultLang")).sendKeys("English \(United States\)");
I hope it may help you
This looks like a known bug. See issue 6822 here.
What you can do now, is to set it by injecting JavaScript directly.
// untested Java code, provides only the logic, you need debug yourself
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].setAttribute('value', arguments[1]);", driver.findElement(By.id("defaultLang")), "English (United States)");

Categories