I'm trying to dynamically format the value entered by the user on a TextView, specifically what I am trying to do is:
1. TextView text = ""
2. User entered = "4"
3. TextView text = 0.04
4. User entered = "5"
5. TextView text = 0.45
6. User entered = "6"
7. TextView text = 4.56
and so on...
I'm using the onTextChangedmethod stub from TextWatcher, although I don't know how to implement this logic.
What is the simplest way to implement this feature?
You have to use NumberFormat in this case, e.g:
double moneyCurrency = 100.1;
NumberFormat baseFormat = NumberFormat.getCurrencyInstance();
String moneyString = baseFormat.format(moneyCurrency);
Related
I need to receive user's input from an edittext, then add some elements inside it?
how can I do it programmatically?
let me explain:
user enters this phrase : ( Hello World )
I want my app change it to : ( Hoelplom wwoerlldc) for example
Any suggestion?
get the text from your edittext
String userInput = myEditText.getText().toString();
and then modify it as you want
To 'Modify' the String you can do it in many ways
You can use substrings , replace() , ... etc
also you can convert it to char array and modify it , then build your string again
String userInput = myEditText.getText().toString();
char[] myArray = userInput.toCharArray();
// your modification(s) here
userInput = new String(myArray);
In my android application I am using spinner for selecting data.. and I created string array for strings that to be displayed in spinner. I put all the details in strings folder. I wanted the selected text t be displayed in edit text once the user selected item..
For example : spinner is used to select country codes suppose user selected USA
then the selected text will be like this
United States of America,+001
I don't need t take all the text and display it in edit text. I need only the text after comma, that is +001. So is there any way to get the text after the comma only
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
I know this will display all text I want only text that dislpaying after comma
You can split your text on the comma:
String text = spinner.getSelectedItem().toString();
String[] splited_text = text.split(",");
text = splited_text[1];
Suppose the text is in a string name text. use this:
String[] temp = text.split(",")
String code = temp[1]; //+001 the code after , temp[0] contains the rest
String seperated[] = spinner.getSelectedItem().toString().split(",");
text = seperated[1];
This will return only "+001".
String code = text.substring(text.indexOf(','));
This is how to get string after last comma:
String founded;
Pattern p = Pattern.compile(".*,\\s*(.*)");
String dd = (String) "Your string, really, really2";
Matcher m = p.matcher(dd);
if (m.find()) {
founded = m.group(1); //returns "really2"
}
In my android app, I have an EditText in which the user enters a decimal which can be as long as the user wants. It can be a number like 25, 54.77, 23.7, 7.88, etc. In the same activity, I have a textView which reads the input into the EditText and displays the decimal but only till the first decimal digit. For e.g. if the user enters: 25 should be displayed as 25, 54.77 as 54.7, 23.7 as 23.7, 7.88 as 7.8. How can I achieve this? I tried using the following code but it didn't work:
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(0);
df.setMaximumFractionDigits(1);
df.format(EditTextNumber);
Use String.format:
String result = String.format("%.1f", yourdecimalvalue);
you can use DecimalFormat too:
NumberFormat formatter = new DecimalFormat("#0.0");
String result = formatter.format(yourdeciamlvalue);
Try NumberFormat instead of DecimalFormat:
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);
TextView.setText(nf.format(editText.getText()));
if you are grbbing the input via an edittext then u can use this code
String number=input.getText().toString(); //input is ur input edittext
String output=number;
int indexOfDecimal=number.lastIndexOf('.');
if(indexOfDecimal!=-1&&indexOfDecimal<output.length()-1)
number=output.substring(0, indexOfDecimal+2); // use number as the output
I need to print the below string which is in Arabic on TextView in Android. It is printing good but when the Arabic text and digits falls in a same string, Android put the digit at end!
here is my code
String str = "مقر 44";
TextView textView = (TextView)findViewById(R.id.test);
textView.setText(str);
Here is the output
you can try with this workaround
String str = "44 "+"مقر ";
TextView textView = (TextView)findViewById(R.id.txt_title);
textView.setText(str);
its not Android who put digit at the end, its because of Arabic writing standard
In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.