Introduction

Today I will explain how to work with an SMS application in Android. I made my own SMS apps. I will tell you how they work and how they are made. In this application I used a layout in which I set many type layouts, all layouts and views are shown here.
In this application, I have used many activity classes for easy coding. Since you know how to connect activity to another, I also connect them in the manner shown here.
For this application you must have some clip art, you can save it in the drawable directory as shown below.
drawablimage.jpg
Step 1
Create a new project as shown below.
smsnew.jpg
Step 2
Create a "create_msg_layout.xml" file as in the following coding.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:layout_alignParentLeft="true"
  10. android:layout_alignParentTop="true"
  11. android:layout_marginTop="1dp" >
  12. <TextView
  13. android:id="@+id/textView1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="To " />
  17. <EditText
  18. android:id="@+id/edSendNo"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:ems="10"
  22. android:inputType="number" >
  23. <requestFocus />
  24. </EditText>
  25. <Button
  26. android:id="@+id/btContactFtech"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text=" "
  30. android:background="@drawable/contact" />
  31. </LinearLayout>
  32. <LinearLayout
  33. android:layout_width="match_parent"
  34. android:layout_height="360dp"
  35. android:layout_above="@+id/linearLayout1"
  36. android:layout_alignParentLeft="true"
  37. android:layout_alignParentTop="true"
  38. android:layout_marginTop="50dp" >
  39. <EditText
  40. android:id="@+id/edSendText"
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content"
  43. android:ems="10"
  44. android:inputType="textMultiLine" />
  45. </LinearLayout>
  46. <LinearLayout
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:layout_alignParentBottom="true"
  50. android:layout_centerHorizontal="true" >
  51. <Button
  52. android:id="@+id/btSend"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:text="Send" />
  56. </LinearLayout>
  57. </RelativeLayout>
Step 3
Create a "inbox.xml" file as in the following coding.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:scrollbars="vertical">
  7. <TextView
  8. android:id="@+id/tvName"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"
  13. android:layout_marginLeft="17dp"
  14. android:text=""
  15. android:textAppearance="?android:attr/textAppearanceLarge" />
  16. <TextView
  17. android:id="@+id/tvDate"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentLeft="true"
  21. android:layout_below="@+id/tvName"
  22. android:text=""
  23. android:textAppearance="?android:attr/textAppearanceMedium" />
  24. <TextView
  25. android:id="@+id/tvTime"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignBaseline="@+id/tvDate"
  29. android:layout_alignBottom="@+id/tvDate"
  30. android:layout_marginLeft="34dp"
  31. android:layout_toRightOf="@+id/tvDate"
  32. android:text=""
  33. android:textAppearance="?android:attr/textAppearanceMedium" />
  34. <TextView
  35. android:id="@+id/tvSmallMsgView"
  36. android:layout_width="match_parent"
  37. android:layout_height="20dp"
  38. android:layout_alignParentLeft="true"
  39. android:layout_below="@+id/tvDate"
  40. android:text=""
  41. android:textAppearance="?android:attr/textAppearanceSmall" />
  42. </RelativeLayout>
Step 4
Create a "list_sms_layout.xml" file, you will see the coding of this file in my attachment.
Step 5
Create a "sent_item_list.xml" file, you will see the coding of this file in my attachment.
Step 6
Create a "view_msg_layout.xml" file, you will see the coding of this file in my attachment.
Step 7
Now open the activity file "MainActivity.java" and update it with the following coding.
  1. package c.him.smsapll;
  2. import android.app.ListActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.ListView;
  8. import android.widget.Toast;
  9. public class MainActivity extends ListActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. final String[] listItem = {"Create Message","Inbox","Send Item"};
  14. /* ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_sms_layout,R.id.textView1,listItem);
  15. setListAdapter(adapter);*/
  16. setListAdapter(new SmsArrayAdapter(this, listItem));
  17. }
  18. @Override
  19. protected void onListItemClick(ListView l, View v, int position, long id) {
  20. // TODO Auto-generated method stub
  21. super.onListItemClick(l, v, position, id);
  22. switch (position) {
  23. case 0:
  24. Intent i1 = new Intent(this,Create_msg.class);
  25. startActivity(i1);
  26. break;
  27. case 1:
  28. Intent i2 = new Intent(this,Inbox.class);
  29. startActivity(i2);
  30. break;
  31. case 2:
  32. Intent i3 = new Intent(this,Sent_item.class);
  33. startActivity(i3);
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. }
Step 8
Create a new Java file "CreateMSG.java" as in the following coding.
  1. package c.him.smsapll;
  2. import java.util.ArrayList;
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.content.ContentValues;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.inputmethodservice.Keyboard.Key;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.provider.ContactsContract.CommonDataKinds.Phone;
  12. import android.telephony.SmsManager;
  13. import android.view.KeyEvent;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.view.View.OnKeyListener;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.Toast;
  20. public class Create_msg extends Activity {
  21. String[] numberlist = new String[20];
  22. int numberlist_index=0;
  23. EditText edWrite_no,edWrite_text;
  24. Button bSend,bFetchContact;
  25. String myEnterNumber="";
  26. boolean isNumericEntered = false;
  27. static Boolean control=false;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.create_msg_layout);
  32. edWrite_no = (EditText)findViewById(R.id.edSendNo);
  33. edWrite_text=(EditText)findViewById(R.id.edSendText);
  34. edWrite_no.setOnKeyListener(new OnKeyListener() {
  35. @Override
  36. public boolean onKey(View v, int keyCode, KeyEvent event) {
  37. if(control==false)
  38. {
  39. control=true;
  40. }
  41. else
  42. {
  43. control=false;
  44. char pressedKey = (char)event.getUnicodeChar();
  45. if(pressedKey=='0'||pressedKey=='1'||pressedKey=='2'||pressedKey=='3'||pressedKey=='4'||pressedKey=='5'||pressedKey=='6'||pressedKey=='7'||pressedKey=='8'||pressedKey=='9')
  46. {
  47. myEnterNumber =myEnterNumber + pressedKey;
  48. isNumericEntered=true;
  49. }
  50. }
  51. return false;
  52. }
  53. });
  54. }
  55. @Override
  56. protected void onResume() {
  57. // TODO Auto-generated method stub
  58. super.onResume();
  59. }
  60. protected void onStart() {
  61. super.onStart();
  62. bSend=(Button)findViewById(R.id.btSend);
  63. bFetchContact=(Button)findViewById(R.id.btContactFtech);
  64. bFetchContact.setOnClickListener(new OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. Uri uri = Uri.parse("content://contacts");
  68. Intent pickContactIntent = new Intent(Intent.ACTION_PICK, uri);
  69. pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
  70. startActivityForResult(pickContactIntent, 1);
  71. }
  72. });
  73. bSend.setOnClickListener(new OnClickListener() {
  74. @Override
  75. public void onClick(View v) {
  76. String s = edWrite_no.getText().toString();
  77. Check_addmyNumber();
  78. try
  79. {
  80. if(numberlist_index <= 0)
  81. {
  82. Toast.makeText(Create_msg.this, "Please, Either enter number or select number from contact.", Toast.LENGTH_SHORT).show();
  83. }
  84. else
  85. while(numberlist_index > 0)
  86. {
  87. Toast.makeText(Create_msg.this, "Sender index number : " + numberlist_index, Toast.LENGTH_SHORT).show();
  88. numberlist_index -=1;
  89. String number = numberlist[numberlist_index];
  90. /*sendLongSMS(number);
  91. sendSMSRecordToSendItem(number);*/
  92. Toast.makeText(Create_msg.this, "Sender number : " + number, Toast.LENGTH_SHORT).show();
  93. numberlist[numberlist_index]=null;
  94. // mistake // Toast.makeText(Create_msg.this, "Sender number (After operation): " + numberlist[numberlist_index], Toast.LENGTH_SHORT).show();
  95. if(numberlist_index == 0){
  96. finish();
  97. }
  98. }
  99. }
  100. catch(ArrayIndexOutOfBoundsException e)
  101. {
  102. Toast.makeText(Create_msg.this, "You can not send it more than 20 person.", Toast.LENGTH_SHORT).show();
  103. }
  104. }
  105. });
  106. }
  107. @Override
  108. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  109. super.onActivityResult(requestCode, resultCode, data);
  110. if (requestCode == 1) {
  111. if (resultCode == RESULT_OK) {
  112. Check_addmyNumber();
  113. Uri contactUri = data.getData();
  114. String[] projection = {Phone.NUMBER,Phone.DISPLAY_NAME,Phone.STATUS};
  115. Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
  116. cursor.moveToFirst();
  117. int column = cursor.getColumnIndex(Phone.NUMBER);
  118. String number = cursor.getString(column);
  119. //edWrite_no.setText(number);
  120. numberlist[numberlist_index]=number;
  121. numberlist_index +=1;
  122. int column2 = cursor.getColumnIndex(Phone.DISPLAY_NAME);
  123. String number2 = cursor.getString(column2);
  124. edWrite_no.setText(edWrite_no.getText().toString()+ " " + number2);
  125. }
  126. }
  127. }
  128. public void Check_addmyNumber(){
  129. if(isNumericEntered== true)
  130. {
  131. numberlist[numberlist_index]=myEnterNumber;
  132. numberlist_index +=1;
  133. myEnterNumber="";
  134. isNumericEntered=false;
  135. }
  136. }
  137. public void sendLongSMS(String number) {
  138. String phoneNumber = number;//.getText().toString();
  139. String message = edWrite_text.getText().toString();
  140. SmsManager smsManager = SmsManager.getDefault();
  141. ArrayList<String> parts = smsManager.divideMessage(message);
  142. smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
  143. }
  144. public void sendSMSRecordToSendItem(String number) {
  145. ContentValues values = new ContentValues();
  146. values.put("address", number);
  147. values.put("body", edWrite_text.getText().toString());
  148. getContentResolver().insert(Uri.parse("content://sms/sent"), values);
  149. }
  150. }
Step 9
Create a file as "ForwordMSG.java", you will see the coding of this file in my attachment.
Step 10
Create a new file "Inbox.java" with the following coding.
  1. package c.him.smsapll;
  2. import java.text.DateFormat;
  3. import java.util.Date;
  4. import android.app.ListActivity;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ListView;
  13. import android.widget.Toast;
  14. public class Inbox extends ListActivity
  15. {
  16. String[] Inbox_name = new String[100],
  17. Inbox_number = new String[100],
  18. Inbox_date = new String[100],
  19. Inbox_type = new String[100],
  20. Inbox_msg = new String[100];
  21. int pos = 0;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState)
  24. {
  25. // TODO Auto-generated method stub
  26. super.onCreate(savedInstanceState);
  27. Inbox_Read();
  28. setListAdapter(new InboxArrayAdapter(this, Inbox_name, Inbox_number, Inbox_date, Inbox_type, Inbox_msg));
  29. }
  30. @Override
  31. protected void onListItemClick(ListView l, View v, int position, long id)
  32. {
  33. // TODO Auto-generated method stub
  34. super.onListItemClick(l, v, position, id);
  35. Toast.makeText(Inbox.this, "Number : " + Inbox_number[position], Toast.LENGTH_SHORT).show();
  36. Intent intent = new Intent(this, ViewMesg.class);
  37. intent.putExtra("name", Inbox_name[position]);
  38. intent.putExtra("no", Inbox_number[position]);
  39. intent.putExtra("date", Inbox_date[position]);
  40. intent.putExtra("time", Inbox_type[position]);
  41. intent.putExtra("msg", Inbox_msg[position]);
  42. startActivity(intent);
  43. }
  44. void Inbox_Read()
  45. {
  46. Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
  47. Cursor cursor1 = getContentResolver().query(
  48. mSmsinboxQueryUri,
  49. new String[]
  50. {
  51. "_id",
  52. "thread_id",
  53. "address",
  54. "person",
  55. "date",
  56. "body",
  57. "type"
  58. }, null, null, null);
  59. startManagingCursor(cursor1);
  60. String[] columns = new String[]
  61. {
  62. "address",
  63. "person",
  64. "date",
  65. "body",
  66. "type"
  67. };
  68. if (cursor1.getCount() > 0)
  69. {
  70. String count = Integer.toString(cursor1.getCount());
  71. while (cursor1.moveToNext())
  72. {
  73. String number = cursor1.getString(cursor1.getColumnIndex(columns[0]));
  74. String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
  75. String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
  76. String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
  77. String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
  78. Inbox_name[pos] = name;
  79. Inbox_number[pos] = number;
  80. if (date != null)
  81. {
  82. long l = Long.parseLong(date);
  83. Date d = new Date(l);
  84. Inbox_date[pos] = DateFormat.getDateInstance(DateFormat.LONG).format(d);
  85. Inbox_type[pos] = DateFormat.getTimeInstance().format(d);
  86. }
  87. else
  88. {
  89. Inbox_date[pos] = date;
  90. Inbox_type[pos] = type;
  91. }
  92. Inbox_msg[pos] = msg;
  93. pos += 1;
  94. }
  95. }
  96. }
  97. }
Step 11
Create a new file "InboxArrayAdapter.java", you will see this coding in my attachment.
Step 12
Create a new file "SentItem.java", you will see this in my attachment.
Step 13
Create a new file "SentItemArrayAdapter.java" with the following code.
  1. package c.him.smsapll;
  2. import java.util.Date;
  3. import android.content.Context;
  4. import android.util.Log;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11. public class Sent_itemArrayAdapter extends ArrayAdapter {
  12. private Context context;
  13. private String[] Send_item_name,Send_item_number,Send_item_date,Send_item_type,Send_item_msg;
  14. Sent_itemArrayAdapter(Context context,String[] Send_item_name,String[] Send_item_number,String[] Send_item_date,String[] Send_item_type,String[] Send_item_msg)
  15. {
  16. super(context,R.layout.sent_item, Send_item_number);
  17. this.context= context;
  18. this.Send_item_name=Send_item_name;
  19. this.Send_item_number=Send_item_number;
  20. this.Send_item_date=Send_item_date;
  21. this.Send_item_type=Send_item_type;
  22. this.Send_item_msg=Send_item_msg;
  23. }
  24. @Override
  25. public View getView(int position, View convertView, ViewGroup parent) {
  26. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  27. View rowView = inflater.inflate(R.layout.sent_item, parent, false);
  28. //ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
  29. TextView name = (TextView) rowView.findViewById(R.id.tvName);
  30. TextView date = (TextView) rowView.findViewById(R.id.tvDate);
  31. TextView msg = (TextView) rowView.findViewById(R.id.tvSmallMsgView);
  32. TextView type = (TextView) rowView.findViewById(R.id.tvTime);
  33. /*String s = Send_item_name[position];
  34. if(s==null)
  35. name.setText(Send_item_number[position]);
  36. else
  37. name.setText(Send_item_name[position]);*/
  38. name.setText(Send_item_number[position]);
  39. date.setText(Send_item_date[position]);
  40. msg.setText(Send_item_msg[position]);
  41. type.setText(Send_item_type[position]);
  42. /*System.out.println(s);
  43. if (s.equals("Create Message")) {
  44. imageView.setImageResource(R.drawable.writemessage);
  45. } else if (s.equals("Send_item")) {
  46. imageView.setImageResource(R.drawable.Send_item);
  47. } else if (s.equals("Send Item")) {
  48. imageView.setImageResource(R.drawable.send);
  49. } else {
  50. imageView.setImageResource(R.drawable.ic_launcher);
  51. }*/
  52. return rowView;
  53. }
  54. }
Step 14
Create a new file "SmsArray Adapter.java", you will see this coding in my attachment in the .rar file.
Step 15
Now create a new file "Viewmsg.java" as in the following coding.
  1. package c.him.smsapll;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. public class ViewMesg extends Activity{
  10. TextView number,date,time,msg;
  11. Button frd;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.view_msg_layout);
  17. number=(TextView)findViewById(R.id.tvNumber);
  18. date=(TextView)findViewById(R.id.tvDate);
  19. time=(TextView)findViewById(R.id.tvTime);
  20. msg=(TextView)findViewById(R.id.tvMsg);
  21. frd=(Button)findViewById(R.id.btFrd);
  22. }
  23. @Override
  24. protected void onStart() {
  25. // TODO Auto-generated method stub
  26. super.onStart();
  27. Intent i = getIntent();
  28. number.setText(i.getStringExtra("no"));
  29. date.setText(i.getStringExtra("date"));
  30. time.setText(i.getStringExtra("time"));
  31. msg.setText(i.getStringExtra("msg"));
  32. //startActivity(i);
  33. frd.setOnClickListener(new OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. Intent click = new Intent(ViewMesg.this,Forward_msg.class);
  37. click.putExtra("message", msg.getText());
  38. startActivity(click);
  39. }
  40. });
  41. }
  42. }
Step 16
Now open the manifest file and update it using all the activities and permissions shown below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="c.him.smsapll"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="17" />
  9. <uses-permission android:name="android.permission.READ_SMS"/>
  10. <uses-permission android:name="android.permission.WRITE_SMS"/>
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/sms"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="c.him.smsapll.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. <activity android:name="Create_msg"
  25. android:label="@string/c_msg">
  26. </activity>
  27. <activity android:name="Inbox"
  28. android:label="@string/Inbox_msg">
  29. </activity>
  30. <activity android:name="Sent_item"
  31. android:label="@string/Send_item_msg">
  32. </activity>
  33. <activity android:name="Forward_msg"
  34. android:label="@string/Frd_msg">
  35. </activity>
  36. <activity android:name="ViewMesg"
  37. android:label="@string/View_msg">
  38. </activity>
  39. </application>
  40. </manifest>
And at last, see the output as in the following:
sms.jpg
inbox.jpg
sentsms.jpg