Spanned text in Number picker - android

I have a problem with to show meter m2 in android. I can use SpannedBuilderString for setText in TextView and it work.
The problem is I want to show m2 in Number Picker like 50 m2 100 m2 but Number Picker only show String and I can't. Please help me fix that. Tks everyone.

Using Unicode Character makes it very easy :
First create an array with your values(this will go to the number picker)
String mValues[] = { "100 " + "\u33A1", "200 " + "\u33A1" };
Now use this method to create number picker with custom values:
private void setNubmerPicker(NumberPicker nubmerPicker,String [] numbers ){
nubmerPicker.setMaxValue(numbers.length-1);
nubmerPicker.setMinValue(0);
nubmerPicker.setWrapSelectorWheel(true);
nubmerPicker.setDisplayedValues(numbers);
}
And for the final step call this method:
setNubmerPicker(yourNumberPicker,mValues);

Apply this custom Formatter to your NumberPicker:
NumberPicker.Formatter formatter = new NumberPicker.Formatter(){
#Override
public String format(int i) {
return String.valueOf(i) + " " + Character.toString((char) 0x33A1);
}
};
numberPicker.setFormatter(formatter);

Related

how to remove special character from string except + in android?

I am fetching number from contact book and sending it to server. i get number like this (+91)942 80-60 135 but i want result like this +9428060135.+ must be first character of string number.
Given your example you want to replace the prefix with a single + character. You also want to remove other non-numeric characters from the number string. Here's how you can do that:
String number = "(+91)942 80-60 135";
number = "+" + number.replaceAll("\\(\\+\\d+\\)|[^\\d]", "");
The regex matches any prefix (left paren followed by a + followed by one or more digits, followed by a right paren) or any non digit character, and removes them. This is concatenated to a leading + as required. This code will also handle + characters within the number string, e.g. +9428060135+++ and +(+91)9428060135+++.
If you simply wanted to remove any character that is not a digit nor a +, the code would be:
String number = "(+91)942 80-60 135";
number = number.replaceAll("[^\\d+]", "");
but be aware that this will retain the digits in the prefix, which is not the same as your example.
You can use String.replace(oldChar, newChar). Use the code below
String phone = "(+91)942 80-60 135"; // fetched string
String trimmedPhone = phone.replace("(","").replace(")","").replace("-","").trim();
I hope it will work for you.
check this. Pass your string to this function or use as per code goes
String inputString = "(+91)942 80-60 135";
public void removeSpecialCharacter(String inputString) {
String replaced = inputString.replaceAll("[(\\-)]", "");
String finalString = replaced.replaceAll(" ", "");
Log.e("String Output", " " + replaced + " " + second);
}

String to display number currency in Android

I have a string
String retail = c.getString(c.getColumnIndex("retail"));
The date is being passed as "99999", I need it to print out as "999.99", how can I do this?
If you always have to add the "." 2 character before the end, this should work:
retail = retail.substring(0, retail.length()-2) + "." + retail.substring(retail.length()-2,retail.length());
This will add a dot two character before the end of the String, as you need.

How to split json response string based on the whitespace between them in android

I have a json response string like this: 2015-09-30 11:09:00 (date and time is a single string)
I need to add "at" between "2015-09-30" and "11:09:00". How can I split this date and time so that I can add the word "at" in between them.
My requirement::I displayed the date and time in a single textview(without splitting them). But now when I split this, how can I display them on two different text view,i.e; date in first textview and time in second text view.Please help me to sort this problem.
String jsonString = "2015-09-30 11:09:00";
String dateTime[]=jsonString.split(" ");
String finalStr = dateTime[1]+" at "+dateTime[0];
i guess this should work
or String finalStr = jsonString.replace(" "," at ");
Use replaceFirst
string.replaceFirst("[ \\t]+", " at ");
You can use split() for that :
String[] dateTimeArray = dateTimeString.split(" ");
String date = dateTimeArray[0]; // date
String time = dateTimeArray[1]; // time
Edit
set it to two different textviews:
tvFirstTextView.setText(date);
tvSecondTextView.setText("at " +time);
String [] DateTime = ANY_JSON_STRING.split(" ");
String Date = DateTime[0];
String Time = DateTime[1];
Now you can play with String as per your requirement i.e. Date + " at " + Times, or anything else like that.

how to add zero on multiple places in string android

I want to show number in two digits format anyone please tell me how i can do this using string format
i am doing this but this give me error
String formatted = String.format("%02d", list.get(i).toString());
Use Integer.parseInt():
String formatted = String.format("%02d", Integer.parseInt(list.get(i).toString()));
Your second argument to String.format should be an integer here. Try
String formatted = String.format("%02d", list.get(i));
(Assuming you have a list of integers).

Formate String after dot "." from a string in java-Android

I have a string like 46542.5435657468, but i want format this string and need only two charector after dot "." like 46542.54. Please suggest me which String method i need to use.
String.format("%.2f", Double.valueOf("46542.5435657468"));
maybe String.format()?
String.format("%.2f", floatValue);
You can use DecimalFormat
first declare this at the top
DecimalFormat dtime = new DecimalFormat("#.##"); //change .## for whatever numbers after decimal you may like.
then use it like this
dtime.format(your string);
like:
String a = "46542.5435657468";
dtime.format(a);
output will be 46542.54
You can use a method like this.
private static String extract(String text) {
String[] values = text.split(".");
return values[0] + "." + values[1].substring(0, 2);
}
The method indexOf tell you the position of the character "." ok?
The method substring cut a peace of the string from the begining (value 0) until the positio of the character "." plus 2 digits more.
public String getNumberFormated(String yourNumber)
{
return yourNumber.substring(0, yourNumber.indexOf(".") + 2);
}
Do you like my solution?

Categories

Resources