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

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!

Related

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 add the three values of textview and to store in one texview

String s1 = (getIntent().getStringExtra("Value1"));
String s3 = (getIntent().getStringExtra("value2"));
String s2 = (getIntent().getStringExtra("value3"));
id1 = (TextView) findViewById(R.id.id1);
id2 = (TextView) findViewById(R.id.id2);
id3 = (TextView) findViewById(R.id.id3);
id4 = (TextView) findViewById(R.id.id4);
id1.setText((s1));
id2.setText((s2));
id3.setText((s3));
How to add the values of the tex view set text values and to store the value as integer in one text view
id1.setText((s1));
id2.setText((s2));
id3.setText((s3));
then your question, add the three values of textview and to store in one texview
int first = Integer.parseInt(id1.getText().toString());
int second = Integer.parseInt(id2.getText().toString());
int third = Integer.parseInt(id3.getText().toString());
int final = first+second+third;
id4.setText(""+final);

Number range on an edittext - only want numbers between 1 and 10

I'm trying to make an app that only allows a user to enter between 1 and 10, I've tried writing my own method where if the number is out of that range, it changes the text views to ERROR and clears what's in the edit text, however it crashes the app. Does anyone know what's wrong with it? New to android.
Code:
public void buttonClick (View v)
{
TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
TextView tvNameEnt = (TextView) findViewById(R.id.NameEnt);
TextView tvNumEnt = (TextView) findViewById(R.id.NumEnt);
EditText num = (EditText) findViewById(R.id.ETnumber);
EditText name = (EditText) findViewById(R.id.ETname);
String nameContent = name.getText().toString();
String numContent = num.getText().toString();
tvName.setText(nameContent);
int value = Integer.parseInt(num.getText().toString());
if (value > 10)
{
tvNum.setText("ERROR");
num.getText().clear();
name.getText().clear();
}
else if (value < 1)
{
tvNum.setText("ERROR");
num.getText().clear();
name.getText().clear();
}
else
{
tvNum.setText(numContent);
tvNameEnt.setVisibility(View.VISIBLE);
tvNumEnt.setVisibility(View.VISIBLE);
tvName.setVisibility(View.VISIBLE);
tvNum.setVisibility(View.VISIBLE);
}
}
You have issue on this line
int value = Integer.parseInt(num.toString());
change to:
int value = Integer.parseInt(num.getText().toString());
Now you call toString() method from Object for EditText object. You have to call getText() method for it as first and after then call toString() method for CharSequence
UPDATE:
You find two times the same view with the same ids. Look at the code below:
TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
there should be R.id.tvNumEnt in the second time, I think so...
TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
The main problem here is that you are trying to get the number in the edittext with: num.toString(), instead you have to use: num.getText().toString()

Cant get the value of an Edittext [duplicate]

This question already has answers here:
Android getText from EditText field
(6 answers)
Closed 8 years ago.
hi i made a program that have EditText... i used for loop to set the number of my EditText, i cant get the value of my EditText(excellent), how can i do that? please help me..my output goes this way...
i want that 3,2,5,1,4 will display in my EditText,,
here are my codes...
final TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
TextView txt2 = new TextView (getApplicationContext());
TextView txt3 = new TextView (getApplicationContext());
TextView txt4 = new TextView (getApplicationContext());
TextView txt5 = new TextView (getApplicationContext());
TextView txt6 = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.addView(txt2);
tableRow.addView(txt3);
tableRow.addView(txt4);
tableRow.addView(txt5);
tableRow.addView(txt6);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Question ");
txt2.setText("Excellent ");
txt3.setText("Best ");
txt4.setText("Better ");
txt5.setText("Good ");
txt6.setText("Poor ");
txt.setTextColor(Color.BLACK);
txt2.setTextColor(Color.BLACK);
txt3.setTextColor(Color.BLACK);
txt4.setTextColor(Color.BLACK);
txt5.setTextColor(Color.BLACK);
txt6.setTextColor(Color.BLACK);
table.addView(tableRow);
TableRow tableRow2 = null;
EditText excellent = null;
EditText best = null;
EditText better = null;
EditText good = null;
EditText poor = null;
TextView name = null;
int j=0;
for(j = 1; j<=count; j++){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
tableRow2 = new TableRow (getApplicationContext());
excellent = new EditText (getApplicationContext());
best = new EditText (getApplicationContext());
better = new EditText (getApplicationContext());
good = new EditText (getApplicationContext());
poor = new EditText (getApplicationContext());
name = new TextView (getApplicationContext());
//i want to retrive the value of this --->//excellent.setBackgroundColor(color);
best.setBackgroundColor(color);
better.setBackgroundColor(color);
good.setBackgroundColor(color);
poor.setBackgroundColor(color);
name.setText("Q#"+Integer.toString(j));
tableRow2.addView(name);
tableRow2.addView(excellent);
tableRow2.addView(best);
tableRow2.addView(better);
tableRow2.addView(good);
tableRow2.addView(poor);
table.addView(tableRow2);
}
final StringBuilder output = new StringBuilder();
final String[] a = excellent.getText().toString().split(",");
output.append(a+",");
TableRow tableRow1 = new TableRow (getApplicationContext());
Button get = new Button(getApplicationContext());
tableRow1.addView(get);
get.setText("Get!");
get.setTextSize(8);
//******************************************************************************//
// GET! //
//******************************************************************************//
get.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText x = new EditText (getApplicationContext());
x.setText(output);
table.addView(x);
}
});
//******************************************************************************//
// END OF GET! //
//******************************************************************************//
The problem is that this
output.append(a+",");
Prints a representation of the object a, not the strings that a contains. Try somthing like this:
for(String s : a){
output.append(s);
}
There are multiple things going wrong here.
you split on "," but that is not what you want. you want the excellent.getText().toString(); of the EditTexts
You append the text it is just after creation. It will be empty string at that time.
What you want to do
Make an ArrayList of EditText
Put all excellent EditTexts in it.
In the onClick go through this list and append all the getText().toString() of these EditTexts
I won't give you an exact implementation. You should be able to figure that out on your own.
Your split function should be followed by a FOR loop. Split gives you a array.
So you need to iterate the array and append inside the lopp. Example
String str = "one-two-three";
for (String val: str.split("-", 2)){
// Append your output with val here
}
Cheers

Android string formatting

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.

Categories

Resources