My first Google Android Java application

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

My first Google Android Java application

Postby WauloK » Sat Feb 09, 2008 1:20 pm

Over the last two days I've been working on a small calculations application.
Today I finally finished it! :D
It's late at the moment so I will soon be uploading information on it.
I hope to put lots of info and descriptions to help people learn from it, on my website in my signature.

For now, here's a sneak preview ;)
Attachments
BestValue_screenshot.png
Best Value PNG screenshot
BestValue_screenshot.png (2.9 KiB) Viewed 15906 times
AndroidChi - Applications for the Google Android platform.
User avatar
WauloK
Developer
Developer
 
Posts: 30
Joined: Wed Dec 19, 2007 4:32 am
Location: Brisbane, Australia

Top

Postby WauloK » Tue Feb 12, 2008 11:40 pm

Since my forum is having issues posting code I will paste my application source here for others to use.

BestValue.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.androidchi.bestvalue;
  2.  
  3.  
  4.  
  5. // Import all the required packages
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.view.View;
  12.  
  13. import android.view.View.OnClickListener;
  14.  
  15. import android.widget.Button;
  16.  
  17. import android.widget.EditText;
  18.  
  19. import android.widget.TextView;
  20.  
  21.  
  22.  
  23. // Main program begins here..
  24.  
  25. public class BestValue extends Activity {
  26.  
  27.     /** Called when the activity is first created. */
  28.  
  29.     @Override
  30.  
  31.     public void onCreate(Bundle icicle) {
  32.  
  33.         super.onCreate(icicle);
  34.  
  35.         setContentView(R.layout.main);
  36.  
  37.        
  38.  
  39.         // Listen for button clicks
  40.  
  41.         Button calcbutton=(Button)findViewById(R.id.calc);
  42.  
  43.         Button clearbutton=(Button)findViewById(R.id.clear);
  44.  
  45.         Button aboutbutton=(Button)findViewById(R.id.about);        
  46.  
  47.  
  48.  
  49.         // Code for calc button
  50.  
  51.         if (calcbutton != null) {
  52.  
  53.                 // Listen for the button being hit
  54.  
  55.                 calcbutton.setOnClickListener(new OnClickListener() {
  56.  
  57.                         public void onClick(View viewParam) {
  58.  
  59.                                 // Find the EditText (where the user can type) that we defined in main.xml
  60.  
  61.                                 EditText price1EditText = (EditText)findViewById(R.id.price1);
  62.  
  63.                                 EditText units1EditText = (EditText)findViewById(R.id.units1);
  64.  
  65.                                 EditText price2EditText = (EditText)findViewById(R.id.price2);
  66.  
  67.                                 EditText units2EditText = (EditText)findViewById(R.id.units2);                         
  68.  
  69.                                 // Make sure the text boxes actually exist
  70.  
  71.                                 if ((price1EditText == null) || (units1EditText == null) || (price2EditText == null) ||
  72.  
  73.                                                 (units2EditText == null))
  74.  
  75.                                         {
  76.  
  77.                                                 showAlert("Error",0,"Couldn't find one of the text entry EditViews in main.xml","Okay", false);
  78.  
  79.                                         }
  80.  
  81.                                 // Make sure the boxes actually contain numbers to calculate with
  82.  
  83.                                 else if (price1EditText.getText().toString().equals("") || price2EditText.getText().toString().equals("") || units1EditText.getText().toString().equals("") || units2EditText.getText().toString().equals("")) {
  84.  
  85.                                         showAlert("Error",0,"Text boxes may not be left empty!","Okay", false);
  86.  
  87.                                 }
  88.  
  89.                                 else {
  90.  
  91.                                         // If they do, convert the entered Strings to actual numbers for calculating with
  92.  
  93.                                 double price1=Double.parseDouble(price1EditText.getText().toString());
  94.  
  95.                                 double price2=Double.parseDouble(price2EditText.getText().toString());
  96.  
  97.                                 double units1=Double.parseDouble(units1EditText.getText().toString());
  98.  
  99.                                 double units2=Double.parseDouble(units2EditText.getText().toString());
  100.  
  101.                                
  102.  
  103.                                 // Make sure they aren't dividing by zero!
  104.  
  105.                                 if ( Double.compare(units1, 0.0) == 0 || Double.compare(units2, 0.0) == 0 ) {
  106.  
  107.                                         showAlert("Error",0,"Quantities may not be zero.","Okay", false);
  108.  
  109.                                 } else {
  110.  
  111.                                         // Calculate the values of units per price
  112.  
  113.                                         double value1=(price1/units1);
  114.  
  115.                                         double value2=(price2/units2);
  116.  
  117.                                
  118.  
  119.                                         // Find the ID of the "result" text field to write results in
  120.  
  121.                                         TextView result=(TextView)findViewById(R.id.result);
  122.  
  123.                                         TextView values=(TextView)findViewById(R.id.valuestext);
  124.  
  125.                                
  126.  
  127.                                         values.setText("Value #1: " + String.format("%.02f",  value1) + " and Value #2: "+ String.format("%.02f",  value2));
  128.  
  129.                                
  130.  
  131.                                         // Is value1 the cheapest?
  132.  
  133.                                         if (value1 < value2) {
  134.  
  135.                                                 result.setText(getString(R.string.valuebest1));
  136.  
  137.                                         }
  138.  
  139.                                         // Is value2 the cheapest?
  140.  
  141.                                         else if (value2 < value1) {
  142.  
  143.                                                 result.setText(getString(R.string.valuebest2));
  144.  
  145.                                         }
  146.  
  147.                                         // They are both the same price per unit
  148.  
  149.                                         else {
  150.  
  151.                                                 result.setText(getString(R.string.valuesequal));
  152.  
  153.                                         }
  154.  
  155.                                 }
  156.  
  157.                                 }
  158.  
  159.                         }
  160.  
  161.                 });            
  162.  
  163.         }
  164.  
  165.         // clear button hit
  166.  
  167.         if (clearbutton != null) {
  168.  
  169.                 clearbutton.setOnClickListener(new OnClickListener() {
  170.  
  171.                         public void onClick(View viewParam) {
  172.  
  173.                                 // find the text boxes
  174.  
  175.                                 EditText price1EditText = (EditText)findViewById(R.id.price1);
  176.  
  177.                                 EditText units1EditText = (EditText)findViewById(R.id.units1);
  178.  
  179.                                 EditText price2EditText = (EditText)findViewById(R.id.price2);
  180.  
  181.                                 EditText units2EditText = (EditText)findViewById(R.id.units2);
  182.  
  183.                                
  184.  
  185.                                 if ((price1EditText == null) || (units1EditText == null) || (price2EditText == null) ||
  186.  
  187.                                                 (units2EditText == null)) {
  188.  
  189.                                         showAlert("Error",0,"Couldn't find one of the text entry EditViews in main.xml","Oops", false); }
  190.  
  191.                                 else {
  192.  
  193.                                         // Find out where result and values text boxes are
  194.  
  195.                                         TextView result=(TextView)findViewById(R.id.result);
  196.  
  197.                                         TextView valuestext=(TextView)findViewById(R.id.valuestext);  
  198.  
  199.                                         // Clear entries in price and units text boxes
  200.  
  201.                                         price1EditText.setText("");
  202.  
  203.                                         units1EditText.setText("");
  204.  
  205.                                         price2EditText.setText("");
  206.  
  207.                                         units2EditText.setText("");
  208.  
  209.                                         // Clear the 'result' text output as well as each text box
  210.  
  211.                                         result.setText("");
  212.  
  213.                                         valuestext.setText("");
  214.  
  215.                                         // Set focus to Price 1 EditText box (put cursor there)
  216.  
  217.                                         price1EditText.requestFocus();
  218.  
  219.                                 }
  220.  
  221.                         }
  222.  
  223.                 });
  224.  
  225.         }
  226.  
  227.         // About button hit
  228.  
  229.         if (aboutbutton != null) {
  230.  
  231.                 aboutbutton.setOnClickListener(new OnClickListener() {
  232.  
  233.                         public void onClick(View viewParam) {
  234.  
  235.                                 // Pop up a window with some info and a button to make it go away
  236.  
  237.                                 showAlert("Best Value by Jason Oakley",0, getString(R.string.abouttext), "Okay, Thanks!", false);
  238.  
  239.                         }
  240.  
  241.                        
  242.  
  243.                 });
  244.  
  245.         }
  246.  
  247.      }
  248.  
  249. }
Parsed in 0.091 seconds, using GeSHi 1.0.8.4


STRINGS.XML:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <string name="app_name">Best Value</string>
  6.  
  7.     <string name="app_version">0.1</string>
  8.  
  9.         <string name="heading">Best Value by AndroidChi.com</string>
  10.  
  11.        
  12.  
  13.     <string name="price1"></string>
  14.  
  15.     <string name="units1"></string>
  16.  
  17.     <string name="price2"></string>
  18.  
  19.     <string name="units2"></string>
  20.  
  21.  
  22.  
  23.     <string name="textprice1">Price #1 :</string>
  24.  
  25.     <string name="textunits1">Qty #1 :</string>
  26.  
  27.     <string name="textprice2">Price #2 :</string>
  28.  
  29.     <string name="textunits2">Qty #2 :</string>
  30.  
  31.    
  32.  
  33.     <string name="calc">Calc</string>
  34.  
  35.     <string name="clear">Clear</string>
  36.  
  37.     <string name="about">About</string>    
  38.  
  39.    
  40.  
  41.     <string name="valuebest1">Item #1 is the Best Value!</string>
  42.  
  43.         <string name="valuebest2">Item #2 is the Best Value!</string>
  44.  
  45.         <string name="valuesequal">Both have the same value!</string>
  46.  
  47.     <string name="result"></string>
  48.  
  49.     <string name="resulttext">Result: </string>
  50.  
  51.     <string name="valuestext"></string>
  52.  
  53.    
  54.  
  55.     <string name="abouttext">Enter two prices and two quantity amounts and Best Value will tell you which price gives the best value for your money. Visit www.AndroidChi.com for more Android applications!</string>      
  56.  
  57. </resources>
  58.  
  59.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


MAIN.XML:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="fill_parent"
  8.  
  9.    >
  10.  
  11.                 <TextView android:id="@+id/heading"
  12.  
  13.                 android:layout_width="fill_parent"
  14.  
  15.                 android:layout_height="wrap_content"
  16.  
  17.                 android:text="@string/heading"
  18.  
  19.                 android:textStyle="bold"
  20.  
  21.                 android:textAlign="center"
  22.  
  23.                 />
  24.  
  25.  
  26.  
  27.         <TextView android:id="@+id/textprice1"
  28.  
  29.            android:layout_width="wrap_content"
  30.  
  31.            android:layout_height="wrap_content"
  32.  
  33.            android:text="@string/textprice1"
  34.  
  35.            android:layout_below="@id/heading"
  36.  
  37.            android:layout_marginTop="13px"            
  38.  
  39.            />
  40.  
  41.         <EditText android:id="@+id/price1"
  42.  
  43.            android:layout_width="100px"
  44.  
  45.            android:layout_height="wrap_content"
  46.  
  47.            android:layout_toRight="@id/textprice1"
  48.  
  49.            android:layout_below="@id/heading"  
  50.  
  51.            android:layout_marginTop="10px"
  52.  
  53.            android:layout_marginLeft="5px"                                  
  54.  
  55.            android:numeric="true"
  56.  
  57.            android:digits="0123456789."
  58.  
  59.            android:text=""
  60.  
  61.            />
  62.  
  63.         <TextView android:id="@+id/textunits1"
  64.  
  65.            android:layout_width="wrap_content"
  66.  
  67.            android:layout_height="wrap_content"
  68.  
  69.            android:text="@string/textunits1"
  70.  
  71.            android:layout_below="@id/textprice1"
  72.  
  73.            android:layout_marginTop="13px"
  74.  
  75.            android:layout_marginLeft="9px"              
  76.  
  77.            />
  78.  
  79.         <EditText android:id="@+id/units1"
  80.  
  81.            android:layout_width="100px"
  82.  
  83.            android:layout_height="wrap_content"
  84.  
  85.            android:layout_toRight="@id/textunits1"
  86.  
  87.            android:layout_below="@id/textprice1"  
  88.  
  89.            android:layout_marginTop="10px"
  90.  
  91.            android:layout_marginLeft="5px"                    
  92.  
  93.            android:numeric="true"
  94.  
  95.            android:text=""
  96.  
  97.            />
  98.  
  99.         <TextView android:id="@+id/textprice2"
  100.  
  101.            android:layout_width="wrap_content"
  102.  
  103.            android:layout_height="wrap_content"
  104.  
  105.            android:text="@string/textprice2"
  106.  
  107.            android:layout_below="@id/textunits1"
  108.  
  109.            android:layout_marginTop="13px"  
  110.  
  111.            />
  112.  
  113.         <EditText android:id="@+id/price2"
  114.  
  115.            android:layout_width="100px"
  116.  
  117.            android:layout_height="wrap_content"
  118.  
  119.            android:layout_toRight="@id/textprice2"
  120.  
  121.            android:layout_below="@id/textunits1"  
  122.  
  123.            android:layout_marginTop="10px"
  124.  
  125.            android:layout_marginLeft="5px"                      
  126.  
  127.            android:numeric="true"
  128.  
  129.            android:digits="0123456789."            
  130.  
  131.            android:text=""
  132.  
  133.            />            
  134.  
  135.         <TextView android:id="@+id/textunits2"
  136.  
  137.            android:layout_width="wrap_content"
  138.  
  139.            android:layout_height="wrap_content"
  140.  
  141.            android:text="@string/textunits2"
  142.  
  143.            android:layout_below="@id/textprice2"
  144.  
  145.            android:layout_marginTop="13px"
  146.  
  147.            android:layout_marginLeft="9px"              
  148.  
  149.            />
  150.  
  151.         <EditText android:id="@+id/units2"
  152.  
  153.            android:layout_width="100px"
  154.  
  155.            android:layout_height="wrap_content"
  156.  
  157.            android:layout_toRight="@id/textprice2"
  158.  
  159.            android:layout_below="@id/textprice2"  
  160.  
  161.            android:layout_marginTop="10px"  
  162.  
  163.            android:layout_marginLeft="5px"                                
  164.  
  165.            android:numeric="true"
  166.  
  167.            android:text=""
  168.  
  169.            />                  
  170.  
  171.         <Button android:id="@+id/calc"
  172.  
  173.            android:layout_width="wrap_content"
  174.  
  175.            android:layout_height="wrap_content"
  176.  
  177.            android:text="@string/calc"
  178.  
  179.            android:layout_below="@id/textunits2"
  180.  
  181.            android:layout_marginTop="13px"
  182.  
  183.            android:layout_marginLeft="0px"  
  184.  
  185.            />
  186.  
  187.         <Button android:id="@+id/clear"
  188.  
  189.            android:layout_width="wrap_content"
  190.  
  191.            android:layout_height="wrap_content"
  192.  
  193.            android:text="@string/clear"
  194.  
  195.            android:layout_toRight="@id/calc"
  196.  
  197.            android:layout_marginTop="0px"
  198.  
  199.            android:layout_alignTop="@id/calc"  
  200.  
  201.            android:layout_marginLeft="5px"
  202.  
  203.            />      
  204.  
  205.         <Button android:id="@+id/about"
  206.  
  207.            android:layout_width="wrap_content"
  208.  
  209.            android:layout_height="wrap_content"
  210.  
  211.            android:text="@string/about"
  212.  
  213.            android:layout_toRight="@id/clear"
  214.  
  215.            android:layout_marginTop="0px"
  216.  
  217.            android:layout_alignTop="@id/calc"  
  218.  
  219.            android:layout_marginLeft="5px"
  220.  
  221.            />
  222.  
  223.          <TextView android:id="@+id/valuestext"
  224.  
  225.            android:layout_width="wrap_content"
  226.  
  227.            android:layout_height="wrap_content"
  228.  
  229.            android:text="@string/valuestext"
  230.  
  231.            android:layout_below="@id/calc"
  232.  
  233.            android:layout_marginTop="13px"              
  234.  
  235.            />                              
  236.  
  237.         <TextView android:id="@+id/resulttext"
  238.  
  239.            android:layout_width="wrap_content"
  240.  
  241.            android:layout_height="wrap_content"
  242.  
  243.            android:text="@string/resulttext"
  244.  
  245.            android:layout_below="@id/valuestext"
  246.  
  247.            android:layout_marginTop="13px"  
  248.  
  249.            />
  250.  
  251.         <TextView android:id="@+id/result"
  252.  
  253.            android:layout_width="wrap_content"
  254.  
  255.            android:layout_height="wrap_content"
  256.  
  257.            android:layout_below="@id/valuestext"            
  258.  
  259.            android:layout_toRight="@id/resulttext"            
  260.  
  261.            android:text=""
  262.  
  263.            android:layout_marginTop="13px"
  264.  
  265.            android:layout_marginLeft="5px"                        
  266.  
  267.            />
  268.  
  269. </RelativeLayout>
  270.  
  271.  
Parsed in 0.028 seconds, using GeSHi 1.0.8.4


If any experienced Java programmers have suggestions, please let me know :)
Last edited by WauloK on Tue Feb 19, 2008 11:53 am, edited 1 time in total.
AndroidChi - Applications for the Google Android platform.
User avatar
WauloK
Developer
Developer
 
Posts: 30
Joined: Wed Dec 19, 2007 4:32 am
Location: Brisbane, Australia

Postby plusminus » Tue Feb 12, 2008 11:58 pm

Hello waulok,

thanks for sharing :)

I myself never check if the Views were actually found from XML, I just assume it :roll:
If they don't I receive a NullPointerException what foces me to review the code.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2696
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby WauloK » Tue Feb 19, 2008 11:54 am

Updated for the new SDK! :)
AndroidChi - Applications for the Google Android platform.
User avatar
WauloK
Developer
Developer
 
Posts: 30
Joined: Wed Dec 19, 2007 4:32 am
Location: Brisbane, Australia

Postby beeshop » Mon Sep 08, 2008 10:55 pm

Hi, is there a direct way to refresh a Activity, with a single button?
I've constructed all the screen dinamically, and nw I want to refresh the activity. Is that possible, wihthout setting text "" component by component?
beeshop
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 12, 2008 9:13 am

Postby plusminus » Tue Sep 09, 2008 2:12 am

Hm, you could do like the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent refresh = new Intent(this, ActivityToRefres.class);
  2.  
  3. startActivity(refresh);
  4.  
  5. this.finish();
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


I'm not sure, but maybe you can 'clean' the ContentView and set it back again...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2696
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests