Android: XML Textview text Property - java

I had seen an Android Tutorial that allows a simple android:text just like the example bellow:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Application"
android:textColor="#ffffff" />
But in my eclipse,
it says:
[I18N] Hardcoded string "Android Application", should use #string resource
What should I do?
I need to insert a lot of Text just like,
Accounting has begun since time immemorial. Consider this truth: God said unto Noah; “and every living thing of all flesh, two of every sort shall thou bring into the ark, to keep them alive with thee; they shall be male and female. Of fowls after their kind, and of cattle after their kind, of every creeping thing of the earth after his kind, two of every sort shall come unto thee, to keep them alive. And take thou unto thee of all food that is eaten, and thou shall gather it to thee; and it shall be for food for thee, and for them.” The Bible – Genesis 6:19-20. “Of every clean beast thou shall take to thee by sevens, the male and his female: and of the beast that are not clean by two, the male and his female. Of fowls of the air by sevens, the male and the female; to keep seed alive upon the face of all earth.” The Bible – Genesis 7:2-3
Any suggestion or tell me what the better thing to do?

This is just a warning.
But by using a string resource, you can support multi-languages.
You have just to create a new string resource in the file strings.xml (res/values/strings.xml).
Then add the line :
<string name="your_name">Your text</string>
and just change :
android:text="Android Application" by android:text="#string/your_name"
Now if you want to add a new language to your application, let's say in french, you will have just to create a new folder in the res folder called values-fr and copy/paste the file strings.xml you already have and finally translate the strings you have defined (i.e <string name="your_name">Your text translated in french</string>)
You can have a look at this (problem resolved using screenshots).

You can use hardcoded strings in your layout (or in code), it is just a warning and eclipse won't fail on compilation.
But android best practice is the remove all strings and texts from your app and layout and put it in one place- so it will be much easier to translate later.
So what eclipse suggest you is to extract the "Android Application" text to strings.xml file (that should contain all your app's strings).
If it's just for the tutorial you can ignore it, or even better- just let eclipse extract the text to strings.xml using quick fix

Related

Properly split a long string to be read by Talkback

I have a long string in my android app that looks like this:
<string name="imprint_text" translatable="false">
...long text here...
</string>
The problem is that Talkback is only able to read the whole string from the beginning, indifferent of where on the screen you touch. What is the best way for this to be split into different paragraphs? Should I just create different strings (imprint_text1, imprint_text2...) and bring them in the correct order or is there a better way to do this?
Unfortunately, I don't think that this is possible. Keep in mind that there's also a limit of 4000 characters if I'm not mistaken. My suggestion would be to use TextToSpeech. You can start it when the user opens the screen or when they tap on anywhere (probably the first one would be better, but it depends on the app). It has a lot of options for setting the speed and even a playSilence method.

Option to change language in an android app

I'm building an android app where i have a settings activity.
In this settings Activity, I have a ListPreference with 3 values, each for a language.
I would like to get the value selected by the user and then change the language of the application according to the choice of the user
How would you do this guys ?
Thanks for reading and help !
The link in the comments above is the complete answer, but just to get you started, to support whatever language the user's device is set to, you need to extract all of the text in your app that will be visible on screen out to a 'strings.xml' file. Then translate all of the strings in that file into a separate 'strings.xml' file for each language you wish to support. This file lives in the res/values folder, and if you wanted to do a Spanish translation, you would create a res/values-es folder, and add a 'strings.xml' file there, with entries that have the same 'name' as the English translation, but with the Spanish translation as the 'value' example:
res/values/strings.xml
<string name="change_voter">Change Voter</string>
res/values-es/strings.xml
<string name="change_voter">Cambiar de Votantes</string>
//Then you get the value into your label like so:
tvVoter.setText(getResources().getText(R.string.change_voter));
You will also need to consider any graphics (bitmaps, backgrounds, buttons) and if they have words written in the default language, there will need to be copies of those translated into the other language and in another localized folder.

How to set subscript and superscript text in string (such as math exponents)

I'm working on an Android app, and I need to be able to include the exponents/powers into a string (Java). I've done some searching and it seems that my scenario is making it difficult. I've looked into options such as ALT+ codes and Unicode. I'm not entirely sure if either of those, or any other options, are actually possible (I know the ALT+ codes only have '1' '2' and '3' subscripts oddly enough).
So, what's my best option, if any?
I'd like to keep it as simple as possible. I have a lot of strings to write (like math problems) and the need to subscript/superscript numbers is random. Can this be done easily?
EDIT: I suppose I was vague. My app is for studying. And the questions/answers are put into arrays and randomized each time. I'd like to be able to set the strings in the Java file as they are now
question[0].text = "solve x^2 if x=5";
But instead of the user seeing "^2", I'd like to use the actual superscripts.
ANOTHER EDIT:
global.java (class/structure that holds questions/answers)
question[0].question = "Question Text. I would like to add a superscript or two here, as if it were a math problem";
question[0].answer[0]. = "Answer 1. Add a subscript here";
question[0].answer[1]. = "Answer 2. Add another superscript here.";
question[0].answer[2]. = "Answer 3. Etc.";
question[0].answer[3]. = "Answer 4. Etc.";
Activity XML
<!--QUESTION-->
<TextView
android:id="#+id/textQuestion"/>
<!--ANSWERS-->
<Button
android:id="#+id/answerOne"
android:onClick="checkAnswer"/>
<Button
android:id="#+id/answerTwo"
android:onClick="checkAnswer"/>
<Button
android:id="#+id/answerThree"
android:onClick="checkAnswer"/>
<Button
android:id="#+id/answerFour"
android:onClick="checkAnswer"/>
Activity Java
//SET QUESTION STRING TO TEXTVIEW
textQuestion.setText(questionDatabase.getQuestion(index));
//SET ANSWER STRINGS TO BUTTONS' TEXT
buttonOne.setText(questionDatabase.getAnswer(index, 0));
buttonTwo.setText(questionDatabase.getAnswer(index, 1));
buttonThree.setText(questionDatabase.getAnswer(index, 2));
buttonFour.setText(questionDatabase.getAnswer(index, 3));
"NEXT" and "PREVIOUS" buttons allow the user to move through the questions by incrementing/decrementing the "index".
So, because of this, the activity never knows (I don't think) which questions/answers need text to be superscript, and why I'd like to be able to set the questions/answers sub/superscript myself when I enter the questions/answers into the array. Can this be done?
EDIT: #jared burrows was very helpful in chat and has helped me almost reach the end of my issue. I'm now able to use setText(Html.fromHtml()); on my textview (question text) and it shows the subscript and superscript properly. But on my buttons (answers) I tried to employ the same tactic and it doesn't work the same. "X2" just becomes "X2". Why is this? Why don't my buttons accept the html just like the textview.
Superscript:
textView.setText(Html.fromHtml("X<sup>2</sup>"));
Subscript:
textView.setText(Html.fromHtml("X<sub>2</sub>"));
References:
Subscript and Superscript a String in Android
http://www.w3schools.com/tags/tag_sub.asp
http://www.w3schools.com/tags/tag_sup.asp
The answer to this was a combination of help from #Jared Burrows and AlanV. So thank you to you both.
view.setText(Html.fromHtml(stringGoesHere));
This does work, but out of the box it doesn't work for buttons due to some frustrating feature in (I think) Android 5.0. Buttons are automatically set to all caps, and because of this, it overrides the html formatting.
So, to allow buttons the ability to use html properly, you must set the attribute to false.
<button
android:textAllCaps="false";/>
Thanks again to Jared Burrows and AlanV.

Java "mystic" menu name

I want to know more about the menu above, but I can't find a name for it.
This picture is taken from Spotify, and I guess everybody knows what it does: Stores all "Tracks" under Track, All Artists under Artist etc. Also when you click on e.g Track, it will sort them from a-z (same menu are used in Windows, inside folders).
I'm not even sure this is a Java feature, but I'll have to ask anyway!

Android string.xml error for every character I type

I am going through the android hello world tutorial, and have been stuck when trying to create an XML UI. For some reason, even on a new program, in which I have made no changes to the default build, it gives the error java.lang.NullPointerException after every character I type. I can't figure out why it is doing this, as I am just trying to edit the text between the Text I want to set it to say something other than what is set by default. However, even with a fresh build, no changes, and I just try to change the text within the xml tags, it still gives the error. What do I need to do to allow it to let me type? I am using the eclips IDE and the android sdk. I was able to do the first part of the tutorial that doesn't involve XML.
Just guessing, but I suspect you are doing something like the following:
<TextView android:text="#string/hello" />
and you are editing it to
<TextView android:text="#string/helloWorld" />
without creating a reference in res/values/strings.xml .
If this is the case, go to strings.xml and edit the proper string there, for example
<string name="hello">Hello World!</string>
becomes
<string name="hello">Hello everybody!</string>
Ok, I finally found the answer somewhere else, it was something wrong with how the file was created by default. I have to add the element xmlns:android as follows <resources xmlns:android="http://schemas.android.com/apk/res/android" />
Interestingly enough, the file will work if I type it one character at a time, dismissing the pop up with each keystroke. However the new element eliminates the pop ups. I am not sure why the error would pop up, yet the program still compile and run correctly on my avd. Oh well, if you have this error add the element and it goes away
I think in your typing contain UTF-8 not pure ASCII. You can change in eclipse by
in Ecipse IDE Window> preference> Under General tab, select workspace.
In text file encoding choose other, in these choose UTF-8 . It will be ok

Categories