Strings.xml and append - java

I am playing a bit around with Android after doing a number of tutorials.
I have a textview in which I want to print various lines of text that I have written in the strings.xml file:
<string name="welcome">Welcome! What is your name?</string>
<string name="welcome2">Welcome!</string>
In my main I have:
gamehistory = (TextView)findViewById(R.id.textView2);
gamehistory.setText(R.string.welcome);
This works fine and it displays the text. I want to use the textbox as a "log" so that a bit later in my code it prints welcome2 in the next line.
gamehistory.append("\n" + R.string.welcome2);
Unfortunately, when I use append, it turns the string into a number.
Is there a way to avoid this?

To append i think will not be there but yes you can concatenate like this
String outStr = getString(R.string.first) +
" " + getString(R.string.second);
For Refrence Link to refrence

String welcomestr = Context.getResources().getString(R.string.welcome2)
gamehistory = (TextView)findViewById(R.id.textView2);
gamehistory.setText(welcomestr);

Use the Context.getResources().getString(id) for that.
getReources().getString(R.string.welcome2); // Since you're calling this in your activity itself.
because R.string.welcome2 is an auto generated integer value for your string resource.

Try adding carriage return.
gamehistory.append("\r\n" + R.string.welcome2);
Or add these lines to TextView part of xml:
android:maxLines="2"
android:minLines="1"

Related

Confused regarding split in Java

I am trying to take a text from a file, and take the a's and b's out using split function.
String inStr = in.readLine();
// for example "a1a1a1a1b"
String lettersStr = letters.readLine();
// for example "ab"
Then i'm doing this trying to split all the letters i want.
Why is this not working?
String outFinal = "\"\\\\s*["+ lettersStr +"]\\\\s*\"";
String[] inSplit = inStr.split(outFinal);
What i'm trying to accomplish is
inStr.split("\\s*[ab]\\s*"));
Which works fine but the problem is that since i'm using a BufferedReader (fileread) the letters to cut out keep changing, hence why i can't just use the line above.
Thanks in advance
Regards
Change
String outFinal = "\"\\\\s*["+ lettersStr +"]\\\\s*\"";
to
String outFinal = "\\s*["+ lettersStr +"]\\s*";

setText method not working properly?

I am trying to execute the settext method on my textview but it doesn't seem to work. I learned that "%s" in the first argument of the string.format method should return the given string of the second argument but it somehow it doesn't work.
SPCalories = Double.longBitsToDouble(sharedPreferences.getLong("Calories", Double.doubleToLongBits(0)));
d_c = SPCalories;
dc_text = Double.toString(d_c);
Calories_text.setText(R.string.Calories + String.format("%s", dc_text));
R.string.Calories:
<string name="Calories">Calories: </string>
Am I doing something wrong with SharedPreferences maybe?
You should do:
<string name="Calories">Calories: %s</string>
And:
Calories_text.setText(String.format(getString(R.string.Calories), dc_text));
You omitted getString method:
Calories_text.setText(getString(R.string.Calories) + String.format("%s", dc_text));
If you wold like to learn more about formatting strings check:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling.

Determine a number from text string

I want determine number from specify string.
Ex: I have many text strings, such as "3.2p" or "3.2px" or "xp3.2" or "p3.2x".
The final result I want is can get number from text in above. Expected result "3.2".
People who know,
Please help me,
Thanks,
I would first remove all the non-numeric characters using a regex, then parse what remains.
String str = input.replaceAll("[^\\d.]", "");
Float.parseFloat(str);
Use this:
String s = "ffffa32.334tccy";
s = s.replaceAll("[^\\d.]", "");

Add three dot to the end of dynamic text android

I have to add 3 dots to the end of a text in a textview. My textView is in an lazy list adapter so the content change very often.Only two lines are shown in the textview.
e.g. suppose text is
abcdefghigdddddddd
dddddddddddddddddddd
result should be
abcdefghigdddddddd
ddddddddddddddddd...
I know i should use .replace("...","");
but how to always write off the last thre words ?
My textview is as follows
holder.title.setText(Html.fromHtml(a.name));
where a.name is the data to be set.
You can add 'elipses' attributes to your TextView, and also specify that the text view is just one line. It could look like this:
<TextView
android:id="#+id/text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1" />
Just set your TextView property android:ellipsize="end" and you will get the result.
First you fetch the text in a String. Then you fetch the length of the string. Then you can fetch the index of the third character from last. The you can easily replace the three last characters with ...
All this can be done using the methods of String class in Java.
Suppose you want to display only 4 characters from the string "raviranjan", and you want to trim all other characters by replacing them with "...". The do as following:-
String str = "raviranjan";
int length = str.length();
String subString = str.substring(0, 3); // takes four characters from beginning
String str2 = subString + "..."; // Add ellipsis
Now str2 is the string you want....
make textView ellipsis and let android truncate it automatically for you .
<TextView ....... bla- bla -------
.......................
android:elipsis="true"
android:lines="num_of_lines" />
If you want to replace extra characters with ellipses, you might be able to get away with something like so:
String str = "abcdefghigdddddddd dddddddddddddddddddd";
String newStr = str.replaceAll("^(.{100})(.*)$","$1...");
The above should match the first 100 characters and throw them in one group, while throw the other characters in another group. The result will be then a new string which is made up from the first group followed by 3 dots.
You can change the value of 100 to whatever amount of characters you want before the ellipses.
you want to add 3 dots at end of your text then
holder.title.setText(Html.fromHtml(a.name+"..."));
for remove 3 char & add 3 dots..
String a = a.name;
a.substring(0, a.length() - 3);
holder.title.setText(Html.fromHtml(a.name+"..."));

Java TextArea and String (print only 1 word of String)

I got problem with my TextArea
String A contain text a,b,c,d
I converted String to textarea using method TextArea.setText(A);
My problem is that textarea print out abcd instead of it I want it printed in lines example
A
B
C
D
I did read book and tried google but I can't find solution to my problem ;(
Sounds like you need to follow the javadoc that JB Nizet linked to above, and take advantage of the String.replace() method. It takes two CharSequences, first the characters to match, the second the characters to replace it with. Find the ", " and replace with "\n". So
CharSequence theseChars = new CharSequence(", ");
CharSequence withTheseChars = new CharSequence("\n");
String newString = A.replace(theseChars, withTheseChars);
And that should get the job done.
I have used the most basic stuff of Java.
I think this is easy to understand
String s = "a,b,c,d";
String s1 =s.replace(",", "");
String s2 = s1.replace("", "\n").toUpperCase();

Categories

Resources