Android string formatting - android

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.

Related

How to textview2 getText from String resource by my textView1 is equal string name

"If the value in textView1 is equal string name I want string to show textView2"
// String Example
<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>
//Example
if textView1 = a
textView2 Will Show Apple
Here you have stringName as a text in your textView1.
You can fetch String value programatically from resourses by stringName.
Step1 : first fetch this both values, and store it in a variable;
String mTvTextStrName = textView1.getText().toString().trim();
String strValue = getStringResourceByName(mTvTextStrName);
use this method to fetch String value.
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
You can also check this values via Log.e.
Step2 : Now set your strValue in your textView2.
// Do your code.. Show Apple
textView2.setText(strValue);
Try using getIdentifier() method of getResources() like below.
TextView textView1= findViewById(R.id.textView1);
TextView textView2= findViewById(R.id.textView2);
String textViewText=getResources().getString(getResources().getIdentifier(textView1.getText().toString(), "string", getPackageName()));
textView2.setText(textViewText);
Referenced from https://stackoverflow.com/a/17086690/9502601

Best way to put variables in a Textview?

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

Displaying the sum value of a SQLite query in a textview as currency

I have the sum total of the income column of my database displaying as a double in a textview but I can't get it to display as currency. When I debug, the results show it should display as "$ 10,432.18", but it just shows as "10432.18". Below is what I used in a listview for the individual income items that worked in the listview, but not in the textview:
TextView cmIncomeSumTextView = (TextView) findViewById(R.id.cm_income_sum_text_view);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(',');
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat("$ #,###.00", symbols);
String cmIncomeDecimal = decimalFormat.format(cmIncomeSum());
cmIncomeSumTextView.setText(cmIncomeDecimal);
any help would be appreciated!
You can try this instead.
string pattern = "$###,###.###";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String format = decimalFormat.format(123456789.123);
System.out.println(format);

How to display EditText in a TextView after changing it to BigDecimal?

I want to get a precise number from an EditText then change in to a BigDecimal and show it in another TextView.
Here's a part of my code:
BigDecimal rate_string = new BigDecimal(0);
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = (BigDecimal) rate.getText();
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());
But it doesn't work!
What should I do?
Thanks
BigDecimal rate_string = new BigDecimal(0);
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = (BigDecimal) rate.getText();
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());
to
BigDecimal rate_string; // P.S here is useless the creation
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = new BigDecimal( rate.getText().toString() );
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());
You cannot convert a String to a BigDeciaml using a cast but BigDecimal have a constructor which takes the value as String.
P.S Make sure you pass a correct number to the constructor or it will throw a NumberFormatException!

How get the first letter of TextView in Android?

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);

Categories

Resources