How to display formatted sums in TextView?

I have a currency symbol in Stringand an amount in double. So far I have shown the following amounts:

amount.setText(currency + " " + amount);

And in some places I have 2 TextViewto display the amount with the addition between them:

currency.setText(currency);
amount.setText(amount);

This is normal. But I am moving this code to layout files with data binding to androids and is there an easy way to format and display amounts in android?

Like symbol[space]amountwhere the sum should be formatted with 2 decimal points, if any, otherwise an integer. For example, 100 will be displayed as 100, not 100.00, but 123.2 will be displayed as 123.20 along with the currency symbol first ($ 123.20, $ 100).

I know that there are several ways (data row format, NumberFormat). I can do it (I have not used any of them yet, I just heard about them), but what will be the easiest and cleanest way to do this?

I am trying to find and convert all 2 textual representations into one and find a unified way to format and display the quantity.

Thank.

+4
source share
7 answers

Inspired by @Nowshad's answer, I can do this without an external library with a data binding library.

If necessary, specify the currency and quantity in the layout files:

          <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:textAppearance="@style/TextAppearance.AppCompat.Large"
                app:amount="@{amount}"
                app:currency="@{currency}" />

And specify the binding adapter:

@BindingAdapter({"currency", "amount"})
    public static void setCurrencyAndAmount(TextView textView, String currency, double amount) {
        SpannableString spannableString = new SpannableString(currency + " " +
                new DecimalFormat("#.##").format(amount));

        spannableString.setSpan(new RelativeSizeSpan(0.6f), 0, 1, 0);

        textView.setText(spannableString);
    }

Simple and efficient.

0
source

amount.setText(currency + " " + amount);, , text.getText(), $ textView .

2 textView - getText(), .

2 0 ...

 double price=23.1000;
 int number=(int)((price*100)%100);
 yourTextView.setText(number==0?new DecimalFormat("0").format(price):new DecimalFormat("0.00").format(price));

: -

23.1000 ------- > 23.10

23.0007 ------- > 23

.....

: - 2, 2 textView. .

+1

, , , . , , MoneyTextView

<org.fabiomsr.moneytextview.MoneyTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    app:symbol="$"
    app:symbolGravity="start|top"
    app:symbolTextSize="30sp"
    app:symbolMargin="6dp"
    app:amount="1256.56"
    app:baseTextSize="54sp"
    app:decimalDigitsTextSize="30sp"
    app:decimalMargin="6dp"
    app:includeDecimalSeparator="false"
    app:baseTextColor="#FBFFE3"/>
+1
amount.setText(currency + String.format("%.2f", amount));
0

TextView.

Android, , System.out.format( "% 02d" , n);?

DataBinding, , origin android: text ( ) ().

Android - (https://developer.android.com/topic/libraries/data-binding/index.html#expression_language)

android:text="@{@string/nameFormat(firstName, lastName)}"
android:text="@{@plurals/banana(bananaCount)}"

But if you want it in a more flexible way, you need to write your own binding methods, as shown below.

In your code

 @BindingAdapter(value = {"android:text", "android:formatArgs"}, requireAll = false)
    public static void bindTextStr(TextView tv, String text, Object[] formatstr) {
        if (formatstr == null) {
            tv.setText(text);
        } else {
            tv.setText(String.format(text, formatstr));
        }
    }

In your xml layout,

<variable
            name="normalStr"
            type="java.lang.String"/>
        <variable name="data" type="Object[]"/>
...
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{normalStr}"
        android:formatArgs="@{data}"
        />

In action code

    mBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_main, null, false);
    mBinding.setNormalStr("Data : 1(%s), 2(%d)");
    mBinding.setData(new Object[]{"Hello World",12345});
0
source

Maybe this is useful: MultiSizeTextView

<com.github.captain_miao.multisizetextview.MultiSizeTextView
    android:id="@+id/multi_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"

    app:beforeText="@string/before_text"
    app:beforeTextColor="@color/red"
    app:beforeTextSize="@dimen/text_size_small"
    app:beforeTextStyle="bold"

    app:centerText="@string/app_name"
    app:centerTextColor="@color/gray"
    app:centerTextSize="@dimen/text_size_big"

    app:afterText="@string/after_text"
    app:afterTextColor="@color/blue"
    app:afterTextSize="@dimen/text_size_small"
    app:afterTextStyle="bold"
    />

enter image description here

0
source

do it like that

int enduser=35000000;
TextView lblEnduser=(TextView)v.findViewById(R.id.lblEnduser);
lblEnduser.setText(String.format("%,.0f", Float.valueOf(enduser) ));

: 35,000,000

0
source

Source: https://habr.com/ru/post/1653754/


All Articles