andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

My first Google Android Java application


 
       anddev.org - Android Development Community | Android Tutorials | Index -> General
Author Message
WauloK
Developer


Joined: 19 Dec 2007
Posts: 30
Location: Brisbane, Australia

PostPosted: Sat Feb 09, 2008 1:20 pm    Post subject: My first Google Android Java application Reply with quote

Over the last two days I've been working on a small calculations application.
Today I finally finished it! Very Happy
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 Wink



BestValue_screenshot.png
 Description:
Best Value PNG screenshot
 Filesize:  2.9 KB
 Viewed:  827 Time(s)

BestValue_screenshot.png



_________________
AndroidChi - Applications for the Google Android platform.
Back to top
View user's profile Send private message Visit poster's website
WauloK
Developer


Joined: 19 Dec 2007
Posts: 30
Location: Brisbane, Australia

PostPosted: Tue Feb 12, 2008 11:40 pm    Post subject: Reply with quote

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

BestValue.java:
Java:
package com.androidchi.bestvalue;

// Import all the required packages
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

// Main program begins here..
public class BestValue extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        // Listen for button clicks
        Button calcbutton=(Button)findViewById(R.id.calc);
        Button clearbutton=(Button)findViewById(R.id.clear);
        Button aboutbutton=(Button)findViewById(R.id.about);        

        // Code for calc button
        if (calcbutton != null) {
          // Listen for the button being hit
          calcbutton.setOnClickListener(new OnClickListener() {
               public void onClick(View viewParam) {
                    // Find the EditText (where the user can type) that we defined in main.xml
                    EditText price1EditText = (EditText)findViewById(R.id.price1);
                    EditText units1EditText = (EditText)findViewById(R.id.units1);
                    EditText price2EditText = (EditText)findViewById(R.id.price2);
                    EditText units2EditText = (EditText)findViewById(R.id.units2);                       
                    // Make sure the text boxes actually exist
                    if ((price1EditText == null) || (units1EditText == null) || (price2EditText == null) ||
                              (units2EditText == null))
                         {
                              showAlert("Error",0,"Couldn't find one of the text entry EditViews in main.xml","Okay", false);
                         }
                    // Make sure the boxes actually contain numbers to calculate with
                    else if (price1EditText.getText().toString().equals("") || price2EditText.getText().toString().equals("") || units1EditText.getText().toString().equals("") || units2EditText.getText().toString().equals("")) {
                         showAlert("Error",0,"Text boxes may not be left empty!","Okay", false);
                    }
                    else {
                         // If they do, convert the entered Strings to actual numbers for calculating with
                    double price1=Double.parseDouble(price1EditText.getText().toString());
                    double price2=Double.parseDouble(price2EditText.getText().toString());
                    double units1=Double.parseDouble(units1EditText.getText().toString());
                    double units2=Double.parseDouble(units2EditText.getText().toString());
                    
                    // Make sure they aren't dividing by zero!
                    if ( Double.compare(units1, 0.0) == 0 || Double.compare(units2, 0.0) == 0 ) {
                         showAlert("Error",0,"Quantities may not be zero.","Okay", false);
                    } else {
                         // Calculate the values of units per price
                         double value1=(price1/units1);
                         double value2=(price2/units2);
                    
                         // Find the ID of the "result" text field to write results in
                         TextView result=(TextView)findViewById(R.id.result);
                         TextView values=(TextView)findViewById(R.id.valuestext);
                    
                         values.setText("Value #1: " + String.format("%.02f",  value1) + " and Value #2: "+ String.format("%.02f",  value2));
                    
                         // Is value1 the cheapest?
                         if (value1 < value2) {
                              result.setText(getString(R.string.valuebest1));
                         }
                         // Is value2 the cheapest?
                         else if (value2 < value1) {
                              result.setText(getString(R.string.valuebest2));
                         }
                         // They are both the same price per unit
                         else {
                              result.setText(getString(R.string.valuesequal));
                         }
                    }
                         }
                    }
               });            
          }
        // clear button hit
        if (clearbutton != null) {
          clearbutton.setOnClickListener(new OnClickListener() {
               public void onClick(View viewParam) {
                    // find the text boxes
                    EditText price1EditText = (EditText)findViewById(R.id.price1);
                    EditText units1EditText = (EditText)findViewById(R.id.units1);
                    EditText price2EditText = (EditText)findViewById(R.id.price2);
                    EditText units2EditText = (EditText)findViewById(R.id.units2);
                    
                    if ((price1EditText == null) || (units1EditText == null) || (price2EditText == null) ||
                              (units2EditText == null)) {
                         showAlert("Error",0,"Couldn't find one of the text entry EditViews in main.xml","Oops", false); }
                    else {
                         // Find out where result and values text boxes are
                         TextView result=(TextView)findViewById(R.id.result);
                         TextView valuestext=(TextView)findViewById(R.id.valuestext);  
                         // Clear entries in price and units text boxes
                         price1EditText.setText("");
                         units1EditText.setText("");
                         price2EditText.setText("");
                         units2EditText.setText("");
                         // Clear the 'result' text output as well as each text box
                         result.setText("");
                         valuestext.setText("");
                         // Set focus to Price 1 EditText box (put cursor there)
                         price1EditText.requestFocus();
                    }
               }
          });
        }
        // About button hit
        if (aboutbutton != null) {
          aboutbutton.setOnClickListener(new OnClickListener() {
               public void onClick(View viewParam) {
                    // Pop up a window with some info and a button to make it go away
                    showAlert("Best Value by Jason Oakley",0, getString(R.string.abouttext), "Okay, Thanks!", false);
               }
               
          });
        }
     }
}


STRINGS.XML:
XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Best Value</string>
    <string name="app_version">0.1</string>
     <string name="heading">Best Value by AndroidChi.com</string>
     
    <string name="price1"></string>
    <string name="units1"></string>
    <string name="price2"></string>
    <string name="units2"></string>

    <string name="textprice1">Price #1 :</string>
    <string name="textunits1">Qty #1 :</string>
    <string name="textprice2">Price #2 :</string>
    <string name="textunits2">Qty #2 :</string>
   
    <string name="calc">Calc</string>
    <string name="clear">Clear</string>
    <string name="about">About</string>    
   
    <string name="valuebest1">Item #1 is the Best Value!</string>
     <string name="valuebest2">Item #2 is the Best Value!</string>
     <string name="valuesequal">Both have the same value!</string>
    <string name="result"></string>
    <string name="resulttext">Result: </string>   
    <string name="valuestext"></string>
   
    <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>     
</resources>


MAIN.XML:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

          <TextView android:id="@+id/heading"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/heading"
          android:textStyle="bold"
          android:textAlign="center"
          />


        <TextView android:id="@+id/textprice1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textprice1"
            android:layout_below="@id/heading"
            android:layout_marginTop="13px"            
            />

        <EditText android:id="@+id/price1"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:layout_toRight="@id/textprice1"
            android:layout_below="@id/heading"  
            android:layout_marginTop="10px"
            android:layout_marginLeft="5px"                                  
            android:numeric="true"
            android:digits="0123456789."
            android:text=""
            />

        <TextView android:id="@+id/textunits1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textunits1"
            android:layout_below="@id/textprice1"
            android:layout_marginTop="13px"
            android:layout_marginLeft="9px"              
            />

        <EditText android:id="@+id/units1"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:layout_toRight="@id/textunits1"
            android:layout_below="@id/textprice1"  
            android:layout_marginTop="10px"
            android:layout_marginLeft="5px"                    
            android:numeric="true"
            android:text=""
            />

        <TextView android:id="@+id/textprice2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textprice2"
            android:layout_below="@id/textunits1"
            android:layout_marginTop="13px"  
            />

        <EditText android:id="@+id/price2"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:layout_toRight="@id/textprice2"
            android:layout_below="@id/textunits1"  
            android:layout_marginTop="10px"
            android:layout_marginLeft="5px"                      
            android:numeric="true"
            android:digits="0123456789."            
            android:text=""
            />
           
        <TextView android:id="@+id/textunits2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textunits2"
            android:layout_below="@id/textprice2"
            android:layout_marginTop="13px"
            android:layout_marginLeft="9px"              
            />

        <EditText android:id="@+id/units2"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:layout_toRight="@id/textprice2"
            android:layout_below="@id/textprice2"  
            android:layout_marginTop="10px"  
            android:layout_marginLeft="5px"                                
            android:numeric="true"
            android:text=""
            />
                 
        <Button android:id="@+id/calc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/calc"
            android:layout_below="@id/textunits2"
            android:layout_marginTop="13px"
            android:layout_marginLeft="0px"  
            />

        <Button android:id="@+id/clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:layout_toRight="@id/calc"
            android:layout_marginTop="0px"
            android:layout_alignTop="@id/calc"  
            android:layout_marginLeft="5px"
            />
     
        <Button android:id="@+id/about"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/about"
            android:layout_toRight="@id/clear"
            android:layout_marginTop="0px"
            android:layout_alignTop="@id/calc"  
            android:layout_marginLeft="5px"
            />

         <TextView android:id="@+id/valuestext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/valuestext"
            android:layout_below="@id/calc"
            android:layout_marginTop="13px"              
            />
                             
        <TextView android:id="@+id/resulttext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/resulttext"
            android:layout_below="@id/valuestext"
            android:layout_marginTop="13px"  
            />

        <TextView android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/valuestext"            
            android:layout_toRight="@id/resulttext"            
            android:text=""
            android:layout_marginTop="13px"
            android:layout_marginLeft="5px"                        
            />

</RelativeLayout>


If any experienced Java programmers have suggestions, please let me know Smile

_________________
AndroidChi - Applications for the Google Android platform.


Last edited by WauloK on Tue Feb 19, 2008 11:53 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
plusminus
Site Admin


Joined: 14 Nov 2007
Posts: 2067
Location: Germany

PostPosted: Tue Feb 12, 2008 11:58 pm    Post subject: Reply with quote

Hello waulok,

thanks for sharing Smile

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

Regards,
plusminus

_________________

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
WauloK
Developer


Joined: 19 Dec 2007
Posts: 30
Location: Brisbane, Australia

PostPosted: Tue Feb 19, 2008 11:54 am    Post subject: Reply with quote

Updated for the new SDK! Smile
_________________
AndroidChi - Applications for the Google Android platform.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.