Green Budget – An android application to track your budget

Hi all… I am planning to create a new free and open source android application called it “Green Budget”. Here i have provided sample user interfaces and i like to gain your suggestions about these interfaces. Based on that i can modify these colors and interfaces. This project already available on GitHub and if you want to download this application with source codes you can see on here. Application is still on the initial step and only the UI is being implemented by myself.

Sending SMS Messages in Android

This is a simple example can be used to send SMS in android devices. In here i have created two emulators and i will send sms from one emulator to another. I have not tested the code in a real device and i am sure it will work without any errors. This example extracted from the   http://stackoverflow.com/questions/5944345/sending-sms-in-android.

Java code looks like below

/*
 * Source : http://stackoverflow.com/questions/5944345/sending-sms-in-android
 * 
 * */

package com.sms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Main extends Activity {

	private Button btnSend;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnSend = (Button)findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {

				sendSMS("5556", "test");

			}
		});

    }

	/**
	 * This method can be used to send SMS from android
	 * @param phoneNumber
	 * @param message
	 */
	private void sendSMS(final String phoneNumber,String message)
    {
		String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(Main.this, 0,
                new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(Main.this, 0,
                new Intent(DELIVERED), 0);

            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(Main.this, "SMS successfully sent to "+phoneNumber,
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(Main.this, "Generic failure",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(Main.this, "No service",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(Main.this, "Null PDU",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }

            }, new IntentFilter(SENT));

            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(Main.this, "SMS delivered",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(Main.this, "SMS not delivered",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            }, new IntentFilter(DELIVERED));

    	SmsManager sms = SmsManager.getDefault();
    	sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }
}

And do not forget to add permission into your AndroidManifest.xml file.

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

and finally your AndroidManifest.xml file looks like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sms"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Download the complete project from here

Download

How to use OnClickListener in android

In android application development  we can use OnClickListener in two ways. In first method is add OnClickListenerto each button individually. Imagine that you have several buttons like below image.

Now you want to use OnClickListener function in each button. Code snippet can be shown as

package com.lister;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class Main extends Activity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1);
        btn1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast pieceToast= Toast.makeText(getApplicationContext(), "Image Button One Clicked", Toast.LENGTH_SHORT);
				pieceToast.show();		

			}
		});

        ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
        btn2.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast pieceToast= Toast.makeText(getApplicationContext(), "Image Button Two Clicked", Toast.LENGTH_SHORT);
				pieceToast.show();		

			}
		});

        ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3);
        btn3.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast pieceToast= Toast.makeText(getApplicationContext(), "Image Button Three Clicked", Toast.LENGTH_SHORT);
				pieceToast.show();		

			}
		});

        ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4);

        ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5);

    	}

	}

As my example you can see you want to use OnClickListener in each and every button you have to implement lot of codes. But when you implement OnClickListener interface to your Action class then you can minimize your code like below

package com.lister;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1);
        btn1.setOnClickListener(this);

        ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
        btn2.setOnClickListener(this);

        ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3);
        btn3.setOnClickListener(this);

        ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4);
        btn4.setOnClickListener(this);

        ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5);
        btn5.setOnClickListener(this);

    }

	public void onClick(View v) {

		Toast pieceToast=null;

		switch (v.getId()) {

		case R.id.imageButton1:
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button One Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();			
			break;

		case R.id.imageButton2:
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button Two Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();
			break;

		case R.id.imageButton3:
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button Three Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();
			break;

		case R.id.imageButton4:			
			pieceToast= Toast.makeText(getApplicationContext(), "Image Button Four Clicked", Toast.LENGTH_SHORT);
			pieceToast.show();
			break;

		default:
			break;
		}

	}

	}

This is very good practice when we are more button in our application. Not only image button you can use any GUI widget and use this technique when you are coding your Android application.