android: how to persistently store a Spanned? - android

I want to save a Spanned object persistently. (I'm saving the String it's based on persistently now, but it takes over 1 second to run Html.fromHtml() on it, noticeably slowing the UI.)
I see things like ParcelableSpan and SpannedString and SpannableString but I'm not sure which to use.

Right now, Html.toHtml() is your only built-in option. Parcelable is used for inter-process communication and is not designed to be durable. If toHtml() does not cover all the particular types of spans that you are using, you will have to cook up your own serialization mechanism.
Since saving the object involves disk I/O, you should be doing that in a background thread anyway, regardless of the speed of toHtml().

I had a similar problem; I used a SpannableStringBuilder to hold a string and a bunch of spans, and I wanted to be able to save and restore this object. I wrote this code to accomplish this manually using SharedPreferences:
// Save Log
SpannableStringBuilder logText = log.getText();
editor.putString(SAVE_LOG, logText.toString());
ForegroundColorSpan[] spans = logText
.getSpans(0, logText.length(), ForegroundColorSpan.class);
editor.putInt(SAVE_LOG_SPANS, spans.length);
for (int i = 0; i < spans.length; i++){
int col = spans[i].getForegroundColor();
int start = logText.getSpanStart(spans[i]);
int end = logText.getSpanEnd(spans[i]);
editor.putInt(SAVE_LOG_SPAN_COLOUR + i, col);
editor.putInt(SAVE_LOG_SPAN_START + i, start);
editor.putInt(SAVE_LOG_SPAN_END + i, end);
}
// Load Log
String logText = save.getString(SAVE_LOG, "");
log.setText(logText);
int numSpans = save.getInt(SAVE_LOG_SPANS, 0);
for (int i = 0; i < numSpans; i++){
int col = save.getInt(SAVE_LOG_SPAN_COLOUR + i, 0);
int start = save.getInt(SAVE_LOG_SPAN_START + i, 0);
int end = save.getInt(SAVE_LOG_SPAN_END + i, 0);
log.getText().setSpan(new ForegroundColorSpan(col), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
I my case I knew that all the spans were of type ForegroundColorSpan and with flags SPAN_EXCLUSIVE_EXCLUSIVE, but this code can be easily adapted to accomodate other types.

My use case was about putting a Spanned into a Bundle, and Google brought me here. #CommonsWare is right that Parcelable is no good for persistent storage, but it's fine for storing into a Bundle. Most spans seems to extend ParcelableSpan, and so this worked for me in onSaveInstanceState:
ParcelableSpan spanObjects[] = mStringBuilder.getSpans(0, mStringBuilder.length(), ParcelableSpan.class);
int spanStart[] = new int[spanObjects.length];
int spanEnd[] = new int[spanObjects.length];
int spanFlags[] = new int[spanObjects.length];
for(int i = 0; i < spanObjects.length; ++i)
{
spanStart[i] = mStringBuilder.getSpanStart(spanObjects[i]);
spanEnd[i] = mStringBuilder.getSpanEnd(spanObjects[i]);
spanFlags[i] = mStringBuilder.getSpanFlags(spanObjects[i]);
}
outState.putString("mStringBuilder:string", mStringBuilder.toString());
outState.putParcelableArray("mStringBuilder:spanObjects", spanObjects);
outState.putIntArray("mStringBuilder:spanStart", spanStart);
outState.putIntArray("mStringBuilder:spanEnd", spanEnd);
outState.putIntArray("mStringBuilder:spanFlags", spanFlags);
Then the state can be restored with something like this:
mStringBuilder = new SpannableStringBuilder(savedInstanceState.getString("mStringBuilder:string"));
ParcelableSpan spanObjects[] = (ParcelableSpan[])savedInstanceState.getParcelableArray("mStringBuilder:spanObjects");
int spanStart[] = savedInstanceState.getIntArray("mStringBuilder:spanStart");
int spanEnd[] = savedInstanceState.getIntArray("mStringBuilder:spanEnd");
int spanFlags[] = savedInstanceState.getIntArray("mStringBuilder:spanFlags");
for(int i = 0; i < spanObjects.length; ++i)
mStringBuilder.setSpan(spanObjects[i], spanStart[i], spanEnd[i], spanFlags[i]);
I've used a SpannableStringBuilder here but it should work with any class implementing Spanned as far as I can tell. It's probably possible to wrap this code into a ParcelableSpanned, but this version seems fine for now.

From Dan's idea:
public static String spannableString2JsonString(SpannableString ss) throws JSONException {
JSONObject json = new JSONObject();
json.put("text",ss.toString());
JSONArray ja = new JSONArray();
ForegroundColorSpan[] spans = ss.getSpans(0, ss.length(), ForegroundColorSpan.class);
for (int i = 0; i < spans.length; i++){
int col = spans[i].getForegroundColor();
int start = ss.getSpanStart(spans[i]);
int end = ss.getSpanEnd(spans[i]);
JSONObject ij = new JSONObject();
ij.put("color",col);
ij.put("start",start);
ij.put("end",end);
ja.put(ij);
}
json.put("spans",ja);
return json.toString();
}
public static SpannableString jsonString2SpannableString(String strjson) throws JSONException{
JSONObject json = new JSONObject(strjson);
SpannableString ss = new SpannableString(json.getString("text"));
JSONArray ja = json.getJSONArray("spans");
for (int i=0;i<ja.length();i++){
JSONObject jo = ja.getJSONObject(i);
int col = jo.getInt("color");
int start = jo.getInt("start");
int end = jo.getInt("end");
ss.setSpan(new ForegroundColorSpan(col),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return ss;
}

A solution I came up with is using GSON with a custom serializer/deserializer. The solution combines some of the ideas mentioned in other answers.
Define some JSON Keys
/* JSON Property Keys */
private static final String PREFIX = "SpannableStringBuilder:";
private static final String PROP_INPUT_STRING = PREFIX + "string";
private static final String PROP_SPAN_OBJECTS= PREFIX + "spanObjects";
private static final String PROP_SPAN_START= PREFIX + "spanStart";
private static final String PROP_SPAN_END = PREFIX + "spanEnd";
private static final String PROP_SPAN_FLAGS = PREFIX + "spanFlags";
Gson Serializer
public static class SpannableSerializer implements JsonSerializer<SpannableStringBuilder> {
#Override
public JsonElement serialize(SpannableStringBuilder spannableStringBuilder, Type type, JsonSerializationContext context) {
ParcelableSpan[] spanObjects = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), ParcelableSpan.class);
int[] spanStart = new int[spanObjects.length];
int[] spanEnd= new int[spanObjects.length];
int[] spanFlags = new int[spanObjects.length];
for(int i = 0; i < spanObjects.length; ++i) {
spanStart[i] = spannableStringBuilder.getSpanStart(spanObjects[i]);
spanEnd[i] = spannableStringBuilder.getSpanEnd(spanObjects[i]);
spanFlags[i] = spannableStringBuilder.getSpanFlags(spanObjects[i]);
}
JsonObject jsonSpannable = new JsonObject();
jsonSpannable.addProperty(PROP_INPUT_STRING, spannableStringBuilder.toString());
jsonSpannable.addProperty(PROP_SPAN_OBJECTS, gson.toJson(spanObjects));
jsonSpannable.addProperty(PROP_SPAN_START, gson.toJson(spanStart));
jsonSpannable.addProperty(PROP_SPAN_END, gson.toJson(spanEnd));
jsonSpannable.addProperty(PROP_SPAN_FLAGS, gson.toJson(spanFlags));
return jsonSpannable;
}
}
Gson Deserializer
public static class SpannableDeserializer implements JsonDeserializer<SpannableStringBuilder> {
#Override
public SpannableStringBuilder deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jsonSpannable = jsonElement.getAsJsonObject();
try {
String spannableString = jsonSpannable.get(PROP_INPUT_STRING).getAsString();
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(spannableString);
String spanObjectJson = jsonSpannable.get(PROP_SPAN_OBJECTS).getAsString();
ParcelableSpan[] spanObjects = gson.fromJson(spanObjectJson, ParcelableSpan[].class);
String spanStartJson = jsonSpannable.get(PROP_SPAN_START).getAsString();
int[] spanStart = gson.fromJson(spanStartJson, int[].class);
String spanEndJson = jsonSpannable.get(PROP_SPAN_END).getAsString();
int[] spanEnd = gson.fromJson(spanEndJson, int[].class);
String spanFlagsJson = jsonSpannable.get(PROP_SPAN_FLAGS).getAsString();
int[] spanFlags = gson.fromJson(spanFlagsJson, int[].class);
for (int i = 0; i <spanObjects.length; ++i) {
spannableStringBuilder.setSpan(spanObjects[i], spanStart[i], spanEnd[i], spanFlags[i]);
}
return spannableStringBuilder;
} catch (Exception ex) {
Log.e(TAG, Log.getStackTraceString(ex));
}
return null;
}
}
For ParcelableSpan you might need to register the types to GSON like so:
RuntimeTypeAdapterFactory
.of(ParcelableSpan.class)
.registerSubtype(ForegroundColorSpan.class);
.registerSubtype(StyleSpan.class); //etc.

My use case was converting a TextView's contents, including color and style, to/from a hex string. Building off Dan's answer, I came up with the following code. Hopefully if someone has a similar use case, it'll save you some headache.
Store textBox's contents to string:
String actualText = textBox.getText().toString();
SpannableString spanStr = new SpannableString(textBox.getText());
ForegroundColorSpan[] fSpans = spanStr.getSpans(0,spanStr.length(),ForegroundColorSpan.class);
StyleSpan[] sSpans = spanStr.getSpans(0,spanStr.length(),StyleSpan.class);
int nSpans = fSpans.length;
String spanInfo = "";
String headerInfo = String.format("%08X",nSpans);
for (int i = 0; i < nSpans; i++) {
spanInfo += String.format("%08X",fSpans[i].getForegroundColor());
spanInfo += String.format("%08X",spanStr.getSpanStart(fSpans[i]));
spanInfo += String.format("%08X",spanStr.getSpanEnd(fSpans[i]));
}
nSpans = sSpans.length;
headerInfo += String.format("%08X",nSpans);
for (int i = 0; i < nSpans; i++) {
spanInfo += String.format("%08X",sSpans[i].getStyle());
spanInfo += String.format("%08X",spanStr.getSpanStart(sSpans[i]));
spanInfo += String.format("%08X",spanStr.getSpanEnd(sSpans[i]));
}
headerInfo += spanInfo;
headerInfo += actualText;
return headerInfo;
Retrieve textBox's contents from string:
String header = tvString.substring(0,8);
int fSpans = Integer.parseInt(header,16);
header = tvString.substring(8,16);
int sSpans = Integer.parseInt(header,16);
int nSpans = fSpans + sSpans;
SpannableString tvText = new SpannableString(tvString.substring(nSpans*24+16));
tvString = tvString.substring(16,nSpans*24+16);
int cc, ss, ee;
int begin;
for (int i = 0; i < fSpans; i++) {
begin = i*24;
cc = (int) Long.parseLong(tvString.substring(begin,begin+8),16);
ss = (int) Long.parseLong(tvString.substring(begin+8,begin+16),16);
ee = (int) Long.parseLong(tvString.substring(begin+16,begin+24),16);
tvText.setSpan(new ForegroundColorSpan(cc), ss, ee, 0);
}
for (int i = 0; i < sSpans; i++) {
begin = i*24+fSpans*24;
cc = (int) Long.parseLong(tvString.substring(begin,begin+8),16);
ss = (int) Long.parseLong(tvString.substring(begin+8,begin+16),16);
ee = (int) Long.parseLong(tvString.substring(begin+16,begin+24),16);
tvText.setSpan(new StyleSpan(cc), ss, ee, 0);
}
textBox.setText(tvText);
The reason for the (int) Long.parseLong in the retrieval code is because the style/color can be negative numbers. This trips up parseInt and results in an overflow error. But, doing parseLong and then casting to int gives the correct (positive or negative) integer.

This problem is interesting, because you have to save all the information you want from the SpannableString or SpannableStringBuilder, Gson doesn't pick them automatically. Using HTML didn't work properly for my implementation, so here's another working solution. All answers here are incomplete, you have to do something like this:
class SpannableSerializer : JsonSerializer<SpannableStringBuilder?>, JsonDeserializer<SpannableStringBuilder?> {
private val gson: Gson
get() {
val rtaf = RuntimeTypeAdapterFactory
.of(ParcelableSpan::class.java, ParcelableSpan::class.java.simpleName)
.registerSubtype(ForegroundColorSpan::class.java, ForegroundColorSpan::class.java.simpleName)
.registerSubtype(StyleSpan::class.java, StyleSpan::class.java.simpleName)
.registerSubtype(RelativeSizeSpan::class.java, RelativeSizeSpan::class.java.simpleName)
.registerSubtype(SuperscriptSpan::class.java, SuperscriptSpan::class.java.simpleName)
.registerSubtype(UnderlineSpan::class.java, UnderlineSpan::class.java.simpleName)
return GsonBuilder()
.registerTypeAdapterFactory(rtaf)
.create()
}
override fun serialize(spannableStringBuilder: SpannableStringBuilder?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
val spanTypes = spannableStringBuilder?.getSpans(0, spannableStringBuilder.length, ParcelableSpan::class.java)
val spanStart = IntArray(spanTypes?.size ?: 0)
val spanEnd = IntArray(spanTypes?.size ?: 0)
val spanFlags = IntArray(spanTypes?.size ?: 0)
val spanInfo = DoubleArray(spanTypes?.size ?: 0)
spanTypes?.forEachIndexed { i, span ->
when (span) {
is ForegroundColorSpan -> spanInfo[i] = span.foregroundColor.toDouble()
is StyleSpan -> spanInfo[i] = span.style.toDouble()
is RelativeSizeSpan -> spanInfo[i] = span.sizeChange.toDouble()
}
spanStart[i] = spannableStringBuilder.getSpanStart(span)
spanEnd[i] = spannableStringBuilder.getSpanEnd(span)
spanFlags[i] = spannableStringBuilder.getSpanFlags(span)
}
val jsonSpannable = JsonObject()
jsonSpannable.addProperty(INPUT_STRING, spannableStringBuilder.toString())
jsonSpannable.addProperty(SPAN_TYPES, gson.toJson(spanTypes))
jsonSpannable.addProperty(SPAN_START, gson.toJson(spanStart))
jsonSpannable.addProperty(SPAN_END, gson.toJson(spanEnd))
jsonSpannable.addProperty(SPAN_FLAGS, gson.toJson(spanFlags))
jsonSpannable.addProperty(SPAN_INFO, gson.toJson(spanInfo))
return jsonSpannable
}
override fun deserialize(jsonElement: JsonElement, type: Type, jsonDeserializationContext: JsonDeserializationContext): SpannableStringBuilder {
val jsonSpannable = jsonElement.asJsonObject
val spannableString = jsonSpannable[INPUT_STRING].asString
val spannableStringBuilder = SpannableStringBuilder(spannableString)
val spanObjectJson = jsonSpannable[SPAN_TYPES].asString
val spanTypes: Array<ParcelableSpan> = gson.fromJson(spanObjectJson, Array<ParcelableSpan>::class.java)
val spanStartJson = jsonSpannable[SPAN_START].asString
val spanStart: IntArray = gson.fromJson(spanStartJson, IntArray::class.java)
val spanEndJson = jsonSpannable[SPAN_END].asString
val spanEnd: IntArray = gson.fromJson(spanEndJson, IntArray::class.java)
val spanFlagsJson = jsonSpannable[SPAN_FLAGS].asString
val spanFlags: IntArray = gson.fromJson(spanFlagsJson, IntArray::class.java)
val spanInfoJson = jsonSpannable[SPAN_INFO].asString
val spanInfo: DoubleArray = gson.fromJson(spanInfoJson, DoubleArray::class.java)
for (i in spanTypes.indices) {
when (spanTypes[i]) {
is ForegroundColorSpan -> spannableStringBuilder.setSpan(ForegroundColorSpan(spanInfo[i].toInt()), spanStart[i], spanEnd[i], spanFlags[i])
is StyleSpan -> spannableStringBuilder.setSpan(StyleSpan(spanInfo[i].toInt()), spanStart[i], spanEnd[i], spanFlags[i])
is RelativeSizeSpan -> spannableStringBuilder.setSpan(RelativeSizeSpan(spanInfo[i].toFloat()), spanStart[i], spanEnd[i], spanFlags[i])
else -> spannableStringBuilder.setSpan(spanTypes[i], spanStart[i], spanEnd[i], spanFlags[i])
}
}
return spannableStringBuilder
}
companion object {
private const val PREFIX = "SSB:"
private const val INPUT_STRING = PREFIX + "string"
private const val SPAN_TYPES = PREFIX + "spanTypes"
private const val SPAN_START = PREFIX + "spanStart"
private const val SPAN_END = PREFIX + "spanEnd"
private const val SPAN_FLAGS = PREFIX + "spanFlags"
private const val SPAN_INFO = PREFIX + "spanInfo"
}
}
If there are other types of spans you have to add them in the when sections and pick the associated information of the span, it's easy to add them all.
RuntimeTypeAdapterFactory is private in the the gson library, you have to copy it to your project.
https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java
now use it!
val gson by lazy {
val type: Type = object : TypeToken<SpannableStringBuilder>() {}.type
GsonBuilder()
.registerTypeAdapter(type, SpannableSerializer())
.create()
}
val ssb = gson.fromJson("your json here", SpannableStringBuilder::class.java)

Related

How can I convert numbers to currency format in android

I want to show my numbers in money format and separate digits like the example below:
1000 -----> 1,000
10000 -----> 10,000
100000 -----> 100,000
1000000 -----> 1,000,000
Thanks
Another approach :
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMaximumFractionDigits(0);
format.setCurrency(Currency.getInstance("EUR"));
format.format(1000000);
This way, it's displaying 1 000 000 € or 1,000,000 €, depending on device currency's display settings
You need to use a number formatter, like so:
NumberFormat formatter = new DecimalFormat("#,###");
double myNumber = 1000000;
String formattedNumber = formatter.format(myNumber);
//formattedNumber is equal to 1,000,000
Hope this helps!
double number = 1000000000.0;
String COUNTRY = "US";
String LANGUAGE = "en";
String str = NumberFormat.getCurrencyInstance(new Locale(LANGUAGE, COUNTRY)).format(number);
//str = $1,000,000,000.00
Currency formatter.
public static String currencyFormat(String amount) {
DecimalFormat formatter = new DecimalFormat("###,###,##0.00");
return formatter.format(Double.parseDouble(amount));
}
Use this:
int number = 1000000000;
String str = NumberFormat.getNumberInstance(Locale.US).format(number);
//str = 1,000,000,000
This Method gives you the exact output which you need:
public String currencyFormatter(String num) {
double m = Double.parseDouble(num);
DecimalFormat formatter = new DecimalFormat("###,###,###");
return formatter.format(m);
}
Try the following solution:
NumberFormat format = NumberFormat.getCurrencyInstance();
((TextView)findViewById(R.id.text_result)).setText(format.format(result));
The class will return a formatter for the device default currency.
You can refer to this link for more information:
https://developer.android.com/reference/java/text/NumberFormat.html
Here's a kotlin Extension that converts a Double to a Currency(Nigerian Naira)
fun Double.toRidePrice():String{
val format: NumberFormat = NumberFormat.getCurrencyInstance()
format.maximumFractionDigits = 0
format.currency = Currency.getInstance("NGN")
return format.format(this.roundToInt())
}
Use a Formatter class
For eg:
String s = (String.format("%,d", 1000000)).replace(',', ' ');
Look into:
http://developer.android.com/reference/java/util/Formatter.html
The way that I do this in our app is this:
amount.addTextChangedListener(new CurrencyTextWatcher(amount));
And the CurrencyTextWatcher is this:
public class CurrencyTextWatcher implements TextWatcher {
private EditText ed;
private String lastText;
private boolean bDel = false;
private boolean bInsert = false;
private int pos;
public CurrencyTextWatcher(EditText ed) {
this.ed = ed;
}
public static String getStringWithSeparator(long value) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
String f = formatter.format(value);
return f;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
bDel = false;
bInsert = false;
if (before == 1 && count == 0) {
bDel = true;
pos = start;
} else if (before == 0 && count == 1) {
bInsert = true;
pos = start;
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
lastText = s.toString();
}
#Override
public void afterTextChanged(Editable s) {
ed.removeTextChangedListener(this);
StringBuilder sb = new StringBuilder();
String text = s.toString();
for (int i = 0; i < text.length(); i++) {
if ((text.charAt(i) >= 0x30 && text.charAt(i) <= 0x39) || text.charAt(i) == '.' || text.charAt(i) == ',')
sb.append(text.charAt(i));
}
if (!sb.toString().equals(s.toString())) {
bDel = bInsert = false;
}
String newText = getFormattedString(sb.toString());
s.clear();
s.append(newText);
ed.addTextChangedListener(this);
if (bDel) {
int idx = pos;
if (lastText.length() - 1 > newText.length())
idx--; // if one , is removed
if (idx < 0)
idx = 0;
ed.setSelection(idx);
} else if (bInsert) {
int idx = pos + 1;
if (lastText.length() + 1 < newText.length())
idx++; // if one , is added
if (idx > newText.length())
idx = newText.length();
ed.setSelection(idx);
}
}
private String getFormattedString(String text) {
String res = "";
try {
String temp = text.replace(",", "");
long part1;
String part2 = "";
int dotIndex = temp.indexOf(".");
if (dotIndex >= 0) {
part1 = Long.parseLong(temp.substring(0, dotIndex));
if (dotIndex + 1 <= temp.length()) {
part2 = temp.substring(dotIndex + 1).trim().replace(".", "").replace(",", "");
}
} else
part1 = Long.parseLong(temp);
res = getStringWithSeparator(part1);
if (part2.length() > 0)
res += "." + part2;
else if (dotIndex >= 0)
res += ".";
} catch (Exception ex) {
ex.printStackTrace();
}
return res;
}
Now if you add this watcher to your EditText, as soon as user enter his number, the watcher decides whether it needs separator or not.
i used this code for my project and it works:
EditText edt_account_amount = findViewById(R.id.edt_account_amount);
edt_account_amount.addTextChangedListener(new DigitFormatWatcher(edt_account_amount));
and defined class:
public class NDigitCardFormatWatcher implements TextWatcher {
EditText et_filed;
String processed = "";
public NDigitCardFormatWatcher(EditText et_filed) {
this.et_filed = et_filed;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable editable) {
String initial = editable.toString();
if (et_filed == null) return;
if (initial.isEmpty()) return;
String cleanString = initial.replace(",", "");
NumberFormat formatter = new DecimalFormat("#,###");
double myNumber = new Double(cleanString);
processed = formatter.format(myNumber);
//Remove the listener
et_filed.removeTextChangedListener(this);
//Assign processed text
et_filed.setText(processed);
try {
et_filed.setSelection(processed.length());
} catch (Exception e) {
// TODO: handle exception
}
//Give back the listener
et_filed.addTextChangedListener(this);
}
}
Updated 2022 answer
Try this snippet. It formats a number in string complete with the currency & setting fractional digits.
Upvote if this helped you! :)
/**
* Formats amount in string to human-readable amount (separated with commas
* & prepends currency symbol)
*
* #param amount The amount to format in String
* #return The formatted amount complete with separators & currency symbol added
*/
public static String formatCurrency(String amount) {
String formattedAmount = amount;
try {
if (amount == null || amount.isEmpty())
throw new Exception("Amount is null/empty");
Double amountInDouble = Double.parseDouble(amount);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
formattedAmount = numberFormat.format(amountInDouble);
} catch (Exception exception) {
exception.printStackTrace();
return formattedAmount;
}
return formattedAmount;
}
private val currencyFormatter = NumberFormat.getCurrencyInstance(LOCALE_AUS).configure()
private fun NumberFormat.configure() = apply {
maximumFractionDigits = 2
minimumFractionDigits = 2
}
fun Number.asCurrency(): String {
return currencyFormatter.format(this)
}
And then just use as
val x = 100000.234
x.asCurrency()
If you have the value stored in a String like me, which was coming from the server like "$20000.00".
You can do something like this in Kotlin (JetpackCompose):
#Composable
fun PrizeAmount(
modifier: Modifier = Modifier,
prize: String,
)
{
val currencyFormat = NumberFormat.getCurrencyInstance(Locale("en", "US"))
val text = currencyFormat.format(prize.substringAfter("$").toDouble())
...
}
Output: "$20,000.00"
NumberFormat.getCurrencyInstance(Locale("ES", "es")).format(number)
here is a kotlin version to Format Currency, here i'm getting an argument from another fragment from an input Field then it will be set in the textView in the main Fragment
fun formatArgumentCurrency(argument : String, textView: TextView) {
val valueText = requireArguments().get(argument).toString()
val dec = DecimalFormat("#,###.##")
val number = java.lang.Double.valueOf(valueText)
val value = dec.format(number)
val currency = Currency.getInstance("USD")
val symbol = currency.symbol
textView.text = String.format("$symbol$value","%.2f" )
}
You can easily achieve this with this small simple library.
https://github.com/jpvs0101/Currencyfy
Just pass any number, then it will return formatted string, just like that.
currencyfy (500000.78); // $ 500,000.78 //default
currencyfy (500000.78, false); // $ 500,001 // hide fraction (will round off automatically!)
currencyfy (500000.78, false, false); // 500,001 // hide fraction & currency symbol
currencyfy (new Locale("en", "in"), 500000.78); // ₹ 5,00,000.78 // custom locale
It compatible with all versions of Android including older versions!

Android random string generator

I have a problem.
I want to draw a random String something like this aXcFg3s2.
What i doing bad ?
How change my random()
private String random;
private String charsEntered;
private EditText et;
private Button ok;
CaptchaInterface.OnCorrectListener mCorrectListener;
public void setOnCorrectListener(CaptchaInterface.OnCorrectListener listener) {
mCorrectListener = listener;
}
public TextCaptcha(Context context) {
super(context);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
public static String random() {
Random generator = new Random();
String x = (String) (generator.nextInt(96) + 32);
return x;
}
public void onCreate(Bundle icicle) {
setContentView(R.layout.main);
random = random();
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Random Number: " + random); // Show the random number
et = (EditText) findViewById(R.id.etNumbers);
ok = (Button) findViewById(R.id.button1);
ok.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
charsEntered = et.getText().toString();
} catch (NumberFormatException nfe) {
Toast.makeText(et.getContext(), "Bla bla bla",
Toast.LENGTH_LONG).show();
}
if (random == charsEntered) {
Toast.makeText(et.getContext(), "Good!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(et.getContext(), "Bad!", Toast.LENGTH_LONG).show();
}
}
the problem is that you've handled only a single character instead of using a loop.
you can create an array of characters which has all of the characters that you wish to allow to be in the random string , then in a loop take a random position from the array and add append it to a stringBuilder . in the end , convert the stringBuilder to a string.
EDIT:
here's the simple algorithm i've suggested:
private static final String ALLOWED_CHARACTERS ="0123456789qwertyuiopasdfghjklzxcvbnm";
private static String getRandomString(final int sizeOfRandomString)
{
final Random random=new Random();
final StringBuilder sb=new StringBuilder(sizeOfRandomString);
for(int i=0;i<sizeOfRandomString;++i)
sb.append(ALLOWED_CHARACTERS.charAt(random.nextInt(ALLOWED_CHARACTERS.length())));
return sb.toString();
}
and on Kotlin:
companion object {
private val ALLOWED_CHARACTERS = "0123456789qwertyuiopasdfghjklzxcvbnm"
}
private fun getRandomString(sizeOfRandomString: Int): String {
val random = Random()
val sb = StringBuilder(sizeOfRandomString)
for (i in 0 until sizeOfRandomString)
sb.append(ALLOWED_CHARACTERS[random.nextInt(ALLOWED_CHARACTERS.length)])
return sb.toString()
}
There are a few things wrong with your code.
You cannot cast from an int to a string. Cast it to a char instead. This however will only give you a single char so instead you could generate a random number for the length of your string. Then run a for loop to generate random chars. You can define a StringBuilder as well and add the chars to that, then get your random string using the toString() method
example:
public static String random() {
Random generator = new Random();
StringBuilder randomStringBuilder = new StringBuilder();
int randomLength = generator.nextInt(MAX_LENGTH);
char tempChar;
for (int i = 0; i < randomLength; i++){
tempChar = (char) (generator.nextInt(96) + 32);
randomStringBuilder.append(tempChar);
}
return randomStringBuilder.toString();
}
Also, you should use random.compareTo() rather than ==
You need to import UUID.
Here is the code
import java.util.UUID;
id = UUID.randomUUID().toString();
this is how i generate my random strings with desired characters and desired length
char[] chars1 = "ABCDEF012GHIJKL345MNOPQR678STUVWXYZ9".toCharArray();
StringBuilder sb1 = new StringBuilder();
Random random1 = new Random();
for (int i = 0; i < 6; i++)
{
char c1 = chars1[random1.nextInt(chars1.length)];
sb1.append(c1);
}
String random_string = sb1.toString();
This function run in kotlin ->
fun randomString(stringLength: Int): String {
val list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray()
var randomS = ""
for (i in 1..stringLength) {
randomS += list[getRandomNumber(0, list.size - 1)]
}
return randomS
}
fun getRandomNumber(min: Int, max: Int): Int {
return Random().nextInt((max - min) + 1) + min
}
Or you can use my library
https://github.com/Aryan-mor/Utils-Library
You can simply use the following method to generate random String with 5 character and it will return arrayList of random String
public ArrayList<String> generateRandomString() {
ArrayList<String> list = new ArrayList<>();
Random rnd = new Random();
String str = "";
String randomLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String randomLetterSmall = "abcdefghijklmnopqrstuvwxyz";
for (int n = 0; n < 50; n++) {
str = String.valueOf(randomLetters.charAt(rnd.nextInt(randomLetters.length())));
str += String.valueOf(randomLetterSmall.charAt(rnd.nextInt(randomLetters.length())));
str += String.valueOf(randomLetterSmall.charAt(rnd.nextInt(randomLetters.length())));
str += String.valueOf(randomLetterSmall.charAt(rnd.nextInt(randomLetters.length())));
str += String.valueOf(randomLetterSmall.charAt(rnd.nextInt(randomLetters.length())));
//Copy above line to increase character of the String
list.add(str);
}
Collections.sort(list);
return list;
}
private fun getRandomHexString(numchars: Int): String? {
val r = Random()
val sb = StringBuffer()
while (sb.length < numchars) {
sb.append(Integer.toHexString(r.nextInt()))
}
return sb.toString().substring(0, numchars)
}
You cannot cast an int to a String. Try:
Random generator = new Random();
String x = String.valueOf (generator.nextInt(96) + 32);
final String[] Textlist = { "Text1", "Text2", "Text3"};
TextView yourTextView = (TextView)findViewById(R.id.yourTextView);
Random random = new Random();
String randomText = TextList[random.nextInt(TextList.length)];
yourTextView.setText(randomText);
Quick one liner using the org.apache.commons.lang3.RandomStringUtils package.
String randonString = RandomStringUtils.randomAlphanumeric(16);
Requires the library dependency in the gradle build file:
implementation 'org.apache.commons:commons-text:1.6'
You can simply convert current time (in millis) to string such as
import java.util.Calendar;
String newRandomId = String.valueOf(Calendar.getInstance().getTimeInMillis());
Or
String newRandomId = Calendar.getInstance().getTimeInMillis() + "";
//Eg: output: "1602791949543"

regular-expressions android

i have string like these for example
309\306\308\337_338
309\306\337_338
310
311\315_316\336_337
311\315_316\336_337
311\335_336
these strings means list of page number , for example string "309\306\308\337_339" means
pages 309,306,308,337,338,339
i want to pass one of these string to function which return it as string like this
309,306,308,337,338,339
this function do that but in c# , i want to impalement in android
private static string Get_PageNumbers(string str)
{
ArrayList arrAll = new ArrayList();
MatchCollection match;
string[] excar;
string strid, firstNumber, lastlNumber;
int fn, ln;
ArrayList arrID = new ArrayList();
//***In Case The Range Number Between "_"
if (str.Contains("_"))
{
// match_reg = new Regex("(w?[\\d]+)*(_[\\d]+)");
Regex matchReg = new Regex("(w?[\\69]+_[\\d]+)*(q?[\\d]+//)*(a?[\\d]+_[\\d]+)*(y?[\\d]+)*");
match = matchReg.Matches(str);
int count = match.Count;
excar = new string[0];
for (int i = 0; i < count; i++)
{
Array.Resize(ref excar, count);
excar[i] = match[i].Groups[0].Value;
if (excar[i] != string.Empty)
arrID.Add(excar[i]);
}
//******IF Array Contains Range Of Number Like"102_110"
if (str.Contains("_"))
{
for (int i = 0; i < arrID.Count; i++)
{
strid = arrID[i].ToString();
if (arrID[i].ToString().Contains("_"))
{
int idy = strid.LastIndexOf("_");
firstNumber = strid.Substring(0, idy);
if (idy != -1)
{
lastlNumber = strid.Substring(idy + 1);
fn = int.Parse(firstNumber);
arrAll.Add(fn);
ln = int.Parse(lastlNumber);
for (int c = fn; c < ln; c++)
{
fn++;
arrAll.Add(fn);
}
}
}
else
{
arrAll.Add(arrID[i].ToString());
}
}
//******If Array Contain More Than One Number
if (arrAll.Count > 0)
{
str = "";
for (int i = 0; i < arrAll.Count; i++)
{
if (str != string.Empty)
str = str + "," + arrAll[i];
else
str = arrAll[i].ToString();
}
}
}
}
//***If string Contains between "/" only without "_"
else if (str.Contains("/") && !str.Contains("_"))
{
str = str.Replace("/", ",");
}
else if (str.Contains("\\"))
{
str = str.Replace("\\", ",");
}
return str;
}
I think this is easier to do with split function:
public static String Get_PageNumbers(String str) {// Assume str = "309\\306\\308\\337_338"
String result = "";
String[] pages = str.split("\\\\"); // now we have pages = {"309","306","308","337_338"}
for (int i = 0; i < pages.length; i++) {
String page = pages[i];
int index = page.indexOf('_');
if (index != -1) { // special case i.e. "337_338", index = 3
int start = Integer.parseInt(page.substring(0, index)); // start = 337
int end = Integer.parseInt(page.substring(index + 1)); // end = 338
for (int j = start; j <= end; j++) {
result += String.valueOf(j);
if (j != end) { // don't add ',' after last one
result += ",";
}
}
} else { // regular case i.e. "309","306","308"
result += page;
}
if (i != (pages.length-1)) { // don't add ',' after last one
result += ",";
}
}
return result; // result = "309,306,308,337,338"
}
For example this function when called as follows:
String result1 = Get_PageNumbers("309\\306\\308\\337_338");
String result2 = Get_PageNumbers("311\\315_316\\336_337");
String result3 = Get_PageNumbers("310");
Returns:
309,306,308,337,338
311,315,316,336,337
310
if i can suggest different implementation....
first, split string with "\" str.split("\\");, here you receive an array string with single number or a pattern like "num_num"
for all string founded, if string NOT contains "" char, put string in another array (othArr named), than, you split again with "" str.split("_");, now you have a 2 position array
convert that 2 strings in integer
now create a loot to min val form max val or two strings converted (and put it into othArr)
tranform othArr in a string separated with ","

How can I store an integer array in SharedPreferences?

I want to save/recall an integer array using SharedPreferences. Is this possible?
You can try to do it this way:
Put your integers into a string, delimiting every int by a character, for example a comma, and then save them as a string:
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int[] list = new int[10];
StringBuilder str = new StringBuilder();
for (int i = 0; i < list.length; i++) {
str.append(list[i]).append(",");
}
prefs.edit().putString("string", str.toString());
Get the string and parse it using StringTokenizer:
String savedString = prefs.getString("string", "");
StringTokenizer st = new StringTokenizer(savedString, ",");
int[] savedList = new int[10];
for (int i = 0; i < 10; i++) {
savedList[i] = Integer.parseInt(st.nextToken());
}
You can't put Arrays in SharedPreferences, but you can workaround:
private static final String LEN_PREFIX = "Count_";
private static final String VAL_PREFIX = "IntValue_";
public void storeIntArray(String name, int[] array){
SharedPreferences.Editor edit= mContext.getSharedPreferences("NAME", Context.MODE_PRIVATE).edit();
edit.putInt(LEN_PREFIX + name, array.length);
int count = 0;
for (int i: array){
edit.putInt(VAL_PREFIX + name + count++, i);
}
edit.commit();
}
public int[] getFromPrefs(String name){
int[] ret;
SharedPreferences prefs = mContext.getSharedPreferences("NAME", Context.MODE_PRIVATE);
int count = prefs.getInt(LEN_PREFIX + name, 0);
ret = new int[count];
for (int i = 0; i < count; i++){
ret[i] = prefs.getInt(VAL_PREFIX+ name + i, i);
}
return ret;
}
Here's my version, based on Egor's answer. I prefer not to use StringBuilder unless I'm building an enourmous string, but thanks to Egor for using StringTokenizer -- haven't made much use of this in the past, but it's very handy! FYI, this went in my Utility class:
public static void saveIntListPrefs(
String name, Activity activity, List<Integer> list)
{
String s = "";
for (Integer i : list) {
s += i + ",";
}
Editor editor = activity.getPreferences(Context.MODE_PRIVATE).edit();
editor.putString(name, s);
editor.commit();
}
public static ArrayList<Integer> readIntArrayPrefs(String name, Activity activity)
{
SharedPreferences prefs = activity.getPreferences(Context.MODE_PRIVATE);
String s = prefs.getString(name, "");
StringTokenizer st = new StringTokenizer(s, ",");
ArrayList<Integer> result = new ArrayList<Integer>();
while (st.hasMoreTokens()) {
result.add(Integer.parseInt(st.nextToken()));
}
return result;
}
I like to use JSON, which can be stored and retrieved as a string, to represent any complex data in SharedPreferences.
So, in the case of an int array:
public void setPrefIntArray(String tag, int[] value)
{
SharedPreferences.Editor prefEditor = PreferenceManager.getDefaultSharedPreferences(context)
.edit();
String s;
try
{
JSONArray jsonArr = new JSONArray();
for (int i : value)
jsonArr.put(i);
JSONObject json = new JSONObject();
json.put(tag, jsonArr);
s = json.toString();
}
catch(JSONException excp)
{
s = "";
}
prefEditor.putString(tag, s);
prefEditor.commit();
}
public int[] getPrefIntArray(String tag, int[] defaultValue)
{
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
String s = pref.getString(tag, "");
try
{
JSONObject json = new JSONObject(new JSONTokener(s));
JSONArray jsonArr = json.getJSONArray(tag);
int[] result = new int[jsonArr.length()];
for (int i = 0; i < jsonArr.length(); i++)
result[i] = jsonArr.getInt(i);
return result;
}
catch(JSONException excp)
{
return defaultValue;
}
}
The beauty is that the same idea can be applied to any other complex data representable as a JSON.
Two solutions:
(1) Use http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
It has split/join functions that let you join and split the integers in one liners:
StringUtils.join([1, 2, 3], ';') = "1;2;3"
StringUtils.split("1;2;3", ';') = ["1", "2", "3"]
You'd still have to convert the strings back to integers, though.
Actually, for splitting java.lang.String.split() will work just as fine:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
(2) Use the SharedPreferences.putStringSet() (API 11):
SharedPreferences.Editor editor = preferences.edit();
int count = this.intSet.size();
if (count > 0) {
Set<String> theSet = new HashSet<String>();
for (Long l : this.intSet) {
theSet.add(String.valueOf(l));
}
editor.putStringSet(PREFS_KEY, theSet);
} else {
editor.remove(PREFS_KEY);
}
editor.commit();
And to get it back:
Set<String> theSet = this.preferences.getStringSet(PREFS_KEY, null);
if (theSet != null && !theSet.isEmpty()) {
this.intSet.clear();
for (String s : theSet) {
this.intSet.add(Integer.valueOf(s));
}
}
This code does not catch the NPEs or NumberFormatExceptions because the intSet is otherwise assured to not contain any nulls. But of course, if you cannot assure that in your code you should surround this with a try/catch.
Here is how the "convert to comma-separated String" solution could look in Kotlin, implemented as extension functions:
fun SharedPreferences.Editor.putIntArray(key: String, value: IntArray): SharedPreferences.Editor {
return putString(key, value.joinToString(
separator = ",",
transform = { it.toString() }))
}
fun SharedPreferences.getIntArray(key: String): IntArray {
with(getString(key, "")) {
with(if(isNotEmpty()) split(',') else return intArrayOf()) {
return IntArray(count(), { this[it].toInt() })
}
}
}
That way you can use putIntArray(String, IntArray) and getIntArray(String) just like the other put and set methods:
val prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit().putIntArray(INT_ARRAY_TEST_KEY, intArrayOf(1, 2, 3)).apply()
val intArray = prefs.getIntArray(INT_ARRAY_TEST_KEY)
I went for the below solution, it's the least verbose of what I could see in this thread (in my case I wanted to have a set as a collection). "value" is the of type Set<Int>.
Save:
sharedPreferences.edit {
if (value.isNotEmpty()) {
putStringSet(key, hashSetOf(*value.map { it.toString() }.toTypedArray()))
} else {
remove(key)
}
}
Retrieve:
val stringSet = sharedPreferences.getStringSet(key, null)
if (stringSet.isNullOrEmpty()) return emptySet()
return setOf<Int>(*stringSet.map { Integer.valueOf(it) }.toTypedArray())
You can only save primitive values in sharedPreference. Use Sqlite instead.

Android from string to string array

hi to all i have this code have this code which reads a some text and it extracts any strings between the '[' and ']' and it should print it on the screen
String lines[] = {addressString};
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
String fields = lines[i].substring(be+1, e);
}
my question is that i want to change the string "fields" to an array string so when i print it
i can print it as fields[0],fields[1],....etc until the end of the text....?
any suggestions...??
Thanks a lot
Is this what you mean?
String lines[] = {addressString};
String fields[] = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
fields[i] = lines[i].substring(be+1, e);
}

Categories

Resources