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);
Related
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
I made a calculator app and I made a clear Button that clears the TextView.
private TextView _screen;
private String display = "";
private void clear() {
display = "";
currentOperator = "";
result = "";
}
I got this code from Tutorial and set the clear button onClick to onClickClear, so it do that part of the code and it works. Now I have made this code delete only one number at a time and it don't work. What can be done to delete only one number at a time?
public void onClickdel(View v) {
display = "";
}
Below code will delete one char from textView.
String display = textView.getText().toString();
if(!TextUtils.isEmpty(display)) {
display = display.substring(0, display.length() - 1);
textView.setText(display);
}
You are modifying the string and not the textview.
To clear the TextView use:
_screen.setText("");
To remove the last character:
String str = _screen.getText().toString();
if(!str.equals(""))
str = str.substring(0, str.length() - 1);
_screen.setText(str);
String display = textView.getText().toString();
if(!display.isEmpty()) {
textView.setText(display.substring(0, display.length() - 1));
}
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(","," ");
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]);
I have lots of editText fields and the user can add info into them.
from these editTexts i want to create one string. im using the stringBuilder at the moment. however if the user does not enter anything to some of the editTexts, i want the stringbuilder to ignore these fields. is this possible? and if so, how can i do it?
this is what im doing at the moment:
String baseString = editText1.getText().toString();
String string2= editText2.getText().toString();
String string3= editText3.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(baseString);
superStringBuilder.append(string2 + string3);
String superString = superStringBuilder.toString();
thank you
You can do something like:
If (string2.equals("")){
//Then do something when the edit text is blank.
superStringBuilder.append(string3);
} else{
superStringBuilder.append(string2 + string3);
}
Hope that helps.
thanks to your help this is an example for others if they have the same problem.
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
String string4 = editText4.getText().toString();
String string5 = editText5.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(string1);
if (string2.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string2);
}
if (string3.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string3);
}
if (string4.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string4);
}
if (string5.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string5);
}
String superString = superStringBuilder.toString();
this will make the string filter out the editText with no text in them :) so the new string created with stringbuilder is either 4 strings or 2 strings :)
thank you