Professional Documents
Culture Documents
6414
Like (0)
(0)
Android offers full access to SMS functionality from our applications with the SMSManager.
Basically by using the SMS Manager, we can replace the native SMS application or create
new type of presentations that can send text messages, react to the received texts, or use
SMS as a data transport layer. SMS message delivery is not always timely, so SMS is not
Basically the widespread adoption of SMS networks make it particularly good tool for
delivering content to non-Android users and reducing the dependency on the third-party
servers. As a technology, SMS implements a mechanism that allows you to send text
messages to other mobile phone users, irrespective of whether they have Android
phones or not. SMS is supported by almost every phone on earth, so the latency is not an
issue, and updates are infrequent, in such situation SMS data messages are an excellent
process.
Sending SMS Messages:
Short Message Service messaging in Android is handled by the SmsManager. We can get a
reference to the SMS Manager by using the static method SMSManger.getDefault, that type
Actually to send SMS messages our applications require the SEND_SMS permission. To
request this permission, add it to the manifest file through a uses-permission tag that is
shown below.
<uses-permission android:name=”android.permission.SEND_SMS”/> .
SMS Manager, passing in the address (phone number) of our recipient and the text message
Basically the final two parameters let you specify objectives of tracking the transmission and
successful delivery of our messages or text. We will also discuss the broadcast receivers
setting up the buttons, fields, textboxes etc for showing and storing the data. This
sendSMSMessage(); method is used for generating the programming model of sending SMS
as well as data.
The above method is used for sending messages from one system to another. This
programming model is totally based on try catch block. By using the try catch block, SMS
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Basically when we create any SMS program, we also add menu items as per our
menu.
Figure 1: Shows the phone number and text messages option
Figure 2: Shows that receiver accepts the text message.
of our outgoing SMS messages, implement and register Broadcast Receivers that listen for
the actions you specify and creating the Pending Intents you pass in to the
sendTextMessage method.
The first Pending Intent parameter, sentIntent, is fired when the message is either
successfully sent or fails to send. The result code for the Broadcast Receiver that receives
The second Pending Intent parameter, deliveryIntent, is fired only after the destination
The following code snippet shows a typical pattern for sending an SMS and monitoring the
The above code describes the broadcast receiver and its details. The first four lines create
different types of intents like send intent and delivery intent etc. Next, the broadcast
the receiver describes different types of messages. The transmission is monitored by using
between multiple emulator instances. Now to send an SMS from one emulator to another,
you need to specify the port number of the target emulator as the “to” address. Actually the
android will automatically route our message to the target emulator instance, where it will
method on an SMS Manager. Actually the sendDataMessage method works much like
and an array of the bytes that constitute the data which we want to send. The subsequent
skeleton code confirms the basic structure of sending a data message system:
The above code describes the process for sending data messages. Intent and port is
mentioned in the first three lines. In the 4th line the data is mentioned as an array of bytes.
Then this data is sent along with the port number, intent etc.
illustration, we will create an SMS application that turns an Android phone into an
emergency response system. Once it is complete, we can set our phone to automatically
We will use location-based services to tell exactly where to find us. The robustness of the
SMS network infrastructure makes SMS an excellent option for applications like this where
EmergencyResponder Activity.
1. import android.app.Activity;
2. import android.app.PendingIntent;
3. import android.content.Context;
4. import android.content.Intent;
5. import android.content.IntentFilter;
6. import android.content.BroadcastReceiver;
7. import android.content.SharedPreferences;
8. import android.location.Address;
9. import android.location.Geocoder;
All the twenty import files which are mentioned here are interrelated with one another.
The import android.app.Activity application is working to generate the activity set, the
Process 2:
This above code is setting up for the emergency purpose message services.
Process 3:
This is modifying the main.xml layout resource. Include a List View to show the people
requesting a status update and a series of buttons that users can press to send/response
SMS messages. Use external resource references to fill in the button text; we will create
Process 4:
This is the step four which is used to finalize the SMS program.
In the above program we are assigning some control points like labelRequestList,
These all are built-in here which controls the working of our SMS sending program blocks.
Actually we update the external strings.xml resource to include the program.zip file. We
should also define the text message when distinguishing requests for status responses.
This is the last step of the Graphical User Interface that we will complete.
In the modern mobile technology, SMS is very popular for sending information in a very fast
way. SMS does not wait for the receiver, as it is not like receiving phone calls. You just need
to send the text and rest will be handled by the system. So the flexibility of SMS makes it a
very good friend of the modern technology. Android application is also very popular for
Assumptions
The authors are assuming the reader has some basic knowledge of Android and have
all of the tools installed and working—specifically Eclipse, the Android SDK and the
Android ADT plug-in for Eclipse.
Readers may also benefit from reading our Quick Tip: Enabling Users to Send Email
From Your Android Applications – The Easy Way.
First, you need to give some thought to want kind of data you want to collect from the
user. The form may have any number of fields. Consider the types of data you want to
collect and choose the appropriate type of control. For example:
For this tutorial, you will be designing a feedback form. This form collects five pieces of
data from the user:
Begin by creating a layout resource for the form screen. The form will have a bunch of
fields, which could span more than a single screen (depending on the device screen
size), so you should consider wrapping the entire form within a ScrollView control to
enable scrollbars.
The ScrollView control must have exactly one child view, so consider which layout
control is most appropriate for the form you want to create. Forms are often contained
within a vertically oriented LinearLayout control, so that the form fields cascade down
the page vertically, one after another. This also helps the user’s focus move from field to
field naturally.
01
02 <?xml version="1.0" encoding="utf-8"?>
03 <ScrollView
04 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
05 android:layout_width="wrap_content"
06 android:layout_height="wrap_content"
07 android:scrollbars="vertical">
08 <LinearLayout
android:layout_width="fill_parent"
09
android:orientation="vertical"
10 android:layout_height="fill_parent">
11
12 <!--Put form controls here-->
13
14 </LinearLayout>
15 </ScrollView>
16
Next, you need to add a TextView control within the LinearLayout control. The TextView
control called TextViewTitle displays the form description and purpose to the user. This
control displays a string resource called @string/feedbacktitle, which must be defined
within the /res/values/strings.xml string resource file.
1 <TextView
android:id="@+id/TextViewTitle"
2
android:layout_width="wrap_content"
3
4 android:layout_height="wrap_content"
android:text="@string/feedbacktitle"
5 android:textSize="10pt">
6 </TextView>
7
Now you need to add your first EditText control just below the TextView control you just
created. This EditText control called EditTextName acts as a form field for the user’s
name. You can use the hint attribute to supply a string to display in the EditText control
when it’s empty (e.g. “Type your name here…”). You can also set the inputType
attribute of the EditText control to apply name entering logic.
1 <EditText
2 android:id="@+id/EditTextName"
3 android:layout_height="wrap_content"
4 android:hint="@string/feedbackname"
5 android:inputType="textPersonName"
android:layout_width="fill_parent">
6 </EditText>
7
Next, you need to add your second EditText control just below the EditText control
called EditTextName. This EditText control called EditTextEmail acts as a form field for
the user’s email address. Again, set the hint attribute to supply a string to display in the
EditText control when it’s empty. This time, set the inputType attribute of the EditText
control to textEmailAddress, which will make entering emails easier on the user.
Next, you need to add a Spinner control just below the EditText control you just created.
This Spinner control called SpinnerFeedbackType allows the user to select the type of
feedback from a fixed list of options (Praise, Gripe, Suggestion, or Bug).
First, you need to define these choices as individual string resources in
the strings.xmlresource file.
1
<?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <!--Other string resources also defined in this file… -->
4 <string name="feedbacktype1">Praise</string>
5 <string name="feedbacktype2">Gripe</string>
<string name="feedbacktype3">Suggestion</string>
6
<string name="feedbacktype4">Bug</string>
7 </resources>
8
Next, create a string array resource using the individual string resources as follows in
/res/values/arrays.xml:
1
<?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <string-array name="feedbacktypelist">
4 <item>@string/feedbacktype1</item>
5 <item>@string/feedbacktype2</item>
6 <item>@string/feedbacktype3</item>
<item>@string/feedbacktype4</item>
7 </string-array>
8 </resources>
9
Now you are ready to configure the Spinner control in your form layout. Begin by
supplying the prompt attribute, which will provide a helpful string at the top of the
Spinner control. Next, specify the list of string choices using the entries attribute—
specifically, set the entries attribute to the string array you just defined:
@array/feedbacktypelist.
1 <Spinner
2 android:id="@+id/SpinnerFeedbackType"
3 android:layout_height="wrap_content"
4 android:prompt="@string/feedbacktype"
5 android:layout_width="fill_parent"
android:entries="@array/feedbacktypelist">
6 </Spinner>
7
Step 7: Add a Multi-Line EditText Control
(Feedback)
Next, you need to add one more EditText control just below the Spinner control. This
EditText control called EditTextFeedbackBody acts as a form field for the feedback text.
Again, set the hint attribute to supply a string to display in the EditText control when it’s
empty. This time you want to give the user ample space to write praise, gripes,
suggestions, or describe bugs in the application. Therefore, you may want to set the
inputType attribute of the EditText control to textMultiLine and specify the number of
lines to draw using the lines attribute.
1
<EditText
2 android:id="@+id/EditTextFeedbackBody"
3 android:layout_height="wrap_content"
4 android:hint="@string/feedbackbody"
5 android:inputType="textMultiLine"
android:lines="5"
6
android:layout_width="fill_parent">
7 </EditText>
8
Next, you need to add a CheckBox control just below the EditText control you just
created. This CheckBox control called CheckBoxResponse allows the user to choose
whether or not they want to request an email response from the app developer. You can
use the text attribute to supply a string to display next to the CheckBox control.
1 <CheckBox
android:id="@+id/CheckBoxResponse"
2
android:layout_height="wrap_content"
3 android:text="@string/feedbackresponse"
4 android:layout_width="fill_parent">
5 </CheckBox>
6
Finally, you are ready to finish off the form with a Button control. If you want to have a
button with text on it, use the Button control; if you prefer a button with a picture on it,
use an ImageButton control instead. We will use a Button control here. First, set the text
on the Button control using the text attribute. Next, you can easily register a click
handler (as opposed to registering it programmatically in your Activity) for your Button
control using the onClick attribute.
1 <Button
2 android:id="@+id/ButtonSendFeedback"
3 android:layout_height="wrap_content"
4 android:text="@string/feedbackbutton"
5 android:onClick="sendFeedback"
android:layout_width="fill_parent">
6 </Button>
7
Excellent! You’ve finished designing your form. Now, all you need to do is implement
the sendFeedback() method in your Activity.
Step 10: Implement a Button click handler
In the Button control, you specified the onClick attribute as sendFeedback. Now you will
need to implement a method called sendFeedback() within your Activity class. For
example:
Now that your form is designed and the controls have been implemented, you next need
to collect the form data from the individual fields when the Button control is clicked.
Finally, your form included a CheckBox control. In this case, the result is just a flag to
tell your application if the box was checked or not.
Now that you’ve got all your form data, you’re ready to craft a message. Simply process
all the data fields and build an appropriate feedback message. For example, you might
use some fields in the message subject, and others in the message body. You can use
format strings to help build the appropriate strings, the specifics of which will be
discussed in an upcoming quick tip.
Conclusion
In this tutorial, you learned how to use various types of input controls to design a
feedback form within an Android application. The EditText control is versatile and
powerful, allowing for many different types of text and freeform input. The Spinner and
Checkbox controls help limit the user’s input to a specific set of responses. The Button
control is a simple way to generate an event to process the form input.
There are many other controls worth exploring for use within forms. There is a lot more
we could cover regarding good form design, how form controls fit into the Activity
lifecycle, and how input methods and such factor into things, but for now, focus on
gaining a good handle on the basics of form controls and how to use them.
We hope you enjoyed this tutorial and look forward to your feedback!
A Form is used to interact with the user. To design a form we will have to start with the various
elements of a form. Consider a Registration form where we get user details, validate it and register
with our service. We use basic details like username,password and contact number. To design such a
form we will have to use the following views or elements:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView_enter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:textSize="18sp"
android:text="@string/enter_name" />
<EditText
android:id="@+id/editText_enter_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView_enter_name"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:inputType="text"
android:textSize="17sp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView_enter_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText_enter_name"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:textSize="18sp"
android:text="@string/enter_user_name" />
<EditText
android:id="@+id/editText_enter_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView_enter_username"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:inputType="text"
android:textSize="17sp"
android:ems="10" >
</EditText>
<TextView
android:id="@+id/textView_enter_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText_enter_username"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:textSize="18sp"
android:text="@string/enter_password" />
<EditText
android:id="@+id/editText_enter_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView_enter_password"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:inputType="textPassword"
android:textSize="17sp"
android:ems="10" >
</EditText>
<TextView
android:id="@+id/textView_enter_rpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText_enter_password"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:textSize="18sp"
android:text="@string/enter_rpassword" />
<EditText
android:id="@+id/editText_enter_rpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView_enter_rpassword"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:inputType="textPassword"
android:textSize="17sp"
android:ems="10" >
</EditText>
<Button
android:id="@+id/button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_enter_rpassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/register" />
</RelativeLayout>
There are various attributes that we are dealing with.The text size,color and style can be altered for
each of the views.The <TextView></TextView> tag is used to define a label or textview. The
various attributes used to format text are:
android:textColor="[hex-code or
pre-defined android color using @android:color/(color-name)]"
These are the same with EditTexts or TextBoxes. In addition you must also specify the input type as
either plain text or password or a phone number etc.
So it is really important to specify the view id as each item is relative to the other. Any change in id
after positioning can cause other dependent views to disappear or stick to the top of the screen. This
is because we are positioning relative as in android:layout_below=”some_id”. If this id changes or
doesn’t exist then it causes the view to disappear. It is very crucial that we change it everywhere in
the layout.
In addition to view position there is one very important role played by android:id attribute. The id
identifies each view uniquely and this is used in java classes to refer the various elements. It is also
the responsibility of the developer to avoid naming conflicts and use unique identifiers for each view.
Here is a simple activity to handle the form. An activity is created by default. We need to define the
form elements and reference it.
TextView textView_name,textView_password,
textView_username,textView_repeat_password;
EditText editText_name,editText_password,
editText_username,editText_repeat_password;
Button button_register;
textView_name=(TextView) findViewById(R.id.textView_enter_name);
textView_username=(TextView) findViewById(R.id.textView_enter_username);
textView_password=(TextView) findViewById(R.id.textView_enter_password);
textView_repeat_password=(TextView) findViewById(R.id.textView_enter_rpassword);
editText_name=(EditText) findViewById(R.id.editText_enter_name);
editText_username=(EditText) findViewById(R.id.editText_enter_username);
editText_password=(EditText) findViewById(R.id.editText_enter_password);
editText_repeat_password=(EditText) findViewById(R.id.editText_enter_rpassword);
button_register=(Button) findViewById(R.id.button_register);
To handle
button click we use button,setOnClickListener(listener).
button_register.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
}
});
button_register.setOnClickListener(this);
@Override
public void onClick(View arg0) {
}
To handle OnClick for multiple buttons we can switch between different ids.Anddon’t forget to
include break statement.
@Override
public void onClick(View arg0) {
switch(arg0.getId())
{
case R.id.button_register:
break;
}
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
TextView
textView_name,textView_password,textView_username,textView_repeat_password;
EditText
editText_name,editText_password,editText_username,editText_repeat_password;
Button button_register;
String username="",name="",password="",repeat_password="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_name=(TextView) findViewById(R.id.textView_enter_name);
textView_username=(TextView) findViewById(R.id.textView_enter_username);
textView_password=(TextView) findViewById(R.id.textView_enter_password);
textView_repeat_password=(TextView)
findViewById(R.id.textView_enter_rpassword);
editText_name=(EditText) findViewById(R.id.editText_enter_name);
editText_username=(EditText) findViewById(R.id.editText_enter_username);
editText_password=(EditText) findViewById(R.id.editText_enter_password);
editText_repeat_password=(EditText)
findViewById(R.id.editText_enter_rpassword);
button_register=(Button) findViewById(R.id.button_register);
button_register.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
switch(arg0.getId())
{
case R.id.button_register:
//Get Input From EditText
name=editText_name.getText().toString().trim();
username=editText_username.getText().toString().trim();
password=editText_password.getText().toString();
repeat_password=editText_repeat_password.getText().toString();
break;
}
}
}
OnClick we get text from editText convert toString() and trim() it.trim() is used to remove extra
spaces at the beginning and end of the string. If the length is greater than 0 it is not empty and valid.
We can register with server here. Otherwise we Toast an error message. Toast notifications are
nothing but popups to display messages.