I'm trying to restrict what my EditText takes as input. I want it to take a maximum of 21 million (20,999,999.99999999). Is it possible to achieve> if, then how do I go about it? I've tried using InputFilter with a pattern [0-9]{0,9}+((\.[0-9]{0,7})?)||(\.)?. Show me how to do it.
Well, I read Francesco's answer and your comment also. If you need to check the decimal point, you can try this below.
EditText someText = findViewById(R.id.someValueInputHi);
String paw = someText.getText().toString().replaceAll("\\s+", ""); //rem unusual spaces
Double someInput = Double.parseDouble(paw);
if (someInput < 5.3465726543d | someInput > 3.7834657836d) {
//Ok now
} else {
//Oops, no man
}
You only take the value between 5.3465726543 and 3.7834657836.
Why double?
As the name implies, a double has 2x the precision of float. In general, a double has 15 decimal digits of precision, while float has 7.
EDIT:
Francesco's answer is now deleted.
Related
By code, I can make a button that inserts these 3 emojis into the text: ⚽️😈🐺
On many phones when the user clicks the button, though, the problem is that ⚽️😈🐺 displays as [X][X][X]. Or even worse, it displays only three empty spaces.
I would like to disable and hide my own built-in emoji-keypad on Android devices that do not display emojis correctly. Does anyone knows or have a tip on how to detect in code if a device has emoji support?
I have read that emoji is supported from android 4.1, but that is not my experience....
I just implemented a solution for this problem myself. The nice thing with Android is that it is open source so that when you come around problems like these, there's a good chance you can find an approach to help you.
In the Android Open Source Project, you can find a method where they use Paint.hasGlyph to detect whether a font exists for a given emoji. However, as this method is not available before API 23, they also do test renders and compare the result against the width of 'tofu' (the [x] character you mention in your post.)
There are some other failings with this approach, but it should be enough to get you started.
Google source:
https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java#441
https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
Based on Jason Gore answer:
For example create boolean canShowFlagEmoji:
private static boolean canShowFlagEmoji() {
Paint paint = new Paint();
String switzerland = "\uD83C\uDDE8\uD83C\uDDED"; // Here enter Surrogates of Emoji
try {
return paint.hasGlyph(switzerland);
} catch (NoSuchMethodError e) {
// Compare display width of single-codepoint emoji to width of flag emoji to determine
// whether flag is rendered as single glyph or two adjacent regional indicator symbols.
float flagWidth = paint.measureText(switzerland);
float standardWidth = paint.measureText("\uD83D\uDC27"); // U+1F427 Penguin
return flagWidth < standardWidth * 1.25;
// This assumes that a valid glyph for the flag emoji must be less than 1.25 times
// the width of the penguin.
}
}
And then in code whenever when you need to check if emoji is available:
if (canShowFlagEmoji()){
// Code when FlagEmoji is available
} else {
// And when not
}
Surrogates of emoji you can get here, when you click on detail.
An alternative option might be to include the Android "Emoji Compatibility" library, which would detect and add any required Emoji characters to apps running on Android 4.4 (API 19) and later: https://developer.android.com/topic/libraries/support-library/preview/emoji-compat.html
final Paint paint = new Paint();
final boolean isEmojiRendered;
if (VERSION.SDK_INT >= VERSION_CODES.M) {
isEmojiRendered = paint.hasGlyph(emoji);
}
else{
isEmojiRendered = paint.measureText(emoji) > 7;
}
The width > 7 part is particularly hacky, I would expect the value to be 0.0 for non-renderable emoji, but across a few devices, I found that the value actually ranged around 3.0 to 6.0 for non-renderable, and 12.0 to 15.0 for renderable. Your results may vary so you might want to test that. I believe the font size also has an effect on the output of measureText() so keep that in mind.
The second part was answerd by RogueBaneling here how can I check if my device is capable to render Emoji images correctly?
I wish to round up a number in android application with coding as shown below.
float num1= Float.parseFloat(n1.getText().toString());
float num2= Float.parseFloat(n2.getText().toString());
float outc = num1/input;
int final1;
The application getting user input for num1 and num2, and perform to find outc. I wish to rounding up the outc to become an integer final1 where like below:
if outc = 3.8 , final1= 4
if outc = 8.01 , final1= 9
if outc = 12.21 , final1= 13
if outc = 20.45 , final1= 21
*only involve positive value
I try to search for solution, and found that mostly they are normal rounding up solution like this.
It that anyway to perform round up as I mentioned?
Thank you.
Math.ceil()
int result = (int) Math.ceil(outc);
BTW, you can't name variable final, it is reserved word.
Use the ceil function described in:
https://developer.android.com/reference/java/lang/Math.html#ceil(double)
Here is a fragment of my app used to show a graph using achartengine:
private void addSampleData() {
...
DecimalFormat twoDForm = new DecimalFormat("####.##");
for(i=0; i<count; i++){
list[i] = Double.valueOf(twoDForm.format(list[i]));
mCurrentSeries.add(i, list[i]);
}
...
}
When i run my app on the emulator there are no problems (with any device and any api), but when i run the app on my real device i get an error like this: "Inavlid Double: 29,32" and if i see the values stored in list[] they are with "," (29,32 - 34,56 - ...)and not with "." (29.32 - 34.56 - ...) like on the emulator.
Why does this happen? How can I do?
Thx in advance!
You're having this problem probably because of the language of your device, just force the separator to be the coma.
DecimalFormat twoDForm = new DecimalFormat("#.##");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator(',');
twoDForm.setDecimalFormatSymbols(dfs);
it is about the decimal formatting characters.
Set your emulator, windows to be locale hungarian or Romanian, as your device and instead of dots you will have the coma as decimal separator!
There is 2 way to soltve this. One is to force a Locale other is to force a decimal separator only.
I am to lazy to provide full code, I hope it helps the idea and you will get it fast with google.
Hi I am working on an android calculator apps and the now working on the manipuations. I have defined for the following:
ArrayList<Float> inputnum = new ArrayList<Float>();
float inputnum1;
float inputnum2;
and then for the operations,
case MULTIPLY:
inputnum1 = inputnum.get(0);
inputnum2 = inputnum.get(1);
inputnum.add(inputnum1 * inputnum2);
Display.setText(String.format("%.9f", inputnum.get(0)));
similar for the division one.
The muliply function and divide function works well for integers (eg 5* 4 output 20.00000000)
however, when it deals with figures with decimal places, eg 5.3 * 4, it output as 21.12000089, which is incorrect.
what is the problem?
also, how to set output to Display to remove unnecessary zero? eg
when 5*4 it only show 20 instead of 20.000000 as final answer?
when 5.3*4 = 21.12 instead of 21.12000000 as final answer?
Thanks a lot!
Just to change all the related float to double will then avoid presenting the rounding error.
If wanted to present 9 decimal places by filling up zero after the dot, eg 7.56 become 7.560000000, can use the below coding.
Display.setText(String.format("%.9f", inputnum.get(0)));
I am trying to write a very simple Android app.
It takes in 2 floating point numbers, does a calculation,
When I run my app, it crashes.
However, if I enter integers instead of floating point numbers, my
program doesn't crash and does the correct calculation.
How to solve this problem? thx a lot
private Button.OnClickListener btnTranListener =new Button.OnClickListener(){
public void onClick(View v){
int amount=Integer.parseInt(editfc.getText().toString());
double $=1.24*(double)amount;
$ = (double) (((int)($ * 1000)) / 1000.0);
outelc124.setText("ELC(1.24)= " + $ + " /pc");
Replace Integer.parseInt() with Double.parseDouble() and you will be able to handle numbers with decimals or integers.
You can also catch NumberformatException to keep it from crashing if they enter other bogus input.