@ home.xml Layout: This layout is the welcome layout for Quick Calculator application. In this layout, a wallpaper is set as background of the layout. While we start this application, this layout runs for 3 seconds.
Listing 1: home.xml layout
Figure 1: Quick Calculator
Java Class Files:
Every Android project contains a "res" (reources) folder. This folder is meant to comprise of number of subfolders.
Listing 2: calc.xml
Figure 2: Quick Calculator
In this layout one EditText is used as display and rest of object are button objects, which are labeled from 1 to 0 natural numbers, mathematical operations, equal button, and reset button.
Two java classes are created in order to manage these two layouts for this application, the first class named home class run home.xml layout for 3 seconds using Thread concept and loads the next layout after 3 seconds by closing the running layout (home.xml).
Note: Layout is closed by calling method finish() and other layout is loaded using new Intent mechanism.
Listing 3: Home class defined in the Home.java file.
This class extends an Activity class. Two methods of Activity class are overridden in this class, which are onCreate(Bundle) and onPause(). onCreate() method defines, which layout is to be loaded using method: setContentView(R.layout.home);
Next, it defines a new thread, which runs the layout for 3 seconds and load next activity layout by starting a new activity: startActivity(new Intent(“com.one.slate.CALC”));
Before starting a new activity, we should close running activity, if it will not be used in future. This can be achieved using onPause() method.
Listing 3: Calculator class defind into the Calc.java file.
This class describe all the complete functionality for a calculator. This class extends Activity class and implements method of View.OnClickListener interface.
- onCreate(Bundle) method loads the calc.xml layout.
- operation() method define all the basic operation for calculator.
- onClick() method is implemented of OnClickListener interface, which handle all the clicking buttons of calculator application.
- These all buttons clicks are managed by switch case conditional statement, where case represents the layout attributes id and describe their functionality.
- setOnClickListener(this) points out click event defined as a switch case in the onClick() method respectively.
Listing 4: XML : AndroidManifest.xml
The AndroidManifest.xml file is the main configuration file for Quick Calculator android application. This loads home layout by default using class Home.class, whose parameters are set into the activity name “.Home”. The next activity defines, how to load next layout (calc). This layout is loaded by Calc.class defined as parameters in this second activity.
These two activities are enclosed into the main xml tag , which describes information about the application representing, when an application starts.
Read more: http://mrbool.com/how-to-create-a-calculator-app-for-android/28100#ixzz31OB9bECx