I try to put one or more variables in a TextView.
For exemple :
Hello I am a "girl" and I live in "Boston"
I would like to know what is the best way to do it :
Can i do it directly in the layout file ?
Can i do it only via Java Class ?
Can i do it via values/styles.xml ?
For now i do it like this :
String text1 = "Hello I am a ";
String text2 =" and I live in ";
String var1= preferences.getString("sex", "null");
String var2= preferences.getString("city", "null");
TextView Text = (TextView) findViewById(R.id.text);
Text.setText(text1 + var1 + text2 + var2);
It works yes but in fact my TextView are very long so I don't think my way is really appropriate.
Can i have some advice ?
Use String.format(String format, Object... args)
String sex = preferences.getString("sex", "null");
String city = preferences.getString("city", "null");
String str = String.format("Hello I am a %s and I live in %s", sex, city);
TextView text = (TextView) findViewById(R.id.text);
text.setText(str);
Note - Avoid concatenation in TextView.setText()
If Text-view is really long then you should be try this.
String var1= preferences.getString("sex", "null");
String var2= preferences.getString("city", "null");
Text.setText("Hello I am a"+var1+"\n"+"I live in"+var2);
This is good way Present Text in Text View
Related
What I have : text1,text2,text3
What I want : text1 text2 text3
replace comma with space?
final String s = "text1,text2,text3".replace(",", " ");
I tried with both replace and replaceAll. But didn't work
This is because both replace() and replaceAll() don't change the String object, they return you a new one. Strings are immutable in Java.
Try This Way:
String data = "text1,text2,text3";
String temp = data.replace(","," ");
Now You have all
This what you should do
String str = "text1,text2,text3"
str = str.replace(","," ");
I have not found documentation on how I can get the first letter of a value in a TextView?
Very easy,
String strTextview = textView.getText().toString();
strTextView = strTextView.substring(0,1);
Alternatively you can try following way too
char firstCharacter = textView.getText().toString().charAt(0);
To get the first letter you'll have to make this call:
char firstCharacter = myTextView.getText().charAt(0);
Use the method from below. Provide the string from TextView as the parameter.
public String firstStringer(String s) {
String str= s.substring(0, Math.min(s.length(), 1));
return str;
}
You can use this
TextView tv = (TextView) findViewById(R.id.textview);
String frstLetter = tv.getText().substring(0, 1);
it is simple. To retrieve the Text from the TextView you have to use getText().toString();
String textViewContent = textViewInstance.getText().toString();
and the first letter textViewContent.charAt(0)
To fetch the content of the string from TextView:
String content = textView.getText().toString();
To fetch the first character
char first = content.charAt(0);
Try this
String value = text.getText().toString();
String firstTen = value.substring(0, 1);
Example: I have a EditText and I want to check the first word is the city name and second word is the pincode. These both words are separated by comma(,).
Hey try this if you don't want to use split. YOu need to get string into a variable from edittext and then use the following code for doing yourself able to validate :)
String str = "tim,52250";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Do this way..
String content="Mehsana,384001";
String[] contentArray=content.split(",");
And you will get
contentArray[0]=Mehsana
contentArray[1]=384001
then you can validate each string content..
Use split() to get the things done.
Ex:
String s= "abc,123"
String s1[]=s.split(",");
String city=s1[0];
String pincode=s1[1];
Try this
String strInput = editText.getText().toString();
String strSplit [] = strInput.split(",");
System.out.println("CityName : " + strSplit[0]);
System.out.println("PinCode : " + strSplit[1]);
String data = "ali,524513"
String []array = data.split(",")
you can validate array[0] and array[1] :)
System.out.println("Name: "+array[0]+" code: "+array[1]);
1) is there any easy way for screen output like in Pascal: write(A,'+',B,'=',C) ?
I tried :
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
Toast.LENGTH_LONG).show();
int A,B,C;
A=4;
B=8;
C=A+B;
String text = getString(A,"+",B,"=",C);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(text);
//Toast.makeText(this,text, Toast.LENGTH_LONG).show();
}
but ít doesn't work.
Every time I will get "...apliccation has stopped"
the best would be something like :
tv.setText(A,"+",B,"=",C) //without stringing integers;
thanks for all your help.
Concatenate the string and use setText afterwards:
tv.setText(A + "+" B + "=" + C);
For longer text, use a StringBuilder.
This is wrong getString(A,"+",B,"=",C)
you want
String text = A + "+" + B + "=" + C;
getString is used for retrieving package string resources
http://developer.android.com/reference/android/content/Context.html#getString(int)
I have a textview that I'm setting the text from a value obtained from a SimpleCursorAdapter. The field in my SQLite database is a REAL number. Here is my code:
// Create the idno textview with background image
TextView idno = (TextView) view.findViewById(R.id.idno);
idno.setText(cursor.getString(3));
My issue is that the text display a decimal. The value is 1081, but I'm getting 1081.0000. How can I convert the string to not display the decimals? I've looked into the formatter, but I can't get the syntax right.
TextView idno = (TextView) view.findViewById(R.id.idno);
String idno = cursor.getString(3);
idno.format("#f4.0");
idno.setText(idno);
Thanks in advanced!
You can use String.format:
String idno = String.format("%1$.0f", cursor.getDouble(3));
Can also you DecimalFormat:
DecimalFormat df = new DecimalFormat("#");
String idno = df.format(cursor.getDouble(3));
If you get a String with a decimal point, you can simply do:
idno.setText(cursor.getString(3).split("\\.")[0]);
// Split where there is a point--^ ^
// |
// Get the first in the array--------+
Note that this:
TextView idno = (TextView) view.findViewById(R.id.idno);
String idno = cursor.getString(3);
is illegal, as you use the same variable name.