Search code examples
javaandroidandroid-studiocurrency

Update MoneyTextView Android Studio


I am creating an app in Android Studio that uses a TextMoneyView to display a price. The price can be updated when a user chooses an item.

XML

<org.fabiomsr.moneytextview.MoneyTextView
    android:id="@+id/totalPrice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    app:baseTextSize="50dp"
    app:decimalSeparator="."
    app:symbol="£"
    app:symbolGravity="start|bottom" />

Java

private int mTotal = 0;
private MoneyTextView mTotalPrice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    mTotalPrice = (MoneyTextView) findViewById(R.id.totalPrice);
}

    public void smallAdd(View view) {
    mTotal += 3;
    ...
    if (mTotalPrice != null)
    {
        mTotalPrice.setText(Integer.toString(mTotal));
    }
}

I wasn't sure how to update it so I copied some of my earlier code I used to update a TextView. My code has the error Cannot resolve method 'setText(java.lang.String)'. I presume this is because setText can only be used with TextView but what would be the alternative for a MoneyTextView?


Solution

  • You have to use moneyTextView.setAmount(value);

    So in your case you have to change:

    mTotalPrice.setText(Integer.toString(mTotal));
    

    with:

    mTotalPrice.setAmount(mTotal);
    

    Check the link for the the readme: https://github.com/fabiomsr/MoneyTextView