In this tutorial we will see how make a alarm application in android device. Android supports alarm services. Here we are using BroadcastReceiver class to listen  for incoming information from user and then use the Toast class to display the alarm message with vibrator. Android Emulator does't make a vibrator we need to make without vibration. In mobile we have to add vibrator code. The following Try It Out demonstrates how you can do this.....

TRY IT OUT Using the AutoCompeteTextView


1.    Using Eclipse, Create an Android project and name it Alarm.

2.    Modify the main.xml file located in the res/layout folder as shown here:

<?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" android:background="@drawable/backrepeat"
android:gravity="center_horizontal">
<EditText android:layout_height="wrap_content" android:id="@+id/time"
android:layout_width="wrap_content" android:hint="Number of seconds"
android:inputType="numberDecimal"></EditText>
<Button android:text="Start Counter" android:id="@+id/ok"
android:onClick="startAlert" android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>

3.    Add the following statements to AlarmActivity.java file:

package com.alarm;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Alarm1Activity extends Activity 
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager)
getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
}

3.    Add the following statements to MyBroadcastReceiver.java file:


package com.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver 
{
public void onReceive(Context context, Intent intent) 
{
Toast.makeText(context, " wake up Moorthy your time is up!!!!.",
Toast.LENGTH_LONG).show();
Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}


4.    Press F11 to debug the application on the Android Emulator. The below screen shown making a alarm within in the second.



Download Code

In this tutorial we will see how we are selecting the date and time in android mobile. Android supports this functionality through the TimePicker and DatePicker views. The following sections show how to make use of these views in your activity...

The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode. The following Try it Out shows you how to use it.

TRY IT OUT  Using the Time&Date Picker View 

1.    Using Eclipse, create an Android  project and name it pickerview.

2.    Modify the main.xml file located in the res/layout folder by adding the following line:

<?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:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="26px"
android:typeface="sans"></TextView>
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="26px"
android:typeface="sans"></TextView>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

3.    Press F11 to debug the application on the Android Emulator. The below shows the Time&Date picker view in action. Besides clicking on the plus(+) and minus(-) button, you can use the numeric keypad on the device to change the hour and minute, and click the AM button to toggle between AM and PM.



4.    Back inn Eclipse, add the following statements to the pickerview.java file:

package com.pickerview;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class pickerview extends Activity 

{
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 2;
private TextView dateDisplay;
private Button pickDate;
private int year, month, day;
private TextView timeDisplay;
private Button pickTime;
private int hours, min;

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
   
dateDisplay = (TextView)findViewById(R.id.textView1);
pickDate = (Button)findViewById(R.id.button1);
pickDate.setOnClickListener( new OnClickListener() 
{
public void onClick(View v) 
{
showDialog(DATE_DIALOG_ID);
}
});
final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
updateDate();
timeDisplay = (TextView)findViewById(R.id.textView2);
pickTime = (Button)findViewById(R.id.button2);
pickTime.setOnClickListener( new OnClickListener () 
{
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
hours = cal.get(Calendar.HOUR);
min = cal.get(Calendar.MINUTE);
updateTime();
}
private void updateTime() 
{
timeDisplay.setText(new StringBuilder().append(hours).append(':')
.append(min));
}
private void updateDate() 
{
dateDisplay.setText(new StringBuilder().append(day).append('-')
.append(month + 1).append('-').append(year));
}
private DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() 
{
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
year = yr;
month = monthOfYear;
day = dayOfMonth;
updateDate();
}
};
private TimePickerDialog.OnTimeSetListener timeListener =
new TimePickerDialog.OnTimeSetListener() 
{
public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
{
hours = hourOfDay;
min = minute;
updateTime();
}
};
protected Dialog onCreateDialog(int id)
{
switch(id) 
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateListener, year, month, day);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeListener, hours, min, false);
}
return null;
}
}

5.    Add the following statements to the android manifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pickerview"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".pickerview"
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 Code

In this tutorial we will see how to send  SMS message from one mobile to another mobile. Using this application, you can send an SMS message to a recipient without user intervention. The following Try It Out shows you how...

TRY IT OUT Sending SMS Messages

1.    Using Eclipse, create a new Android project and name its as sms.

2.    Add the following statements to the main.xml file:

<?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="fill_parent" 
android:layout_height="wrap_content" 
android:text="Enter the phone number of recipient"   />     
<EditText 
android:id="@+id/txtPhoneNo"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content"  />
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content"         
android:text="Message"    />     
<EditText 
android:id="@+id/txtMessage"  
android:layout_width="fill_parent" 
android:layout_height="150px"
android:gravity="top"       />          
<Button 
android:id="@+id/btnSendSMS"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:text="Send SMS"
android:onClick="act"    />    
</LinearLayout>

3.    In the AndroidManifest.xml file, add the following statements:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smsv"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".act"
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>
<uses-permission  android:name="android.permission.SEND_SMS"/>
</manifest>

4.    Add the following statements to the act.java file:

package com.smsv;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class act extends Activity 
{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);        

btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);

btnSendSMS.setOnClickListener(new View.OnClickListener() 
{
public void onClick(View v) 
{                
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();                 
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getBaseContext(), "message has been sent", Toast.LENGTH_SHORT).show();
}
});        
}
}

5.    Press F11 to debug the application on the Android Emulator. Using the Android SDK and AVD Manager, Launch another AVD.

6.    On the first Android Emulator, click Send SMS button to send an SMS message to the second emulator. The right side emulator shows the SMS message received by the second emulator (note the notification bar at the top of the second emulator.


Download Code

In this tutorial i will show how to create a simple listview application. This article is about creating listview.

1.    So now we are creating a project in Eclipse IDE.

1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as  ListviewActivity.
2. Once the project is created open your main activity java file and extend the class from ListActivity.

public class ListviewActivity extends ListActivity 

2.    Now we need a string for display your list item. So now we are implementing array concept. Here the following code look like.

String args[]={"TamilNadu","Bombay","Kerala","Andra","Karnataka","Mangloor","Goa","Delhi"};

3.    In ListView each list item will be an xml layout, so we can customize each list item. Create an XML file under res/layout folder and name it as main.xml and type the following code. This xml layout will be single list item row.
( Right Click on res/layout ⇒ New ⇒ Android XML File)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

4.    Now open your main activity java file and type the following code. In the following code i am importing all xml resources data and storing them in an Array. On the next step i am binding array to ListAdapter.

public class ListviewActivity extends ListActivity 
{
String args[]={"TamilNadu","Bombay","Kerala","Andra","Karnataka","Mangloor","Goa","Delhi"};
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,args);
setListAdapter(ad);
}
public void onListItem(ListView lv,View v,int pos,long id)
{
super.onListItemClick(lv,v,pos,id);
Toast.makeText(getBaseContext(), "you selected at="+args[pos], Toast.LENGTH_LONG).show();
}
}

5.    The final step is to add an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.listview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ListviewActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

6.    Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.


Download Code

This is the first part of the android application tutorial. This application aims to provide an way to show the message when the button clicks.I created the eclipse project and set up a basic interface for the main activity of the application.

In web technology we displayed a message using java script or any other programming concepts. In android we are using onclick listener for making to display message.

setOnClickListener(new View.OnClickListener() 

In android toast is a notification message that pop up, display a certain amount of time, and automatically fades in and out, most people just use it for debugging purpose.

code snippets to create a Toast message :
//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();

//display in long period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();

when a user clicks the button, the android system calls the activity onclick(view) method. The method must be public and accept a view as its only parameter.

public class Toast1Activity extends Activity 
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() 
{  
public void onClick(View arg0) 
{ Toast.makeText(getBaseContext(), "you clicked me", Toast.LENGTH_LONG).show(); }
});
}
}

 

XML layout


Instead of applying an onclicklistener to the button in your activity, you can assign a method to your button in the xml layout using android:onclick attribute.

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="button" />

when a user clicks the button, the android system calls the activity onclick(view) method. The method must be public and accept a view as its only parameter.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button" />
</LinearLayout>

 

AndroidManifest


This manifest.xml file presents essential information about the application to the android system.It names the java packages for the application. The package name serves as a unique identifier for the application. This is the basic application for andrid beginners. It helps to implement different application.

Final Output:




Download Code



This post is to help you understand android development environment and the tools which you are going to use for developing the application. So lets start with basic steps...

1. Download & Install the Android SDK


a. Download the Android SDK 
b. Install/Extract the downloaded SDK
c. Go to the directory where you installed the SDK and open SDK Manager to open Android SDK and AVD Manager.
d. In AVD manager under Avaliable Packages you can see different versions of SDK’s 


Select SDK Platform tools and one of the version of SDK and click on install.

2. Downloading Eclipse Software


Although there are lot of IDE out there Eclipse is recommended IDE which will give you best support for Android app development.

You can download Eclipse IDE from here

3. Installing Android Development Toolkit (ADT) plug-in


a. Open Eclipse s/w and under Help -> Install New Software…
b. Now you will see a window which allows you to install new plug-in
c. Click on Add button and in Name and in Location give the link 
https://dl-ssl.google.com/android/eclipse/ and proceed with further steps.




4. Creating Sample Project


Creating a sample android project involves very few steps
a. In your Eclipse IDE go to File -> Android Project
b. Give Project Name, Select Build Target, Application Name, Package Name, Activity Name, Min SDK version and click Finish





Now you can see bunch of files created in the project explorer.




5. Creating New Android Virtual Device (AVD)


The AVD is an emulator which provides you android hardware and software environment to test application on computer.

In Eclipse open SDK Manager under Windows -> Android SDK and AVD Manager.




Click on AVD Manager.




Give Name, Select Target give SD Card size and click on Create AVD.




Now a new AVD is created with the specification you provided and Close the Android SDK and AVD Manager.

6. Running the Project


Once you successfully created AVD you are ready to test your application.

Right Click on the project in Package Explore and click on Run As -> Android Application.




Now you can see an AVD is opened and booting up.( It will take much time to launch AVD for the first time)

Once the AVD started you can see the output on the AVD screen.