Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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));
}
@Jahani

This comment has been minimized.

Copy link

commented Aug 25, 2015

My phone shows £ instead of $

@edof

This comment has been minimized.

Copy link

commented Aug 29, 2015

@Jahani maybe because your local curency is £ not $

@abdulhannanali

This comment has been minimized.

Copy link

commented Sep 1, 2015

@Jahani we are using NumberFormat library provided by android which formats the number according to whatever your local currency is. So it's understood from here that your local currency might not be $ Euro or pound or whatever that thing is.

@anasimao

This comment has been minimized.

Copy link

commented Sep 2, 2015

My phone shows a weird symbol instead of the currency, is like a star shaped character

@kewal82

This comment has been minimized.

Copy link

commented Sep 19, 2015

Iam from Rupees using country and the phone just shows RSs that i put on XML code only till ODER button is pressed after that it just use $ sign.how to fix it.

@akarsh

This comment has been minimized.

Copy link

commented Sep 24, 2015

@Jahani, @edof, @abdulhannanali,
I think in order to see the currency you want. Just change the language in the android phone.
I got pound because of having the language as English (UK), when i changed it to English(India) it gave me RS. I hope this solves the problem

@zynabsmaan

This comment has been minimized.

Copy link

commented Oct 14, 2015

my app has stopped
my mobile phone didn't run the app
why :(

@freei

This comment has been minimized.

Copy link

commented Nov 10, 2015

@zynabsmaan. Give more information about your problem.

@ykoech

This comment has been minimized.

Copy link

commented Dec 14, 2015

If you want to change from whatever currency that is being displayed to $, just change your device's language to English(United states) under settings.

@havinhphu188

This comment has been minimized.

Copy link

commented Jan 24, 2016

for me, its display đ(vietnamese currency). the currency depend on system language. you can change it in setting> language and input

@ankushkaira

This comment has been minimized.

Copy link

commented Feb 10, 2016

cannot resolve symbol 'price_text_view' in mainactivity.java

@Otzkhan

This comment has been minimized.

Copy link

commented Feb 15, 2016

Hello, when I click the order button, nothing changes. There is a screenshot of codes and I noticed something showed in picture. Why color of this area is different? Can this cause this problem? What is solution?
adsiz

@joja19

This comment has been minimized.

Copy link

commented Feb 24, 2016

@Otzkhan, mine also are green quotes, but the order button is working fine.
I just noted that when rotating the screen the numbers disappear and I have to click "order" again.

@fabiorbap

This comment has been minimized.

Copy link

commented May 22, 2016

@ankushkaira Have you set an id for the price text view? If not, then you have to set that view's id to be price_text_view
@Otzkhan Hover your mouse over that part of the code and check what android studio says. Also, did you write the onClick attribute on the xml for the button? If not, that may be the reason why it's not working

@nassimjd

This comment has been minimized.

Copy link

commented May 30, 2016

Calculation is right but does not show currency here. No big deal but would like to know why. Anyone else with this prob?

@totoantonio

This comment has been minimized.

Copy link

commented May 31, 2016

Was here!

@Clumsy-Coder

This comment has been minimized.

Copy link

commented Jun 6, 2016

@nassimjd on line 6 of the code, add an argument for the method getCurrencyInstance(), it takes a Locale type. This forces the symbol to be the choice you want.
ex: priceTextView.setText(NumberFormat.getCurrencyInstance(Locale.CANADA).format(number));

here's more info about the method: link
info about Locale type: link .Go to the Fields section.

Also, you might have a problem running the app on the phone after making the change.
Try:
Build > Clean Project. Then
Build > Rebuild Project. Then
Launch the app like you normally would.

Hope it helps.

@rohit-yadav

This comment has been minimized.

Copy link

commented Jun 17, 2016

I'm getting an error.. after I press order it shows..

2
0
RS. 10.00
₹0

I don't know why it is changing my Quantity and Price text.
Need help..

@dinhnambk

This comment has been minimized.

Copy link

commented Jun 29, 2016

2016-06-29

I got these errors. I tried to change minSDKVerson to 15 but when I run app on my emulator which has API 23, it doesn't work. How can i fix it

@mumairofficial

This comment has been minimized.

Copy link

commented Jun 29, 2016

to get $ currency do this trick with .getCurrencyInstace(new Locale("en", "US"))

@florianmonfort

This comment has been minimized.

Copy link

commented Jun 30, 2016

@dinhnambk I have the same problem

@neochaochaos

This comment has been minimized.

Copy link

commented Jul 4, 2016

@florianmonfort I changedimport android.icu.text.NumberFormat; to import java.text.NumberFormat;. Fixed the problem.

@Journeyman92

This comment has been minimized.

Copy link

commented Jul 4, 2016

Mine displays the price on the order button and the price does not change. I dont know what the problem is.

@vicky2193

This comment has been minimized.

Copy link

commented Jul 5, 2016

@Rohit5479 @Journeyman92
Check to make sure that your id names for the TextViews match the ids being used in the Java code. I ran into the same problem because I named my TextViews differently than they did in the video, so it didn't match up to their code. If you still have a problem, make sure you set up the id names with the corresponding TextView (i.e. android:id="@+id/quantity_text_view" is in the same view as android:text="0")

@ghost

This comment has been minimized.

Copy link

commented Jul 5, 2016

CURRENCY SYMBOL ISSUE FIX:

In order for your currency to display the correct one based on your country. You MUST go to your phone's settings and change the "Language" setting to the precise one for your country in which you reside in.

So for instance:

For the tutorial if you want to see the "$" symbol then change your "Language" settings to "English United States(US)"

@ferragamoking74

This comment has been minimized.

Copy link

commented Jul 12, 2016

I had an issue with the import android.icu.text.NumberFormat. The app crashed after clicking the button. I changed to import java.text.NumberFormat on the advice of neochaochaos and it works. Thanks a million!!

@musti2304

This comment has been minimized.

Copy link

commented Jul 20, 2016

@neochaochaos
Your solution worked for me. Thanks

@leelakrishnap

This comment has been minimized.

Copy link

commented Jul 20, 2016

when rotating the screen the numbers disappear and I have to click "order" again.
Any solution to this?
Thanks in Advance!!

@chekwon78

This comment has been minimized.

Copy link

commented Jul 22, 2016

image

I included "import java.text.NumberFormat;" as advised and fixed the problem with NumberFormat but it still can't resolve price_text_view. Any help on this would be greatly appreciated

@cRAN-cg

This comment has been minimized.

Copy link

commented Jul 24, 2016

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

@ghost

This comment has been minimized.

Copy link

commented Aug 1, 2016

One note: if you have downloaded the API level 24 from SDK manager you should import manually the java.text.

@udit71992

This comment has been minimized.

Copy link

commented Aug 1, 2016

My app is not showing any symbol.

@androidar

This comment has been minimized.

Copy link

commented Aug 2, 2016

Thanks, @Clumsy-Coder, @neochaochaos, and @guiwp.. That worked for me as well..

@udit71992: I was having the same problem, but the method suggested by Clumsy-Coder worked for me
Here is the code I used: priceTextView.setText(NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(number));

@a923225

This comment has been minimized.

Copy link

commented Aug 6, 2016

When I clicked the Button on app, the app was stopped.
It doesn't work. Can anyone tell me what happened?

1
2

@ashrafsaleh

This comment has been minimized.

Copy link

commented Aug 12, 2016

android
what should i do with these problem

@com0t

This comment has been minimized.

Copy link

commented Aug 12, 2016

thanks! i changed language and succed

@MabelM1

This comment has been minimized.

Copy link

commented Aug 15, 2016

@neochaochaos Thanks for posting your solution!

@developerturkey34

This comment has been minimized.

Copy link

commented Aug 15, 2016

@ashrafsaleh

  1. Change android.icu.text.NumberFormat; to import java.text.NumberFormat
    2)Build > Clean Project
  2. Build > Rebuild Project
    It will work :)
@trxre

This comment has been minimized.

Copy link

commented Aug 18, 2016

@leelakrishnap

https://developer.android.com/training/basics/activity-lifecycle/recreating.html :

Your activity will be destroyed and recreated each time the user rotates the screen.

After rotating, your app has the default setting again. Your created number (price) was deleted and doesn't exsist anymore. You have to create it again.
We can't fix that yet.

@thebatking

This comment has been minimized.

Copy link

commented Aug 19, 2016

I can't find preferences for auto import setting.

@nabilatajrin

This comment has been minimized.

Copy link

commented Aug 27, 2016

Thanks to @neochaochaos, you saved my day..

@ItIsAnismit

This comment has been minimized.

Copy link

commented Aug 27, 2016

My code says this kind of error, what is the problem and the solution?

private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

/**
 * 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));`
}

}

@GetSetSan

This comment has been minimized.

Copy link

commented Sep 7, 2016

For those still having issues, make sure you've the below line in the top @line 17 (just under the line: import android.widget.TextView;)

import java.text.NumberFormat;

That should fix the issue.

@hirase

This comment has been minimized.

Copy link

commented Sep 7, 2016

@GetSetSan you solved my problem. thanks

@GetSetSan

This comment has been minimized.

Copy link

commented Sep 8, 2016

@hirase No Worries mate, Glad to hear that.

@vtk13

This comment has been minimized.

Copy link

commented Sep 14, 2016

I have an error: "Call requires API level 24 (current min is 15): android.icu.text.NumberFormat#getCurrencyInstance". But application works fine.
screen

@speedy0309

This comment has been minimized.

Copy link

commented Sep 17, 2016

screenshot_2016-09-17-16-07-02
Help me with this guys...
My App works just fine but the only problem is it shows "Rs" instead showing symbol for rupee...
is there any way to show the symbol...

thank u in advance

@BlessedWish

This comment has been minimized.

Copy link

commented Sep 24, 2016

How can I change my $ sign to show Le when the ORDER is clicked?

I placed the Le in front of the 0 like this Le0 , but after I click the order button it shows like this $20.00

Anybody, thanks

@ChenSam

This comment has been minimized.

Copy link

commented Sep 28, 2016

Use priceTextView.setText(NumberFormat.getCurrencyInstance(Locale.US).format(number)); for US dolloar

@wisnukurniawan

This comment has been minimized.

Copy link

commented Oct 2, 2016

Look this is how i create method for display price in Indonesia

capture

@Ahmedfarghal

This comment has been minimized.

Copy link

commented Oct 6, 2016

Please help me how to fix that error
screen shot 2016-10-06 at 5 23 26 pm

@manivannanragavan

This comment has been minimized.

Copy link

commented Oct 12, 2016

Please help to solve my errors....
when i run app in my phone and click order button it shows " unfortunately java has stopped " What does it mean?.
The error showing is
image

@Vitnere

This comment has been minimized.

Copy link

commented Oct 19, 2016

@neochaochaos Tnx for bug fix

@00amaranth00

This comment has been minimized.

Copy link

commented Oct 23, 2016

@GetSetSan, thanks .. this instruction fixed the issue with NumberFormat after updating the settings

For those still having issues, make sure you've the below line in the top @line 17 (just under the line: import android.widget.TextView;)

import java.text.NumberFormat;

That should fix the issue.

@Janipasha

This comment has been minimized.

Copy link

commented Oct 28, 2016

add unambigous on the fly is not working to resolve the NumberFormat error. What should I do ?

@mJeevankishore

This comment has been minimized.

Copy link

commented Oct 30, 2016

instead of my $0 price value is getting changed to $20 please help me to correct this

@mJeevankishore

This comment has been minimized.

Copy link

commented Oct 30, 2016

i have given rs indian currency but after run it is changing back to $ please help me out

@RuanDeLange

This comment has been minimized.

Copy link

commented Nov 3, 2016

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

@kanchanavenkatesan

This comment has been minimized.

Copy link

commented Nov 4, 2016

for me price_text_view is showing error.I tried above all methods..can anyone pls tell me ,what can i do??

@kingfawzy

This comment has been minimized.

Copy link

commented Nov 15, 2016

when i made Auto import i got an error message and it didn't be correct unless i added this code
import java.text.NumberFormat;
should i make import to every parameter i will add or the auto import should work???

@parmarravi

This comment has been minimized.

Copy link

commented Nov 19, 2016

Use this it worked for me .

priceTextView.setText(java.text.NumberFormat.getCurrencyInstance().format(number));

@a-tafari

This comment has been minimized.

Copy link

commented Nov 26, 2016

thank you @parmarravi.. that worked for me too.

@BalaPrasad346

This comment has been minimized.

Copy link

commented Nov 27, 2016

screenshot 24
i am not getting phone preview ,any one plz help how to get it

@BalaPrasad346

This comment has been minimized.

Copy link

commented Nov 27, 2016

screenshot 26

i am unable to change price,

@SimonPeter1909

This comment has been minimized.

Copy link

commented Dec 6, 2016

where should i go to enable the auto import?

@sereasdh

This comment has been minimized.

Copy link

commented Dec 10, 2016

i think lots of miss importing java.text.NumberFormat;

@gvkkumar143

This comment has been minimized.

Copy link

commented Dec 13, 2016

screenshot_2016-12-14-03-53-36-758_com example android justjava

My First App... Thank u Instructors Because of you I made my dream fullfill as i am From Mechanical Stream You People Made Our day Thanks for your Program

code for the above app is mentioned below so friends you can also do this

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/cup"
    android:scaleType="fitXY"/>
<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Quantity"
    android:textAllCaps="true"
    android:textColor="#FFFFFF"
    android:textSize="30dp"
    android:padding="20dp"
    android:layout_centerHorizontal="true"/>

<Button
    android:id="@+id/plus_button_view"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_below="@id/quantity_text_view"
    android:layout_marginBottom="16dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="60dp"
    android:onClick="increment"
    android:text="+"
    android:padding="20dp"/>

<TextView
    android:id="@+id/quantityvalue_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/quantity_text_view"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="16dp"
    android:paddingTop="15dp"
    android:text="0"
    android:textColor="#FFFFFF"
    android:textSize="22sp"
    android:padding="20dp"/>

<Button
    android:id="@+id/minus_button_view"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_below="@id/quantity_text_view"
    android:layout_marginBottom="16dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="60dp"
    android:onClick="decrement"
    android:text="-"
    android:padding="20dp"/>

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/minus_button_view"
    android:layout_marginBottom="16dp"
    android:text="Price"
    android:textColor="#FFF"
    android:textSize="25dp"
    android:layout_centerHorizontal="true"
    android:textAllCaps="true"
    android:padding="20dp"/>

<TextView
    android:id="@+id/pricevalue_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/price_text_view"
    android:layout_marginBottom="16dp"
    android:text="Rs. 0"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:textColor="#FFFFFF"
    android:padding="20dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/pricevalue_text_view"
    android:onClick="submitOrder"
    android:layout_centerHorizontal="true"
    android:text="Order"
    android:padding="20dp"/>
@AbhilashG97

This comment has been minimized.

Copy link

commented Jan 7, 2017

For those having issues with NumberFormat, here are a couple of things you can try -

  1. Go to Help > Find Action > Type "Auto Import" > Press Enter > Tick "Add unambiguous imports on the fly." [Obvious approach]
  2. Click on NumberFormat > Press Alt+Enter > Select NumberFormat (java.text)

Hope this helps.

@Shin303igami

This comment has been minimized.

Copy link

commented Jan 9, 2017

I have enabled Java Auto Import section, yet header file is not been automatically included & NumberFormat has red underlined.
Everytime I have to press Alt + Enter to include library " import java.text.NumberFormat; ".
Can anyone tell me why import java.text.NumberFormat; isn't getting automatically included in MainActivity.java

@HumbsBR

This comment has been minimized.

Copy link

commented Jan 12, 2017

For those having issues with NumberFormat, here are a couple of things you can try -

Go to Help > Find Action > Type "Auto Import" > Press Enter > Tick "Add unambiguous imports on the fly." [Obvious approach]
Click on NumberFormat > Press Alt+Enter > Select NumberFormat (java.text)
Hope this helps.

Thanks, man!

@LiorA1

This comment has been minimized.

Copy link

commented Jan 14, 2017

Imported it manually..

@filiphanek

This comment has been minimized.

Copy link

commented Jan 17, 2017

@thebatking

For Windows, go to File > Settings > Editor > General > Auto Import

@filiphanek

This comment has been minimized.

Copy link

commented Jan 17, 2017

Just my observation. I activated Auto Import and it did not do what it should. I had to add manually line below:
import java.text.NumberFormat;

Do anybody know why?

@adam-sanchez

This comment has been minimized.

Copy link

commented Jan 17, 2017

17-01-2017 15-17-50
works well!

@nadeemqwerty

This comment has been minimized.

Copy link

commented Jan 18, 2017

on pressing button , it shows ,"Unfortunately Just Java has stopped.".

@AHMADanis

This comment has been minimized.

Copy link

commented Jan 18, 2017

I did Auto Import but it did not import anything automatically then I import it manually. Now it gona start working but now there is issue in my currency sign. I reside in sweden and its showing me "£" sign instead of local currency or doller sign

@kainfrost

This comment has been minimized.

Copy link

commented Jan 22, 2017

error

I have this error , i tried Auto import settings , alt + enter on number format, but still continues , can someone help me ? I can't get over and it bothers me.

@Sunny613

This comment has been minimized.

Copy link

commented Jan 25, 2017

hello kainFrost,

i think that problem is occurring because you have pasted the code at wrong position.this code should be pasted inside the last brace
refer to this image

copy and paste below link in your browser to see the image

https://cloud.githubusercontent.com/assets/25347790/22295814/4111d65e-e33e-11e6-93ed-47ce4e3c7b2a.jpg

@pmessina

This comment has been minimized.

Copy link

commented Jan 27, 2017

@kainfrost Auto import brings in the classes you need for the activity , not change the number format. if you want to change the Locale for your number format, you do something like this: NumberFormat.getCurrencyInstance(Locale.US).format("3.30");

@hercules7

This comment has been minimized.

Copy link

commented Jan 27, 2017

My app Crashes after I click the order button in my Mobile with SDK 17. How to fix that?
screenshot 12
screenshot 13

This was what i got when I tried to run the code on my mobile
On mobile It show "Unfortunately Just Java has stopped" Please help!

@Mergen23

This comment has been minimized.

Copy link

commented Jan 31, 2017

these Solve my all errors!

1 - first you need to import this line of code in the MainActivity:
### import java.text.NumberFormat;
2- below the display(2); paste this code:
### displayPrice(2*5);
3- add two TextView of Price and the Number

@piotrsierpinski

This comment has been minimized.

Copy link

commented Feb 1, 2017

And be cool - by default it shows your local currency. If you are from UK, it'll be £.

@AliEtoom

This comment has been minimized.

Copy link

commented Feb 3, 2017

untitled
In case if you faced the error which states: Call Requires API Level 24 (current min is 15) the way to solve it is:

  1. First comment this import: import android.icu.text.NumberFormat;

  2. and instead use this import: import java.text.NumberFormat;

@Anshuman1638

This comment has been minimized.

Copy link

commented Feb 4, 2017

@AliEtoom for your reply. Although the problem is resolved. Can you please explain me the difference between the two cases?

@moacirsis

This comment has been minimized.

Copy link

commented Feb 15, 2017

Erro no numberFormat, já fiz os imports e chamei na activity ?

main
activity

@jimmmy94

This comment has been minimized.

Copy link

commented Feb 19, 2017

I did Auto Import but it did not import anything automatically.
Can anyone show me how to import manually?

@jJayyyyyyy

This comment has been minimized.

Copy link

commented Feb 20, 2017

@neochaochaos thanks a lot!

@Mojcaaap

This comment has been minimized.

Copy link

commented Feb 22, 2017

@jimmmy94 I had the same problem. Fix: after you did your Auto Import, slide with your mouse on .NumberFormat a message pops up, then press alt+enter, it offers you 2 options to import. I chose the java.text.(double click). Hope it helps.

@rikumo

This comment has been minimized.

Copy link

commented Feb 23, 2017

When I started an empty activity, my MainActivity extends AppCompatActivity and not ActionBarActivity in the video. Anyone knows why?

@amangautam015

This comment has been minimized.

Copy link

commented Feb 28, 2017

for local currency there are separate functions eg Rupees "hi" and "IN" .... >>Output = Rs. 10
curreny

@meditat

This comment has been minimized.

Copy link

commented Mar 3, 2017

it says requires min api label 24(min api 19).

@meditat

This comment has been minimized.

Copy link

commented Mar 3, 2017

`package com.example.shiva.justjava; /**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */

import android.icu.text.NumberFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.shiva.justjava.R;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);}

//code for increasing the quantity in our app.
public int counter=0 ;
public void submitOrder(View view){
counter++;
displayPri(counter);
displayQty(counter);
}
public void displayPri(int counter){
int coff=5;
TextView pri=(TextView)findViewById(R.id.price_text_view);
counter=5*counter;
String p=Integer.toString(counter);
pri.setText("$"+p);
}
public void displayQty(int counter){
TextView qty =(TextView)findViewById(R.id.quantity_text_view);
String s=Integer.toString(counter);
qty.setText(s);
}
}

`

I MADE IT LIKE THIS AND EVERYTHING IS WORKING FINE.

@kamilk812

This comment has been minimized.

Copy link

commented Mar 3, 2017

Can you Help mi in this code? I don't know where I made mistakes.I suppose that it is easy for you but not for me now.
1
2
3

@KedCole

This comment has been minimized.

Copy link

commented Mar 6, 2017

I have read through forum posts, but I believe I am missing answer...I do NOT receive any errors in event log, BUT when I click on the "ORDER" button in emulator my app crashes - below is code used, which is showing all the requirements I believe...can anyone provide any assistance??

/**

Add your package below. Package name can be found in the project's AndroidManifest.xml file.
This is the package name our example uses:
package com.example.android.justjava;
*/

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**

 This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(5);
}
/**

 This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

/**
 * 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));
}

}

@CoMarie

This comment has been minimized.

Copy link

commented Mar 8, 2017

can't understand this error
image

Some help ? Thanks!

@Sowailam

This comment has been minimized.

Copy link

commented Mar 18, 2017

@utkarshmahanata the comment before the display() method needs asterisk.
solve it and see if it works

@Sowailam

This comment has been minimized.

Copy link

commented Mar 18, 2017

@CoMarie it's not an error. it's telling your that the method is not used so use it inside the submitOrder() method to display the price when button is clicked.
and make sure to add android:id="@+id/price_text_view" in the price TextView

@neliamx4

This comment has been minimized.

Copy link

commented Mar 21, 2017

I've modified my Android Studio preferences to automatically import the unambiguous thing but still it didn't import it automatically :(

@vmasullo

This comment has been minimized.

Copy link

commented Mar 25, 2017

it worked perfectly

@AdeVic

This comment has been minimized.

Copy link

commented Apr 8, 2017

Please, how can i locate prefrences?

@rjoshi91

This comment has been minimized.

Copy link

commented Apr 14, 2017

import java.text.NumberFormat; need to be added below 4 import statements above in the code block. This is needed to told in advance or at start.

@plrang

This comment has been minimized.

Copy link

commented Apr 17, 2017

Seems to work as expected with the english interface

@PrateekAgr

This comment has been minimized.

Copy link

commented Apr 18, 2017

My app is not working and android studio shows
"Could not identify launch activity: Default Activity not found
Error while Launching activity"
and the auto import function is also not working
screenshot 94
screenshot 95

@EslamElgohary

This comment has been minimized.

Copy link

commented Apr 19, 2017

can any one tell me please when we will learn the code in "MainActivity.java" window without copy and paste, because i'm now on lesson 8 of this course and still don't know how :(
Thanks.

@EslamElgohary

This comment has been minimized.

Copy link

commented Apr 19, 2017

@PrateekAgr just try to close project and open it again, I hope this will fix your problem.

@EslamElgohary

This comment has been minimized.

Copy link

commented Apr 19, 2017

@AdeVic There is no "prefrences" on windows version . it called "settings" and you can find it under "File" menu

@odingit

This comment has been minimized.

Copy link

commented Apr 24, 2017

My Phone don't show $ or ¥

@PrateekAgr

This comment has been minimized.

Copy link

commented Apr 24, 2017

@EslamElgohary thx a lot!!

@slickr1ck

This comment has been minimized.

Copy link

commented Apr 26, 2017

ok i am back. So i got past this step before and then i was all the way to part 19 then i realized i ddint know anything that i learned so i started over (JUST JAVA) now i am back here . so for some reason i am stuck at this part where Number format is still highlighted.
image
.
All of my code is correct. MainActivity =-=-==-=-=-=-=-=(Or i think it is correct)
image

Code for Activity_main.....
image

@jsbaidwan

This comment has been minimized.

Copy link

commented Apr 30, 2017

@AliEttom
change import
import android.icu.text.NumberFormat;
to
import java.text.NumberFormat;
It will work

@vhstriker

This comment has been minimized.

Copy link

commented May 2, 2017

capture
does anyone know what I did wrong because I have no Idea

@ShanakaWickramaarachchi

This comment has been minimized.

Copy link

commented May 3, 2017

@vhstriker First repoen your project if not solved http://stackoverflow.com/questions/17421104/android-studio-marks-r-in-red-with-error-message-cannot-resolve-symbol-r- check this

@EslamElgohary you are now writing content in MainActivity.java if u dnt get the code its cz u have less knwledge in java ,so first complete java
course and come back,goodluck

@jerryVG

This comment has been minimized.

Copy link

commented May 10, 2017

vhstriker delete } from line number 38 . then put } at last line 45.

@priya2019

This comment has been minimized.

Copy link

commented Jun 1, 2017

As soon as I click "order"
the app stops and says "unfortunately just java has stopped".
what to do?
image

@trulyrohan

This comment has been minimized.

Copy link

commented Jun 3, 2017

it says cannot resolve price_text_view and quantity_text_view in spite of adding ids in the text views. No clue why...someone please help me out!!!

@PochtarIvan

This comment has been minimized.

Copy link

commented Jun 5, 2017

@priya2019
I Think it happens because of wrong import. You are using import android.icu.text.NumberFormat;
Change it to another one - import java.text.NumberFormat;
When import will be changed you see that .getCurrencyInstance().format(number) aren't red anymore

@PochtarIvan

This comment has been minimized.

Copy link

commented Jun 5, 2017

@trulyrohan
It could be because of you haven't added an import android.widget.TextView; but would be better to see screen shot with your java code and xml. And explanation how are you getting the error.

@karanbawa

This comment has been minimized.

Copy link

commented Jun 7, 2017

i am not able to find preferences or related things in android 2.3 to resolve number format...please help how to proceed.

@gouravdx6

This comment has been minimized.

Copy link

commented Jun 9, 2017

@HumbsBR , can you please explain this step in detail
Click on NumberFormat > Press Alt+Enter > Select NumberFormat (java.text)

@haagamble

This comment has been minimized.

Copy link

commented Jun 11, 2017

I was not able to find the preferences either. The main thing is that you need to have
import java.text.NumberFormat;
at the top under the other imports. You can just type it in. Or you can press Alt+Enter (doesn't matter where your cursor is) and a box will pop open saying "Class to Import". When I do it there are two choices. Choose the one that says NumberFormat(java.text). When you do that you will see the import line of code added to the top under the other imports.

@Tushar301

This comment has been minimized.

Copy link

commented Jun 12, 2017

2017-06-13

here is my code and it is working fine. Hope this will help you!

  1. activity_main.xml code:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Quantity"
    android:textAllCaps="true" />
<Button
    android:id="@+id/plus_button_view"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginBottom="16dp"
    android:layout_alignParentRight="true"
    android:onClick="increment"
    android:text="+" />

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    android:textColor="#000000"
    android:textSize="16sp" />
<Button
    android:id="@+id/minus_button_view"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:onClick="decrement"
    android:layout_marginTop="16dp"
    android:text="-" />
<TextView
    android:id="@+id/tag_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="16dp"
    android:text="Price"
    android:textAllCaps="true"
    android:textColor="#000000"
    android:textSize="16sp" />

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="$10"
    android:textColor="#000000"
    android:textSize="16sp" />

<Button
    android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="Order"
    android:textAllCaps="true"
    android:onClick="submitOrder"
   />
  1. Main_Activity.java :

package com.example.dell.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.view.View.*;

import android.os.Bundle;
//import android.view.View.OnClickListener;
import android.view.View.*;
import android.view.View;

import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    public int counter=2 ;

    public void increment(View v){
    counter++;
    displayQuantity(counter);
    }

    public void decrement(View vi){
    counter--;
    displayQuantity(counter);
    }
    public void submitOrder(View view){

     displayPrice(counter);
     displayQuantity(counter);
    

    }
    public void displayPrice(int counter){
    int x=10;
    TextView price=(TextView)findViewById(R.id.price_text_view);
    counter=x*counter;
    String p=Integer.toString(counter);
    price.setText("$"+p);
    }
    public void displayQuantity(int counter){
    TextView qty =(TextView)findViewById(R.id.quantity_text_view);
    String s=Integer.toString(counter);
    qty.setText(s);
    }
    }

@an20ki

This comment has been minimized.

Copy link

commented Jun 13, 2017

my auto-import dosent work .. i had to manually import .. import java.text.NumberFormat;

please help

@schaepersliam

This comment has been minimized.

Copy link

commented Jun 26, 2017

Here is a small extra:

If you want to set the coffee price to a decimal number copy and paste this code into your java file instead of the original code.

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

And for the submitOrder method add this to the code:

displayPrice(2.50);

By the way of course you can edit the value here too.
After adding this your method should look somethig like this:

public void submitOrder(View view) {
        display(1);
        displayPrice(2.50);
}
@SagarKisanAvhad

This comment has been minimized.

Copy link

commented Jul 5, 2017

For changing default currency to your Country's currency:
need to change getCurrencyInstance() method.

  1. Replace above method by getCurrencyInstance(Locale.Country_Constant)
    e.g. default currency: poun and would like to change into $. Locale constant for US is us so my method is getCurrencyInstance(Locale.us)

  2. If Locale constant for my country is not present then also need to replace your method
    e.g. my country is India. Locale constant for it is not there. then for getting India's currency's symbol I will change method by getCurrencyInstance(new Locale("en", "IN"))
    image

@Ropi23152023

This comment has been minimized.

Copy link

commented Jul 8, 2017

can someone help me?
I tried running the app I created in android studio 2.3.3 using genymotion android emulator, but there was an error. Error says this

"Error: Failed to complete Gradle execution.
Cause:
CreateProcess error = 5, Access is denied"

Previously I've used genymotion android emulator to run apps made in android studio and have no problem. But this time there is an error like the above, I've looked it up on google but nobody has a case like me

please help....

@iarunsingh07

This comment has been minimized.

Copy link

commented Jul 10, 2017

@Mergen23 thanks mate 👍

@ChadH1971

This comment has been minimized.

Copy link

commented Jul 16, 2017

@karanbawa
image
Click on File and go down to other settings, click on default settings and you will see Auto Import at the bottom of the list. Then click on the check boxes.
I hope this helps. It was hard to find for me as well. I am using an HP laptop with Windows 10 to run Android Studio 2.3.3

@lakshmikantdeshpande

This comment has been minimized.

Copy link

commented Jul 23, 2017

For everyone who's facing this error:
"Call requires API level 24 (current min is 15): android.icu.text.NumberFormat#getCurrencyInstance"

Navigate to the top of the file and delete this line.
import android.icu.text.NumberFormat;

all the best :)

@Da-Tony-W

This comment has been minimized.

Copy link

commented Jul 23, 2017

if you have trouble finding auto import , and you are on windows like me, try go to file->settings->editor->general->auto import

@jiingg

This comment has been minimized.

Copy link

commented Aug 12, 2017

4f5429df5ea6361fa8d3f08dfcdccdf9--programmer-humor-computer-engineering
me rn

@wxy022

This comment has been minimized.

Copy link

commented Aug 30, 2017

I had a problem in the MainActivity.java file.
It did not auto import the class so I did it manually (I'm guessing it was manual because it didn't do it automatically as video stated.

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

On the 3rd line, the 'NumberFormat' text was in red with the comment "Cannot resolve symbol 'NumberFormat'"

@neochaochaos 's comment helped me out.
I had the cursor over the text and typed 'alt+enter'. I tried it multiple times because I didn't know what it was in the beginning, and it actually gave me 2 different popups as below :
20170830_125314
20170830_125330

I used the "Class to Import" box and clicked "NumberFormat (java.text)"

Not entirely sure what it did, but it works now.

@agrawalsam

This comment has been minimized.

Copy link

commented Aug 31, 2017

FOR THOSE WHO ARE UNABLE TO REMOVE ERROR even after following steps press Alt + Enter and select Number Format Java option . Now it will run fine .

@ShaiGotlieb

This comment has been minimized.

Copy link

commented Oct 4, 2017

Works for $ sign in Israel (instead of NIS sign: ₪)

@kumaishi

This comment has been minimized.

Copy link

commented Oct 8, 2017

Just press Alt and Enter to NumberFormat to solve the problem of the error.
well it is work with me .

@Bunny724

This comment has been minimized.

Copy link

commented Oct 20, 2017

My mobile API level is 17 and the minimum API for using the currency method..i want to do it on minimum api how can i do this?

@Bunny724

This comment has been minimized.

Copy link

commented Oct 20, 2017

wahooo I got it...it happens because of wrong import i.e import android.icu.text.NumberFormat;
we should import import java.text.NumberFormat; to remove the error from the number format....

@iamcodder

This comment has been minimized.

Copy link

commented Nov 5, 2017

nice

@Rita1981

This comment has been minimized.

Copy link

commented Nov 10, 2017

big thx to @agrawalsam it work very well

@azioba

This comment has been minimized.

Copy link

commented Nov 10, 2017

Turning auto import On didnt do the magic for me! i had to add import java.text.NumberFormat manualy on top of my .java file

@roborache

This comment has been minimized.

Copy link

commented Nov 16, 2017

Sadly Android studio didn't helped me out when I checked all the important boxes for auto import. I had to do it manually with alt+enter - BUT my question here is , whats the difference between java.text and android.icu.text ???????
image

@Yusufiadi

This comment has been minimized.

Copy link

commented Nov 17, 2017

I got it!

@sauravgupta2800

This comment has been minimized.

Copy link

commented Nov 19, 2017

mine is showing indian rupees i think its because of local time stamp or currency issues.

@c3nk

This comment has been minimized.

Copy link

commented Nov 20, 2017

thank you @azioba it works!

@lukaffm

This comment has been minimized.

Copy link

commented Nov 22, 2017

This action what @roborache posted worked for me.
:)

@kzidan86

This comment has been minimized.

Copy link

commented Nov 26, 2017

I have a problem setting the currency. It shows USD while the language on my computes is set to English UK. I want the currency to show AED (for United Arab Emirates).
Tried using : Currency currency = Currency.getInstance(7842) -> ISO currency code; but the app crashed.
Can someone help

@ecemkusculu

This comment has been minimized.

Copy link

commented Nov 26, 2017

Please help me :(
error
why i get these errors ?

@tubai07

This comment has been minimized.

Copy link

commented Nov 27, 2017

@kzidan86 please change the language of your phone.

@ghost

This comment has been minimized.

Copy link

commented Nov 29, 2017

import java.text.NumberFormat; this line fixed my problem numberformat

@MathiasPujado

This comment has been minimized.

Copy link

commented Dec 7, 2017

Thank you @neochaochaos.

@gaietta80

This comment has been minimized.

Copy link

commented Dec 12, 2017

@GetSetSan thanks for the tip, you solved my problem.

@Wreia

This comment has been minimized.

Copy link

commented Dec 23, 2017

I seem to have a problem. When I turn on this app, Price text View doesn't appear at all. What is wrong?

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.justjava.MainActivity">
    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="Quantity"
            android:textAllCaps="true" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="#000000"
            android:layout_marginBottom="16dp"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="Price"
            android:textAllCaps="true" />

        <TextView
            android:id="@+id/price_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="$0"
            android:textColor="#000000"
            android:textSize="16sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:onClick="submitOrder"
            android:text="Order"
            android:textAllCaps="true" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

package com.example.android.justjava;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends Activity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      /
      public void submitOrder(View view) {
      display(2);
      displayPrice(2
      5);
      }

    /**

    • This method displays the given quantity value on the screen.
      /
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      /
      *
    • 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));
      }
      }
@visht05

This comment has been minimized.

Copy link

commented Dec 26, 2017

Currency symbol is based on the language which you have set in your device. For example in my case in order to turn the currency symbol from $ to Rs, i changed my device's language from English(US) to English(India). hope it helps!

@visht05

This comment has been minimized.

Copy link

commented Dec 26, 2017

@Wreia Instead of $0 try only 0. This would solve your problem!

@ElsaThong

This comment has been minimized.

Copy link

commented Jan 5, 2018

What happen to my XML [_text](url
xml
)

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="quantity"
    android:textSize="16sp"
    android:padding="16dp"
    android:textAllCaps="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:text="0"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:id="@+id/quantity_text_view"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="price"
    android:textSize="16sp"
    android:padding="16dp"
    android:textAllCaps="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:text="$0"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:id="@+id/price_text_view"/>

<Button
    android:id="@+id/button_id"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="order"
    android:layout_marginTop="16sp"
    android:onClick="submitOrder"/>

[

jus java error

](url)

@ghost

This comment has been minimized.

Copy link

commented Jan 7, 2018

This one saved my life 👍

For those having issues with NumberFormat, here are a couple of things you can try -

Go to Help > Find Action > Type "Auto Import" > Press Enter > Tick "Add unambiguous imports on the fly." [Obvious approach]
Click on NumberFormat > Press Alt+Enter > Select NumberFormat (java.text)
Hope this helps.

@kenkingsx19

This comment has been minimized.

Copy link

commented Jan 7, 2018

Please not minding the Location of where someone is or the Language of the Phone, is there any other method one can change the Currency?
Because i had it set programmed to the Dollar sign but on clicking Order it changes to the Euro sign.

Thank you all as i await your answers.

@belic1983

This comment has been minimized.

Copy link

commented Jan 10, 2018

Hi to everyone,

can anyone please help me, I am kind of desperate and don't really know what to do. I follow every step what Katherine does and lately with regards to Java code module addTextViewsPrice android studio tells me when i want to run it through my cell following

00:51 Instant Run performed a full build and install since
the installation on the device does not match the local build on disk.
(Don't show again)

when i click on app just_java on my cell phone, it blinks and goes back to the main screen of the phone,

is there any solution for that or what should i do,

can someone please send me the final code for this module 6 so I can at least verify on my cell phone that it works and learn based on it.

thank you, lukas

@tokagecreative

This comment has been minimized.

Copy link

commented Jan 11, 2018

@ashrafsaleh in MainActivity.java add this line of code import java.text.NumberFormat; below import android.widget.TextView;. This will resolve the problem with NumberFormat

@aniamark

This comment has been minimized.

Copy link

commented Jan 19, 2018

@GetSetSan thank you!

@aydinaytac

This comment has been minimized.

Copy link

commented Jan 29, 2018

@tokagecreative it worked for me too, thank you ;) I think Katherine forgot to tell but after watching the solution video, I saw she didn't forget :)

@ELJOKER77

This comment has been minimized.

Copy link

commented Feb 1, 2018

i Auto Import it and now is work

@m-HARD

This comment has been minimized.

Copy link

commented Feb 6, 2018

@neochaochaos @DarkMindParis Thanks

@abdahma01

This comment has been minimized.

Copy link

commented Feb 9, 2018

untitled

@Mohammed-Abdelhady

This comment has been minimized.

Copy link

commented Feb 10, 2018

new code: +1:
import java.text.NumberFormat;
import java.util.Locale;

/**
     * 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(Locale.US).format(number));
    }
@MasterSami1985

This comment has been minimized.

Copy link

commented Feb 13, 2018

28034429_1989855161289028_1384010428_o

well done Mate

@badrddinb

This comment has been minimized.

Copy link

commented Feb 16, 2018

DONE
image

@Rosariobc

This comment has been minimized.

Copy link

commented Feb 19, 2018

Update: solved. The solution was Alt+Enter and selection of the class to run.

add unambigous on the fly is not working to resolve the NumberFormat error. What should I do? I still have the error
screen shot 2018-02-19 at 2 18 53 pm

@DewofyourYouth

This comment has been minimized.

Copy link

commented Feb 20, 2018

It worked, but even though I changed the preferences I had to manually import java.text.NumberFormat and there's a bunch of squiggly lines everywhere

@MohammedZaid20

This comment has been minimized.

Copy link

commented Feb 23, 2018

Was here

@kelliemariet

This comment has been minimized.

Copy link

commented Mar 1, 2018

My code is OK for both, it shows up fine in the emulator on the screen, but when I open it on my phone it's just a blank screen. Help!

@hudaSust

This comment has been minimized.

Copy link

commented Mar 16, 2018

did it without difficulties

@LailaHussain

This comment has been minimized.

Copy link

commented Mar 22, 2018

Done

@akshaynew2011

This comment has been minimized.

Copy link

commented Mar 24, 2018

Friends I get the price_text_view error inspite adding id in xml file. Please assist

Java Code :

/**

  • IMPORTANT: Make sure you are using the correct package name.
  • This example uses the package name:
  • package com.example.android.justjava
  • If you get an error when copying this code into Android studio, update it to match teh package name found
  • in the project's AndroidManifest.xml file.
    **/

package com.example.aksha.add_button;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • 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));
      }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView QuantityTextView = (TextView) findViewById(R.id.Quantity_text_view);
      QuantityTextView.setText("" + number);
      }

}

XML Code





@saidfuad

This comment has been minimized.

Copy link

commented Apr 7, 2018

@akshaynew2011 Kindly paste in your xml code.

@Lizziemasten88

This comment has been minimized.

Copy link

commented Apr 13, 2018

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="quantity"
android:textSize="16sp"
android:padding="16dp"
android:textAllCaps="true"/>



[

@umeklinks

This comment has been minimized.

Copy link

commented Apr 23, 2018

I discovered that this method has a challenge and could not display the value price for the coffee after requesting you add some annotations.

public void displayPrice (int number) {
`/**

  • After requesting you add some annotations.
    */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
    priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }
    }

So I solved the issue by simply changing
import android.icu.text.NumberFormat;

to

import java.text.NumberFormat;

Then remove the annotaion from the displayPrice method. Which will now look like this...

// This method displays the quantity value on the screen
public void displayPrice (int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}

Thanks a lot.

Below is a screenshot:

ffff

fffffffw

screenshot_2018-04-23-15-52-57

@shoooka

This comment has been minimized.

Copy link

commented May 4, 2018

not work and give me this message
Compilation failed; see the compiler error output for details.
solved. The solution was Alt+Enter and selection of the class to run.

@SaeedAlbaradie

This comment has been minimized.

Copy link

commented May 9, 2018

wow ... It is hard in the begining , but the nice Thing when u see it work on ur Handy..
Thanks

@noslen254

This comment has been minimized.

Copy link

commented May 14, 2018

i cant find preferences help

@Tapendrakmr

This comment has been minimized.

Copy link

commented May 18, 2018

.  For Windows, go to File > Settings > Editor > General > Auto Import
.  For Mac, go to Android Studio > Preferences > Editor > General > Auto Import
@serdoune

This comment has been minimized.

Copy link

commented May 27, 2018

image
I have t preferences

@asmaadahab

This comment has been minimized.

Copy link

commented May 30, 2018

untitled2

@Winga-E

This comment has been minimized.

Copy link

commented May 31, 2018

image

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="Quantity"
    android:textSize="20sp"
    android:textAllCaps="true" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:onClick="decrement"
        android:padding="16dp"
        android:text="-" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="0"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:onClick="increment"
        android:text="+" />

</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="Price"
    android:textSize="20sp"
    android:textColor="@android:color/black"
    android:textAllCaps="true" />

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="$0"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:onClick="submitOrder"
    android:text="Order" />

I actually find A option best.

@austinjerrytech

This comment has been minimized.

Copy link

commented Jun 4, 2018

Hi, I am Austin from Nigeria.
How do i get the currency symbol to show the Naira symbol (apart from hard-coding it using Unicode characters.

@Swethasudarsan

This comment has been minimized.

Copy link

commented Jun 27, 2018

The app doesn't run on my device.
It says just java has stopped working.
I tried it on another device and end up with the same.

@harunaadoga

This comment has been minimized.

Copy link

commented Jul 9, 2018

App works fine on my emulator, showing the dollar sign though.

@medax07

This comment has been minimized.

Copy link

commented Jul 11, 2018

If you want to change from whatever currency that is being displayed to $, just change your device's language to English(United states) under settings.

@070794

This comment has been minimized.

Copy link

commented Jul 11, 2018

hi..
can someone help me? i'm having a problem at TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
its said that variable R might not be initialized. thanks in advance

@1syntra1

This comment has been minimized.

Copy link

commented Jul 17, 2018

@Mergen23 my app is working thanks to your post

@tanwarrohit825

This comment has been minimized.

Copy link

commented Jul 21, 2018

capture

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"

// add your image in it
android:src="@drawable/coffe"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="Quantivty"
    android:textAllCaps="true"
    android:textColor="@android:color/white"

    android:textSize="24sp"
    android:textStyle="normal"
    app:layout_constraintBaseline_toBaselineOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.086"
    app:layout_constraintLeft_toLeftOf="@+id/imageView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.048" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="order"
    android:textAllCaps="true"
    android:textStyle="bold"
    android:onClick="submitOrder"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/imageView"
    app:layout_constraintHorizontal_bias="0.06"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3"
    app:layout_constraintVertical_bias="0.239" />

<TextView
    android:id="@+id/quantity_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"


    android:text="0"
    android:textColor="@android:color/background_light"
    android:textSize="30sp"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.139"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Price"
    android:textAllCaps="true"

    android:textColor="@android:color/background_light"
    android:textSize="25sp"
    android:textStyle="bold"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.893"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.033" />

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text=""
    android:textColor="@android:color/white"
    android:textSize="39sp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.857"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.051"
    tools:text="$0" />
@Manhans

This comment has been minimized.

Copy link

commented Aug 2, 2018

In my case was necessary write the follow code:
import java.text.NumberFormat;

After that was ok.

@KalEl0638

This comment has been minimized.

Copy link

commented Aug 7, 2018

When I set the Auto Import it didn't add in import java.text.NumberFormat; so I did same as @Manhans and typed out the code and all was good after this.

@guandalinivitor

This comment has been minimized.

Copy link

commented Aug 16, 2018

@Manhans I did the same as you

@shaan0708

This comment has been minimized.

Copy link

commented Oct 27, 2018

@ashrafsaleh

  1. Change android.icu.text.NumberFormat; to import java.text.NumberFormat
    2)Build > Clean Project
  2. Build > Rebuild Project
    It will work :)

how i change the line number 1

@mohamed000esmat

This comment has been minimized.

Copy link

commented Dec 7, 2018

- -

@devkudasov

This comment has been minimized.

Copy link

commented Dec 8, 2018

For more universal code You can add displayPrice(0); in the end of onCreate Method!

@ahmedmohamed84

This comment has been minimized.

Copy link

commented Dec 9, 2018

whatsapp image 2018-12-09 at 9 47 29 pm
It works fine

@shankar-shiv

This comment has been minimized.

Copy link

commented Dec 22, 2018

Yup it works fine too 👍 💯

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        display(2);
        displayPrice(2 * 5);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    /**
     * 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));
    }
}
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="#D3D3D3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quantity"
        android:textAllCaps="true"
        android:padding="16dp"/>

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="0"
        android:textColor="#000000"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Price"
        android:textAllCaps="true"/>

    <TextView
        android:id="@+id/price_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textColor="#000000"
        android:textSize="16sp"
        android:text="$0" />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#228b22"
        android:onClick="submitOrder"
        android:layout_margin="16dp"
        android:text="Order"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textAllCaps="true"/>
</LinearLayout>

Feel freeto copy the code above !

@dhairyap99

This comment has been minimized.

Copy link

commented Dec 31, 2018

Mine is coming but my local currency is Indian Rupee. How do I add that symbol??

@ajlivright

This comment has been minimized.

Copy link

commented Feb 16, 2019

Thank you <3

@mritunjaysharma394

This comment has been minimized.