Android Tutorial: How to create a feedback button

How to create a feedback button

In this tutorial I will show you how to implement a feedback button in your Android application. You can also use this code to compose other emails from within your app.

Creating the layout file

For this tutorial I will use a button to trigger the feedback code, but you can also use a menu option or an item in your preference screen in your final app.

First we will begin by putting a button on a layout called “activity_main.xml”

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_feedback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_feedback" />
</RelativeLayout>

As you can see I have created a button called “btn_feedback”, it’s text is specified by a resource string and its OnClick event is set to “btnFeedbackOnClick”.

Adding the string resources

Now we will specify our resource strings, these are allocated in the file “res/values/strings.xml”. Using string resources helps us change the strings later if we want to and it makes it easy to add translations for the strings if we need to.

<resources>
    <string name="app_name">Feedback example</string>
    <string name="button_feedback">Send feedback</string>        
    <string name="title_send_feedback">Send feedback</string>

    <string name="mail_feedback_email">YourEmailAdress</string>
    <string name="mail_feedback_subject">Feedback on your app</string>
    <string name="mail_feedback_message">Hi, 
\n\nYour Feedback sample app rocks! I would like to give you some feedback:</string>
</resources>

Strings:
“app_name”: Your application title.
“button_feedback”: Text on feedback button.
“title_send_feedback”: Title of chooser dialog.
“mail_feedback_email”: Email address where the feedback is going.
“mail_feedback_subject”: Subject of the email.
“mail_feedback_message”: Default mail message.

Coding the feedback button

Now we need to create our activity. I Have named my activity MainActivity and the corresponding class file is named “MainActivity.java”. As You can see in the next part of the code I am using the support library in this example. This of course is not necessary, you can also extend from the Activity class if you want to.

package com.example.feedbackexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;


public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

Next we add an OnClickListener for the button in the OnCreate method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_feedback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
    }

Now for the actually feedback message code. I’ve created a private method called sendFeedback which will create an intent that will start an activity to compose the feedback email.

    //Start a new activity for sending a feedback email
    private void sendFeedback() {
        final Intent _Intent = new Intent(android.content.Intent.ACTION_SEND);
        _Intent.setType("text/html");
        _Intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ getString(R.string.mail_feedback_email) });
        _Intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.mail_feedback_subject));
        _Intent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.mail_feedback_message));
        startActivity(Intent.createChooser(_Intent, getString(R.string.title_send_feedback)));
    }

Last thing is to the new sendFeedback method from the button’s OnClickListener, and that’s it.

        findViewById(R.id.btn_feedback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendFeedback();
            }
        });

For this example I am using type “text/html” you can also use “text/plain” but when testing I found that this did not work for all email clients.

I hope this post will help you develop your Android app.
Till next post.

Example project

An example project for this article can be found on Github.
https://github.com/MartinvanZ/FeedbackExample

More information

For more information see the developer online reference:
http://developer.android.com/reference/android/content/Intent.html

2 comments on “Android Tutorial: How to create a feedback button

    • In that case you should be looking at a different solution. For example you could make a webpage that accepts a call from the app with a parameter for a message that the user has typed in the app. On the server site you could then log those messages to a database or save them as files. In that case I suggest that you look at how to make a http request. This webpage might be useful for you: https://developer.android.com/training/volley/simple.html

Leave a Reply

Your email address will not be published. Required fields are marked *