I have a function that simply converts units, and my goal is for the output number to be displayed in expanded form, as in showing the full number, not "3.0E7". The code is as follows:
public void convertUnits(View view)
{
EditText fromNumberEditText = (EditText)view.findViewById(R.id.fromField);
TextView toNumberEditText = (TextView)view.findViewById(R.id.toField);
String fromUnitString = selectedList[selectedLeftItem];
String toUnitString = selectedList[selectedRightItem];
float fromUnit = Float.parseFloat(fromUnitString.split(",")[2]);
float toUnit = Float.parseFloat(toUnitString.split(",")[2]);
float fromNumber = Float.parseFloat(fromNumberEditText.getText().toString());
float toNumber = (fromNumber * fromUnit) / toUnit;
toNumberEditText.setText(Float.toString(toNumber));
}
The toNumberEditText has marquee, and because of that I would like for it to show the expanded number, i.e. 30000000 not 3.0E7. Whenever the toNumber float is anything more than 7 digits it assumes the "floating e" form. Is there a way to either change the amount of digits before it does this, or remove it altogether?
Use String.format():
toNumberEditText.setText(String.format("%f", toNumber));
Related
i am new to Android Studio . I want to increase or decrease textView's line spacing on click. currently i am using this one and it works but i want when user click + than line spacing increased and decreased for - .
here is my code.
textView.setLineSpacing(0,1.1f);
i have tried this but not works
private int textSpace = (int) 1.0f;
private int diff = (int) 0.1f;
and than this
textSize = textSpace+diff;
textView.setLineSpacing(0,textSize);
same for minus, but its not working , please help
Here is a checked example. I hope everything is well described
// Here put ids of your views
TextView textView = findViewById(R.id.textView);
Button plusButton = findViewById(R.id.plus);
Button minusButton = findViewById(R.id.minus);
// The multiplier may be unchanged
final float multiplier = textView.getLineSpacingMultiplier();
// Set the appropriate offset
final float offset = 1f;
plusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() + offset, multiplier));
minusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() - offset, multiplier));
I suppose you want to achieve something like this:
private int textSize = 20;
button.setOnClickListerner {
textSize += diff;
textView.setLineSpacing(0, textSize);
}
I want to set the minimum fixed width for an EditText so that it can contain its hint but also the typed, length-limited content like a number of 2 digits.
Some details:
I want to be able to do this dynamically since I have numerous
fields for different purposes with different hints (in different languages) and input length (some 2 digits, others 4).
Hints are not necessarily longer than the input itself. A
hint could be "dd" or "Day" and the input could be a to digit
number.
I do not need room for hint and content at the same time;
hints disappear when the user starts typing.
I'm using custom fonts in an extended EditText class, but that should be handled as I'm copying the EditText's Paint.
I have a utility method for doing so, but it returns a width that is too narrow so the hint is clipped. What am I doing wrong?
The EditText is specified in XML like this:
<EditText
android:id="#+id/birthday_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/birthday_month_hint"
android:lines="1"
android:maxLength="2">
In my Activity I first find the EditText and then prepare it using Texts.setNiceAndTightWidth(monthEditText, 2) defined below (including helper methods):
public class Texts
{
public static void setNiceAndTightWidth ( EditText editText, int maxInputLength )
{
// String of chars to test for widest char. Include all possible input chars and chars of hint, as we need to make room for hint as well.
String testChars = String.format("1234568790%s", editText.getHint().toString());
char widestChar = getWidestChar(editText, testChars);
String widestString = repeat(widestChar, maxInputLength);
float widestStringWidth = getTextWidth(editText, widestString);
int width = (int)(widestStringWidth + 0.5f);
editText.setWidth(width);
// This was an experiment but it doesn't help.
ViewGroup.LayoutParams lp = editText.getLayoutParams();
lp.width = width;
editText.setLayoutParams(lp);
}
public static char getWidestChar ( TextView textView, String testChars )
{
float width, widest = 0;
char widestChar = '\0';
// Using Paint properties of TextView, including Fontface, text size, etc.
Paint paint = new Paint( textView.getPaint() );
for ( int i = 0 ; i < testChars.length() ; i++ ) {
width = paint.measureText(testChars, i, i+1);
if ( width > widest ) {
widest = width;
widestChar = testChars.charAt(i);
}
}
return widestChar;
}
public static String repeat ( char ch, int length )
{
char[] chars = new char[length];
Arrays.fill(chars, ch);
String string = String.valueOf(chars);
return string;
}
public static float getTextWidth ( TextView textView, CharSequence text )
{
Paint paint = new Paint( textView.getPaint() );
float width = paint.measureText(text, 0, text.length());
return width;
}
}
I need to calculate how many characters (having pre-defined size eg 20dp) will fit on the text view to divide the long text into different Views? Like any reader app
I'm using the following code that works fine for a single line. My question is how to determine the max number of lines that will fit in text view in various screen sizes?
string abc = "This string is a loooong string";
final float densityMultiplier = getResources().getDisplayMetrics().density;
final float scaledPx = 20 * densityMultiplier;
int numChars;
Paint paint = txtArea.getPaint();
paint.setTextSize(scaledPx);
for (numChars = 1; numChars <= abc.length(); ++numChars)
{
if (paint.measureText(abc, 0, numChars) >= screenWidthDp)
{
break;
}
}
Simply use TextView.getLineHeight() to get height of a line
Here is my button on click method. here a input 3 floating number. after calculation i want to show my result with 3 digit after point.It is shown in 3 text field. how can i do . ?
public void cal(View V){
float dRa,dRb,dRc,z21,z12,z11,z22;
dRa = Float.parseFloat(ra.getText().toString());
dRb = Float.parseFloat(rb.getText().toString());
dRc = Float.parseFloat(rc.getText().toString());
//Result=dRa+dRb+dRc;
z21 = ((dRa*dRb)/(dRa+dRb+dRc));
z11=(dRa*(dRb+dRc)/(dRa+dRb+dRc));
z22=(dRb*(dRa+dRc)/(dRa+dRb+dRc));
Z21.setText("Z21 & Z12 are "+z21);
Z11.setText("Z11' is "+z11);
Z22.setText("Z22' is "+z22);
}
Just did that today with 2 fraction digits. here's how I did it (though I used a double and not a float)
NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(0);
format.setMaximumFractionDigits(3);
Z21.setText(format.format(Z21));
Z11.setText(format.format(Z11));
Z22.setText(format.format(Z22));
Hi i want make so the TextView level can put out decimal but i don'ty know how to do that any one got an idea? NOw it only puts out 1 but i want it to put out 1.80. :)
public class Main extends Activity {
int counter;
EditText weight, hours;
TextView amount, level;
Button calcuate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
weight = (EditText) findViewById(R.id.weight);
hours = (EditText) findViewById(R.id.hours);
amount = (TextView) findViewById(R.id.amount);
level = (TextView) findViewById(R.id.alcohol_level);
calcuate = (Button) findViewById(R.id.calcuate);
final String widmark = getResources().getString(
R.string.widmark);
final String hundra = getResources().getString(
R.string.hundra);
final String cl = getResources().getString(
R.string.cl);
calcuate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Integer wid, mgs;
String w = weight.getText().toString();
String h = hours.getText().toString();
wid = Integer.parseInt(w) * Integer.parseInt(widmark) / Integer.parseInt(hundra);
mgs = Integer.parseInt(cl) / Integer.parseInt(wid.toString()) / Integer.parseInt(hundra);
level.setText(mgs.toString());
}
});
}
}
Your mgs variable is a Integer object. Set it to type float to have decimal places be displayed.
float mgs = Integer.parseInt(cl) / Integer.parseInt(wid.toString()) / Integer.parseInt(hundra);
I hope this helps.
int division will only produce int's. If you want your output to be of float or double type, you must use double or float division.
Double mgs;
mgs = Double.parseDouble(cl) / Double.parseDouble(wid.toString()) / Double.parseDouble(hundra);
Note that not all variables considered in the expression need to be double's, only one of them does.
Another thing that is noteworthy here is whether or not you want two decimal places (assuming a currency here). By default, Java/Android will only spit out as many decimal places as necessary. 1.80 will display as 1.8. To alleviate this, you should use a NumberFormat (specifically, use NumberFormat.getCurrencyInstance()) so that you can specify that you want the number of decimals for your default Locale's currency.