I want to divide two numbers fetched from edit text and save it in a TextView but I am unable to save a float type Number into String
tv.setData(div.toString()); //not working
In this code, where tv is TextView type variable and div is float type variable.
Use String.valueOf(div) instead of div.toString() like so:
tv.setText(String.valueOf(div));
Related
This is how i am setting text to a TextView using a place holder.
myTextView.setText(String.format(getResources().getString(R.string.total_savings), totalSavings));
This is string resource value
<string name="total_savings">Total Savings: %1$d</string>
Now, I need to get the value of %1$d from the TextView.
My question is, how do I get that decimal integer value from the TextView by using the getText() method.
Set this text in String.xml file
<string name="total_savings">Total Saving : %1$d</string>
In Java file for set the text Use
myTextView.setText(getResources().getString(R.string.total_savings));
To get the value of textview use
String output = myTextView.getText().toString();
You should have totalSavings stored somewhere in your data structure.
However if you want to get the value from the TextView then
String input = mTextView.getText().toString();
input = input.subString(15, input.length());
Since you are getting the value from a TextView where user cannot change data as he pleases, there is no need for validation.
How can I set textsize from setting preference fragment into textView? The font size is selected from a listView I will provide the code below the image.
Please see my image below:
I know I am doing something wrong. Please show me a proper way to do this.
This is my font size value:
This is my listpreference:
<ListPreference
android:title="Font Size"
android:summary="Select desirable font size"
android:key="FontSizeKey"
android:entries="#array/select_font_size"
android:entryValues="#array/select_font_size_value"
android:defaultValue="24"/>
and also if you guys have any website/tutorial/blogs link for me to refer, it would be good.
The value which TextView accept is float. One more thing is that to get the value from SharedPreference, use KEY.
String setFontSize = getSharedPreferences.getString("FontSizeKey");
setDuaText = setTextSize(Float.valueOf(setFontSize));
convert string to float using this method
secDuaText.setTextSize(Float.parseFloat(setFontSize));
From the hint its quite clear that you the method is expecting a float value. And you are passing it a string value. So either save it as a float using putDouble in your shared prefs and use getDouble and save it in a float variable. Like below
SharedPrefs.putDouble("FontSizeKey", 24.0);
double setFontSize = SharedPrefs.getDouble("FontSizeKey");
Else, just parse the String for a float value like below
secDuaText.setTextSize(Float.parseFloat(setFontSize));
Double x=Double.parseDouble(textview.getText().toString());
Double y=Double.parseDouble(edittext.getText().toString());
Double z=x*y;
finalcost=z+costofevents;
or is their any new way to find two value and add to finalcost object
If Double.parseDouble(...) is what causing problem then try converting String to Double like
Double.valueOf(textview.getText().toString());
Edit:
Also make sure you have declared your TextView and EditText properly.
Am writing an android code where a text view is being populated by a calculated % inside my java class.
As i had to calulate the %, my final variable (calcPerc) is a double type of variable. It wont setText into my TextView.
Please help.
Code
int totDiff = (totalMA - totalIA);
double beforeperc = ((double)totDiff / (double)totalIA);
calPerc = ((double)beforeperc*100);
percentage = (TextView)findViewById(R.id.populatePercentage);
percentage.setText(calPerc);
The error i am getting is
The method setText(CharSequence) in the type TextView is not applicable for the arguments (double)
Also, one more followup question. The variable "calPerc" as it is double, gives me infinite digits after the decimal. How do i round it off to 2 decimals only?
This should format the value for display (with max 2 decimals) correctly for your current locale;
NumberFormat twoDecimals = NumberFormat.getInstance();
twoDecimals.setMaximumFractionDigits(2);
percentage.setText(twoDecimals.format(calPerc));
To have more control over formatting (like limiting the number of digits after the decimal point), you can use String.format():
percentage.setText(String.format("%.2f %%", calPerc));
But be wary of the default locale.
Here is my formula:
(R.id.dch135-(R.id.drg59*.0752)-(R.id.drg6*.061)-(R.id.drg11*.0412)-(R.id.dplenum*.0802)-(R.id.s2way*4.5)-(R.id.s3way*6.9)-(R.id.sbal35*4.5)-(R.id.sunbal70*8.8)-(R.id.s4way*8.8)-(R.id.s8way*13)-(R.id.sdc6th*3.3)-(R.id.sdc9th*2.7)-(R.id.sdc12th*2)-(R.id.sdc6tp*6)-(R.id.sdc9tp*9)-(R.id.sdc12tp*12)-(R.id.samp*14.5)
I have editText boxes on several layouts. I want to run the formula above on them and get a result to display on a editText box on another layout called results.
I have tried using the getText but all I get back is "Cannot invoke setText(String) on the primitive type int".
Any help would be great!
Thanks
It seems that when you say (R.id.drg59*.0752) you mean "get the value from the EditText with ID drg59 and multiply if by 0.0752. However, what that means in Java is "multiply the value R.id.drg59 (an integer value defined in R.java, not whatever is currently in your edittext) by 0.0752".
You need to parse each of the EditText values to a number (a float, presumably) and THEN multiply it by the 0.0752. For example:
EditText drg59Text = (EditText) findViewById(R.id.drg59);
float drg59Value = Float.parseFloat(drg59Text.getText());
float result = drg59Value * 0.0752;
So on and so forth.
Also, when setting the text value you need to convert the value to a string, the easiest way is generally to just append you value to an empty string:
EditText resultText = ...
resultText.setText("" + result); //where "result" is a float value