In this tutorial we will make an ElectricityBill calculator which calculates the bill by entering the number of units consumed and the Consumer category according to the slab rates. This tutorial helps you exercise many aspects of the android SDK and app development. Hopefully, you can even use this app on your phone whenever you are interested in your consumption.
STEP 0: Getting Started
This Tutorial assumes that you have setup your basic android-eclipse working environment. You can as well download the source code provided with this tutorial and run your project to see the results, you can modify the code to your liking and analyze the functionality.
STEP 1: Creating the Basic Application Skeleton
Create a new project in eclipse and choose Android
Choose the name of the application to something like: Portable ElectricityBill Calculator
The project name to something like: ElecBill
The package name to something like: com.elecbill
The Android SDK version to something new like 2.3.3
This will generate the basic application skeleton of your project for you.
STEP 2: Modify your Layout
Now browse the Package Explorer in Eclipse and open res/layout/main.xml and change it to something that looks like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:text="@string/unitstxt"
android:id="@+id/textView3"
android:layout_height="wrap_content">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/nill"
android:id="@+id/unitsText">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calcbut"
android:id="@+id/calcbutton">
</Button>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="@string/nill"
android:id="@+id/billTV">
</TextView>
</LinearLayout>
Now we will have a Liner Layout with an EditText item to enter the number of units, a button to calculate the bill on click and a textView to show the bill. Don’t forget to save your changes!
STEP 3: Running the Application For the First Time
It’s time to run the application for the first time. To do so from Eclipse, choose Run->Run or (Ctrl+F11)
STEP 4: Adding Some Code to Control the Behavior
Now It’s time to add some code to implement some actionm when the button is clicked.
Browse through the Package Explorer and open src/com.elecbil/elecbill.java here you will have your class Extending Activity class and an onCreate method.
Next, we get our button, edittext and textview widget items in the Activity by using the finViewById function.
For this we will have to import the following libraries:
import android.widget.Button; import android.widget.EditText; import android.widget.TextView;
Now, we add some code to perform some action when the button is pressed. We use an onClickListener, which registers a callback to be invoked when the view is clicked.
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//action goes here
}
});
Next, we add some action i.e to display what is typed in the edittext in a toast message.
Toast.makeText(ElecBillMain.this, unitsText.getText().toString(), Toast.LENGTH_LONG).show();
Now run the application and when you click the button you will get a toast message displaying the contents of the edittext box.
STEP 5: Adding Some Real Functionality
Here, we add a new function calcBill() which will calculate the bill according to the slab rates as per the Andhra Pradesh Electricity Tariff. Ill leave the designing the method to you. You can use nested if’s and check the slab rates. But, before that we will have to convert the string entered in the edittext into a float and throw a “NumberFormatException” exception if the string entered is not a number. Here we use the valueOf() function of float and pass string obtained from the textbox (here referred by unitsText) as argument. We surround this statement by try-catch and display a toast message saying invalid entry if an exception is caught.
try{
units = Float.valueOf(unitsText.getText().toString());
}catch(NumberFormatException e){
Toast.makeText(ElecBillMain.this, "Invalid Entry", Toast.LENGTH_LONG).show();
}
Obviously the function call to the bill calculate method succeeds the above code and the output is shown in the textview widget something similar to this:
billTV.setText("bill: Rs." +bill);
STEP 5: Adding A Spinner to select the category
<Spinner
android:layout_width="match_parent"
android:id="@+id/spinner1"
android:layout_height="wrap_content">
</Spinner>
Here, we reference spinner1 we have created with the spinner object.
Next, we create an ArrayAdapter named adapter of the CharSequence type.
Then we create a new string array in the strings.xml located in res/values/strings.xml and name it category
finally, we attach the adapter to the spinner using setAdapter method.
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Now, we can add an OnItemSelectedListener and set a public variable spinpos to the current position of the item selected in the spinner widget. We then check for the spinpos value in the calcBill method and perform the corresponding slab calculation.
class MyOnItemSelectedListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
spinpos = pos;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
The final Application looks something like this…
Thats it!!
Thank you for following this tutorial, I hope you have followed all the parts of this tutorial. If you did’nt you can feel free to post your doubts in the comments section. You can also download the source files and run the simulation in proteus or keil uVision.
Download the Source code
Similar topics you might like…
- None Found




love your article ,very informative
Good post. Its realy nice. Many info help me.