Reputation: 3
Recently I decided to develop an app in Android. I have used this forum and other tools online to teach myself how to develop a simple app that asks the user for their name and answer few questions. My aim to allow the user to send the answers to any email of their choice. So far I have been able to achieve this:
From: ********@gmail.com
To: ********@gmail.com
Subject: Survey result for John Doe
Name: John Doe
Question 1: true
End of Survey.
My aim is to include in the email not the state of question 1 (i.e. "True") but instead I want to show the answer of their question. For Example, Question 1: B. 26-35.
I have searched online for the past couple of days but I have been having issues to find some help as I'm still new to programming. The closest thing I came across was this SO Question but it is not what I'm looking for. Any help or guidance would be highly appreciated.
MainActivity.java
package com.example.android.sampleupdrs;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
}
public void emailResult (View view)
{
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
RadioGroup radioQ1 = (RadioGroup) findViewById(R.id.radio_Q1);
final RadioButton checkedRadioQ1 = (RadioButton) radioQ1.findViewById(radioQ1.getCheckedRadioButtonId());
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
radioQ1.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged (RadioGroup group, int checkedQ1Id)
{
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedQ1Id);
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
if (isCheckedRadioQ1)
{
tv.setText("Checked: " +checkedRadioButton.getText());
}
}
});
String resultsEmail = createEmailSummary(name, isCheckedRadioQ1);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Survery result for " + name);
intent.putExtra(Intent.EXTRA_TEXT, resultsEmail);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
private String createEmailSummary (String name, boolean isCheckedRadioQ1) {
String resultsEmail = "Name: " + name;
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1;
resultsEmail += "\nEnd of Survey";
return resultsEmail;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Name"
android:inputType="textCapWords"/>
<TextView
android:text="@string/q1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/textView1"/>
<RadioGroup
android:id="@+id/radio_Q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/q1b0_url"
android:text="@string/q1b0string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp" />
<RadioButton
android:id="@+id/q1b1_url"
android:text="@string/q1b1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/q1b0_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="@+id/q1b2_url"
android:text="@string/q1b2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/q1b1_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="@+id/q1b3_url"
android:text="@string/q1b3string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/q1b2_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="@+id/q1b4_url"
android:text="@string/q1b4string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/q1b3_url"
android:layout_marginBottom="30dp"
android:gravity="start" />
</RadioGroup>
<TextView
android:text="@string/q2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/textView2"/>
<RadioGroup
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nextQ1"
android:onClick="emailResult"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right" />
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<string name="app_name">Survey</string>
<string name="q1string">1. Select age group</string>
<string name="q1b0string">A. 18-25</string>
<string name="q1b1string">B. 26-35</string>
<string name="q1b2string">C. 36-45</string>
<string name="q1b3string">D. 46-59</string>
<string name="q1b4string">E. 60+</string>
</resources>
Upvotes: 0
Views: 638
Reputation: 875
In MainActivity
class,
you can get the text of the Radio button in this way:
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
text = "";
if (isCheckedRadioQ1) {
text = checkedRadioQ1.getText().toString();
tv.setText("Checked: " +checkedRadioButton.getText());
}
In method createEmailSummary
do
resultsEmail += "\nQuestion 1 " + text;
instead of
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1
You'll have to send text
as a parameter to createEmailSummary
instead of isCheckedRadioQ1
Upvotes: 1
Reputation: 5087
If I understand correctly, what you need is checkedRadioButton.getText().toString()
and not the boolean isCheckedRadioQ1.
Upvotes: 0