Composing a mail and send a mail with the default email client on device.
contactus.xml :
package com.apc.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ContactUs extends Activity implements OnClickListener{
Button buttonSend;
EditText txtTo;
EditText txtSubject;
EditText txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactus);
buttonSend = (Button) findViewById(R.id.buttonSend);
txtTo = (EditText) findViewById(R.id.editTextTo);
txtSubject = (EditText) findViewById(R.id.editTextSubject);
txtMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String to = txtTo.getText().toString();
String subject = txtSubject.getText().toString();
String message = txtMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email,
"Choose an Email client :"));
}
});
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Manifest file :
Place the internet permission in manifest file
<uses-permission android:name="android.permission.INTERNET"/>
Cheers :-)
contactus.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textViewSubject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextSubject"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="5" />
<Button
android:id="@+id/buttonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
ContactUs.java:
package com.apc.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ContactUs extends Activity implements OnClickListener{
Button buttonSend;
EditText txtTo;
EditText txtSubject;
EditText txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactus);
buttonSend = (Button) findViewById(R.id.buttonSend);
txtTo = (EditText) findViewById(R.id.editTextTo);
txtSubject = (EditText) findViewById(R.id.editTextSubject);
txtMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String to = txtTo.getText().toString();
String subject = txtSubject.getText().toString();
String message = txtMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email,
"Choose an Email client :"));
}
});
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Manifest file :
Place the internet permission in manifest file
<uses-permission android:name="android.permission.INTERNET"/>
Cheers :-)
No comments:
Post a Comment