To explain: I'm learning Android Studio I'm trying to make a program which has a form similar to the ones where you sign up for an account on a website (with a username, name, email address and a date picker in spinner mode so I can set the date of birth. I'm setting it up so it shouldn't let you sign up for an account if you are under 18 years old according to the Date of Birth. However, I'm also setting up a "Submit" button that takes you to a screen which says "Thanks for Signing Up [username]!" I'm using an intent (In the sendMessage method) and a OnClickListener (In the onClick method). My problem is that I'm trying to (1) Put out a toast with either the dob or a message stating that the dob is under 18 (2) Then proceed to the "Thank You page". Can anyone give me some clarity in my confused logic or do you have a better way that I can accomplish this? This question might be a duplicate and I'm checking, but I couldn't find anything close (Let me know if you did though) This is what I've done so far . . .
MainActivity.java:
package com.example.simpleregistrationform.feature;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.SimpleRegistrationForm.MESSAGE";
DatePicker simpleDatePicker;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate the date picker and a button
simpleDatePicker = findViewById(R.id.simpleDatePicker);
submit = findViewById(R.id.submit);
// perform click event on submit button
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get the values for day of month , month and year from a date picker
String day = "Day = " + simpleDatePicker.getDayOfMonth();
String month = "Month = " + (simpleDatePicker.getMonth() + 1);
String year = "Year = " + simpleDatePicker.getYear();
int age = Math.abs(simpleDatePicker.getYear() - (Calendar.getInstance().get(Calendar.YEAR)));
if (age < 18){
Toast.makeText(getApplicationContext(),"Users under 18 cannot register",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year + "\n" + age, Toast.LENGTH_LONG).show();
}
}
});
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = findViewById(R.id.user_Name);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.simpleregistrationform.feature.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/register"
android:textSize="24sp"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<DatePicker
android:id="@+id/simpleDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="124dp"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:spinnersShown="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="128dp"
android:ems="10"
android:hint="@string/enter_your_name"
android:inputType="textPersonName"
android:text="@string/name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="196dp"
android:ems="10"
android:hint="@string/enter_your_email_address"
android:inputType="textEmailAddress"
android:text="@string/email"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/user_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/enter_your_user_name"
android:inputType="textPersonName"
android:text="@string/user_Name"
app:layout_constraintBottom_toTopOf="@+id/name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.746" />
<TextView
android:id="@+id/dob"
android:layout_width="100dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="264dp"
android:hint="@string/enter_your_date_of_birth"
android:text="@string/Date_Of_Birth"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:onClick="sendMessage"
android:text="@string/submit"
app:layout_constraintBottom_toTopOf="@+id/user_Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
My DisplayMessageActivity java file:
package com.example.simpleregistrationform.feature;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
And My activity_display_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/user_Name"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/thank_you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/thank_you_for_signing_up"
android:text="@string/thank_you_for_signing_up"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="27dp"
tools:text="@string/thank_you_for_signing_up" />
</android.support.constraint.ConstraintLayout>
Solution (thanks to David Wasser and Basil Bourque) it took me some time, but I've fused the sendMessage method into the onCreate method that had the onClickListener code as follows (I've commented out the original "sendMessage" method). I admit that it's nasty coding, but it works!:
package com.example.simpleregistrationform.feature;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.SimpleRegistrationForm.MESSAGE";
DatePicker simpleDatePicker;
//Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate the date picker and a button
simpleDatePicker = findViewById(R.id.simpleDatePicker);
Button submit = findViewById(R.id.submit);
// perform click event on submit button
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get the values for day of month , month and year from a date picker
String day = "Day = " + simpleDatePicker.getDayOfMonth();
String month = "Month = " + (simpleDatePicker.getMonth() + 1);
String year = "Year = " + simpleDatePicker.getYear();
int age = Math.abs(simpleDatePicker.getYear() - (Calendar.getInstance().get(Calendar.YEAR)));
if (age < 18){
Toast.makeText(getApplicationContext(),"Users under 18 cannot register",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year + "\n" + "You are " + age + " years old", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(v.getContext(), DisplayMessageActivity.class);
EditText editText = findViewById(R.id.user_Name);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
/** Called when the user taps the Send button *//*
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = findViewById(R.id.user_Name);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}*/
}