Tuesday, March 5, 2013

Android using e-mail services for feedback

we can use our mail services for different purposes and i made a feedback for and i used it successfully. for this purpose please make a project with the name FeedBack and have a main activity with name
MainActivity.java with the following code


package com.jitesh.feedback;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;

public class MainActivity extends Activity {
// For storing the place data


EditText etComment, etEmail;
Button btnSubmit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get data passed from Intent
       Intent i = getIntent();
      
       
       // Get btnCancel and set it's onClick listener to finish the app
       Button btnCancel = (Button) findViewById(R.id.btnCancel);
       btnCancel.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
finish();
}
       });
       
       // Get etComment and etEmail and set their onFocusChange to enable/disable Submit button accordingly
       // if the fields are filled/empty
       etComment = (EditText) findViewById(R.id.etComment);
       etComment.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus)
verifyCommentEmail();
}
       });
       etEmail = (EditText) findViewById(R.id.etEmail);
       etEmail.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus)
verifyCommentEmail();
}
       });
       
       // Get rbRating and etPhoneNumber to get their information later for sending email
       final RatingBar rbRating = (RatingBar) findViewById(R.id.rtRating);
       final EditText etPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
       
       // Get submit button and set it's onClick to start and intent to send email with the relevant information from the form
       btnSubmit = (Button) findViewById(R.id.btnSubmit);
       btnSubmit.setEnabled(false);
       btnSubmit.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("plain/text");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"upadhyay.jitesh@gmail.com"});
               // emailIntent.putExtra(android.content.Intent.EXTRA_BCC, new String[]{etEmail.getText().toString()});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.feedback_for)+"an interesting place");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, etComment.getText().toString()+"\n\n"+getResources().getString(R.string.rating)+rbRating.getRating()+"\n"+getResources().getString(R.string.my_contact_number)+etPhoneNumber.getText().toString());
                startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.chooser_send_feedback)));
                finish();
}
       });
}

// Method that's being called everytime a textfield loses focus
// purpose: to only enable submission if comment and email is filled
private void verifyCommentEmail() {
if (etComment.getText().toString().length() > 0 && etEmail.getText().toString().length() > 0) {
btnSubmit.setEnabled(true);
} else {
btnSubmit.setEnabled(false);
}
}
}


the activity_main.xml is having following layout as given below

<?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" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/rating2"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <RatingBar
                    android:id="@+id/rtRating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:max="4"
                    android:numStars="4"
                    android:saveEnabled="true" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/comment"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <EditText
                    android:id="@+id/etComment"
                    android:layout_width="fill_parent"
                    android:layout_height="100dp"
                    android:gravity="top"
                    android:hint="@string/i_find_that_this_place"
                    android:inputType="textMultiLine" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/your_details"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <EditText
                    android:id="@+id/etEmail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/email" 
                    android:text="upadhyay.jitesh@gmail.com"/>

                <EditText
                    android:id="@+id/etPhoneNumber"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/contact_number" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/bottom_bar"
        android:gravity="center|center_vertical"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/submit" />
    </LinearLayout>

</LinearLayout>

/res/values/strings.xml is having following resources

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">FeedBack</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

    <string name="source">Source: </string>
    <string name="rating2">Rating</string>
    <string name="comment">Comment</string>
    <string name="i_find_that_this_place">I find that this place...</string>
    <string name="your_details">Your Details</string>
    <string name="email">Email</string>
    <string name="contact_number">Contact Number</string>
    <string name="cancel">Cancel</string>
    <string name="submit">Submit</string>
    
      <string name="feedback_for">Feedback for </string>
    <string name="rating">Rating: </string>
    <string name="my_contact_number">My Contact Number: </string>
    
     <string name="chooser_send_feedback">Send Feedback:</string>
    
    
</resources>



download the source code from the link  mailservice feedback

No comments:

Post a Comment