Display Text from Checkbox on AlertDialog

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Mar 12, 2015 5:04:50 PM via Website

I want to display the text (e.g. "Sports") from a checkbox when it is clicked on the AlertDialogue along with the name, email and date of birth message.

<resources>

<string name="app_name">FormInput</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_data_input">Enter your details</string>
<string name="name_prompt">Name:</string>
<string name="DOB_prompt">Date Of Birth:</string>
<string name="email_prompt">Email:</string>
<string name="next_command_prompt">Next</string>
<string name="exit_command_label">Exit</string>
<string name="clear_command_prompt">Clear</string>
<string name="check_one">Sports</string>
<string name="check_two">Reading</string>
<string name="check_three">Sleeping</string>
<string name="question">Hobbies:</string>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/name_prompt"
    tools:context=".DataInput" />

<EditText
    android:id="@+id/EditTextName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName" >
    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/question" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="@string/check_one" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox1"
    android:text="@string/check_two" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/checkBox2"
    android:text="@string/check_three" />


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/DOB_prompt" />

<DatePicker
    android:id="@+id/DatePickerDOB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/email_prompt" />

<EditText
    android:id="@+id/EditTextEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textEmailAddress" />

<Button
    android:id="@+id/ButtonNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next_command_prompt" />

<Button
    android:id="@+id/ButtonClear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/clear_command_prompt" />


private void displayNextAlert() {

    // Get what the user entered
    EditText nameInput = (EditText) findViewById(R.id.EditTextName);
    DatePicker dobInput = (DatePicker) findViewById(R.id.DatePickerDOB);
    EditText emailInput = (EditText) findViewById(R.id.EditTextEmail);

    String strName = nameInput.getText().toString();
    String strDOB = dobInput.getDayOfMonth() + "/"
            + (dobInput.getMonth() + 1) + "/" + dobInput.getYear();
    String strEmail = emailInput.getText().toString();


    // Create and display the Alert dialog
    new AlertDialog.Builder(this)
            .setTitle("Details entered")
            .setMessage(
                    " Details entered:\n " + strName + "\n " + strDOB
                            + "\n " + strEmail).setNeutralButton("Back",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing - it will just close when clicked
                }
            }).show();
}