Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2015 20:34
Show Gist options
  • Star 75 You must be signed in to star a gist
  • Fork 63 You must be signed in to fork a gist
  • Save anonymous/fa134c55a4a43e8d6004 to your computer and use it in GitHub Desktop.
Save anonymous/fa134c55a4a43e8d6004 to your computer and use it in GitHub Desktop.
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
@LaszloLajosT
Copy link

Why do I get these erorrs?
‏‏لقطة الشاشة (9)

Do you know that this course is in Java and not kotlin?:) You can find some solutions on StackOverFlow.
One of your search could be like this: Unresolved reference in kotlin with id and activity_main

Solution could be:

  1. Removing import android.R solved the issue.
  2. Sometimes all you need is clean and rebuild the project.
Build -> Clean Project 
Build -> Rebuild Project

@LaszloLajosT
Copy link

LaszloLajosT commented Jun 17, 2020

i want to change the currency from USD to SAR or any other currency.
How i can change it.
this is the code i'm using
} private void displayPrice(int number) { TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); }

Change currency?
Same message on comment but slightly more info.

@UsmaniyDiyorbek
Copy link

image
where is properties??

@Abdulmateenchitrali
Copy link

By default the NumberFormat.getCurrencyInstance().formate() give us dollar sign if you want to your choice of sign then look underneath
code for pkr i just change the method like this

public void displayPrice(int price){
TextView textView=(TextView)findViewById(R.id.price_value);
Locale pakistaniRupees=new Locale("urdu","pk");
NumberFormat pakistaniFormat=NumberFormat.getCurrencyInstance(pakistaniRupees);
textView.setText(" "+pakistaniFormat.format(price));

}

@Abdulmateenchitrali
Copy link

Abdulmateenchitrali commented Jul 8, 2020 via email

@humandroid01
Copy link

Why there is so many errors in this code?

@Yashi279
Copy link

my code shows error in NumberFormat. please help me to solve this problem

@Yashi279
Copy link

android
what should i do with these problem

Add--- import.java.text.NumberFormat in your code

@Abdulmateenchitrali
Copy link

Abdulmateenchitrali commented Jul 12, 2020 via email

@tarek9999
Copy link

@akarsh
thank you

@trx6
Copy link

trx6 commented Jul 19, 2020

@Abdulmateenchitrali

Screen Shot 2020-07-19 at 21 48 52

how can I modify this errors?

@tarek9999
Copy link

tarek9999 commented Jul 19, 2020 via email

@vijayindalkar
Copy link

ooskai @Abdulmateenchitrali you should delete import android.support.v7.app.AppCompatActivity; and write this : import androidx.appcompat.app.AppCompatActivity;

On Sun, Jul 19, 2020 at 7:50 PM Asuka @.> wrote: @.* commented on this gist. ------------------------------ @Abdulmateenchitrali https://github.com/Abdulmateenchitrali [image: Screen Shot 2020-07-19 at 21 48 52] https://user-images.githubusercontent.com/68496362/87881349-b0537f80-ca09-11ea-8b1c-288734ca985d.png how can I modify this errors? — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/fa134c55a4a43e8d6004#gistcomment-3383928, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQFJ5BAFOGA4FL3YBLWO5C3R4MW5JANCNFSM4H3KMDZA .

no need to do that

@trx6
Copy link

trx6 commented Jul 23, 2020

@tarek9999
@vijayindalkar
i wrote this like you say:
import androidx.appcompat.app.AppCompatActivity;
and the errors is gone+Thx

@Mvlprem
Copy link

Mvlprem commented Aug 1, 2020

For all of those who are trying to get currency in their locale this will help

The lines of code you see below is exactly the same as display method the one you see in quantity_text_view

private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}

The major difference here is this line
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));

Now what exactly this line of code does is,it's adding the currecy symbol to your calculation like this $10.00

This whole line is just to get currency sign **(NumberFormat.getCurrencyInstance().format()

Now to change currency to your locale there are two ways:

Easy Way

  1. priceTextView.setText("₹"+number);

In this you provide your currency symbol directly inside these "" marks

This line is exactly same as one you see in display method but here your adding your currency sign to the number

In this way you don't get decimal numbers what i mean is you get like this ₹10 instead of this ₹10.00

Udacity Way

  1. Like i said above

This whole line is just to get currency sign (NumberFormat.getCurrencyInstance().format(number).

All we have to do is change it to your local like this

priceTextView.setText(NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(number));

Above i've added this new Locale("en","IN") new line inside of getCurrencyInstance()

By adding that line we are saying i want the currency to be in other local instead of default one with this new Locale()

Now we need to tell in what local we want the currency to be.

We say that by adding these "en" , "in"

  • "en" -This is language and i want it to be in ENGLISH.

  • "IN" -This is country since i'm INDIAN i say "IN"

HOPE THIS WILL HELP , THANK YOU!

@adelrabi
Copy link

adelrabi commented Sep 8, 2020

Annotation 2020-09-08 194840

what is wrong ?

@LaszloLajosT
Copy link

what is wrong ?

Just look at the error message it tells you.

... Invalid character ....

<String name=...
can't use special characters like "$" or "#" or white character etc.
The resource name must start with a letter.

@chetan081
Copy link

For all of those who are trying to get currency in their locale this will help

The lines of code you see below is exactly the same as display method the one you see in quantity_text_view

private void displayPrice(int number) { TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); }

The major difference here is this line
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));

Now what exactly this line of code does is,it's adding the currecy symbol to your calculation like this $10.00

This whole line is just to get currency sign **(NumberFormat.getCurrencyInstance().format()

Now to change currency to your locale there are two ways:

Easy Way

  1. priceTextView.setText("₹"+number);

In this you provide your currency symbol directly inside these "" marks

This line is exactly same as one you see in display method but here your adding your currency sign to the number

In this way you don't get decimal numbers what i mean is you get like this ₹10 instead of this ₹10.00

Udacity Way

  1. Like i said above

This whole line is just to get currency sign (NumberFormat.getCurrencyInstance().format(number).

All we have to do is change it to your local like this

priceTextView.setText(NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(number));

Above i've added this new Locale("en","IN") new line inside of getCurrencyInstance()

By adding that line we are saying i want the currency to be in other local instead of default one with this new Locale()

Now we need to tell in what local we want the currency to be.

We say that by adding these "en" , "in"

  • "en" -This is language and i want it to be in ENGLISH.
  • "IN" -This is country since i'm INDIAN i say "IN"

HOPE THIS WILL HELP , THANK YOU!

That was really awesome Thanks!

@adham117
Copy link

2020-12-16 (3)
can some one help me

@rdn025
Copy link

rdn025 commented May 29, 2021

TIP:
The currency used is the one your phone uses, not what is set on your computer. Some phones don't allow you to change the currency explicitly, but your selected language on the phone dictates this, for example choosing English (United Kingdom) will automatically set the currency to "£" (Pounds).
On a Samsung phone in most cases this can be set under Options->Controls->Language and Input

Thnx Buddy!!! It Resolves my problem.

@alf-win
Copy link

alf-win commented Jun 15, 2021

Screenshot 2021-06-15 at 11 39 41 PM

Screenshot 2021-06-15 at 11 49 21 PM

please help !

@simarjeet30
Copy link

@chekwon78 Do try adding a TextView in activity_main.xml and assign it an id like this android:id="@+id/price_text_view".

Thanks soo much ,it worked for me

@Kgarmel
Copy link

Kgarmel commented Jul 25, 2021

Hello,
Need your help to solve the numberFormat error after adding the "Add unambiguous imports on the fly"

image

@Kgarmel
Copy link

Kgarmel commented Jul 25, 2021

It's ok, solved by adding "import java.text.NumberFormat;" . thanks.

@artjomHeim
Copy link

grafik

error: invalid method declaration; return type required
} displayPrice(2*5);
^

@SaiPrajoth
Copy link

don't why my app is crashing after clicking on the ORDER button

@shashankagrawal158
Copy link

error
How do I resolve this error?

@saurav-ux
Copy link

Code of [chetan081] approach

private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price);
priceTextView.setText("₹ "+ number);
}

@noah12binche
Copy link

android_errer
How do i resolve this error

@Saphiriela
Copy link

After latest update of Android studio it should be this code in Kotlin:

/**
* This method displays the given price on the screen.
*/
private fun displayPrice(number: Int) {
val priceTextView = findViewById(com.example.justjava.R.id.price_text_view) as TextView
priceTextView.setText(java.text.NumberFormat.getCurrencyInstance().format(number))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment