In: Computer Science
Android Studio. Java. Please create an application that
-> An activity that allows user to enter name, gender, date
of birth, state of residence (selected from a pre-defined list: CA,
AZ, NV, OR), email address and favorite website.
This activity has a button "Show Data" that displays detail
entered
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Details Saver" android:textColor="@android:color/black" android:textSize="24sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/nameField" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginTop="100dp" android:layout_marginEnd="50dp" android:ems="10" android:hint="Enter Name" android:inputType="textPersonName" android:padding="16dp" android:textAlignment="center" android:textColor="@android:color/black" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <RadioGroup android:id="@+id/radioGroup2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="10dp" android:layout_marginEnd="32dp" android:orientation="horizontal" android:padding="16dp" android:textAlignment="center" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/nameField"> <RadioButton android:id="@+id/femaleRB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Female" /> <RadioButton android:id="@+id/maleRB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Male" /> </RadioGroup> <EditText android:id="@+id/dobField" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:ems="10" android:enabled="true" android:hint="Date of birth" android:inputType="textPersonName" android:padding="16dp" android:textAlignment="center" android:textColor="@android:color/black" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/radioGroup2" /> <Spinner android:id="@+id/statesSpinner" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginTop="30dp" android:layout_marginEnd="100dp" android:padding="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/dobField" /> <Button android:id="@+id/showDataButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="100dp" android:layout_marginEnd="32dp" android:backgroundTint="#2196F3" android:elevation="10dp" android:padding="16dp" android:text="Show Data" android:textColor="@android:color/white" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/statesSpinner" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.Toast; import java.util.ArrayList; import java.util.Calendar; import java.util.List; public class MainActivity extends AppCompatActivity { private EditText nameField; private RadioButton femaleRb, maleRB; private EditText dobField; private Spinner statesSpinner; private Button showDataButton; private DatePickerDialog.OnDateSetListener mDateSetListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameField = findViewById(R.id.nameField); femaleRb = findViewById(R.id.femaleRB); maleRB = findViewById(R.id.maleRB); dobField = findViewById(R.id.dobField); dobField.setKeyListener(null); dobField.setCursorVisible(false); dobField.setFocusable(false); statesSpinner = findViewById(R.id.statesSpinner); showDataButton = findViewById(R.id.showDataButton); dobField.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day); dpd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dpd.show(); } }); mDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { month += 1; dobField.setText(dayOfMonth + " - " + month + " - " + year); } }; // populate the spinner for States List<String> states = new ArrayList<>(); states.add("CA"); states.add("AZ"); states.add("NV"); states.add("OR"); ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, states); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); statesSpinner.setAdapter(spinnerArrayAdapter); // show data button action listener showDataButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameField.getText().toString(); String gender = ""; if(!femaleRb.isChecked() && !maleRB.isChecked()) Toast.makeText(MainActivity.this, "Please select your gender", Toast.LENGTH_SHORT).show(); else { if(femaleRb.isChecked()) gender = "Female"; else gender = "Male"; } String dob = dobField.getText().toString(); String state = statesSpinner.getSelectedItem().toString(); if(TextUtils.isEmpty(name) || TextUtils.isEmpty(gender) ||TextUtils.isEmpty(dob) || TextUtils.isEmpty(state)) Toast.makeText(MainActivity.this, "All fields are mandatory", Toast.LENGTH_SHORT).show(); else { Intent showDataIntent = new Intent(MainActivity.this, ShowDataActivity.class); showDataIntent.putExtra("name", name); showDataIntent.putExtra("gender", gender); showDataIntent.putExtra("dob", dob); showDataIntent.putExtra("state", state); startActivity(showDataIntent); } } }); } }
activity_show_data.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".ShowDataActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginTop="50dp" android:layout_marginEnd="100dp" android:text="Data Viewer" android:textColor="@android:color/black" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/nampPlace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginTop="130dp" android:layout_marginEnd="50dp" android:text="Your Name:" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/genderPlace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginTop="30dp" android:layout_marginEnd="50dp" android:text="Your Gender:" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/nampPlace" /> <TextView android:id="@+id/dobPlace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginTop="30dp" android:layout_marginEnd="50dp" android:text="Your date of birth:" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/genderPlace" /> <TextView android:id="@+id/statePlace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginTop="30dp" android:layout_marginEnd="50dp" android:text="Your state of resisdence:" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/dobPlace" /> <Button android:id="@+id/backButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:backgroundTint="#03A9F4" android:padding="16dp" android:text="back" android:textAlignment="center" android:textColor="@android:color/white" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/statePlace" /> </androidx.constraintlayout.widget.ConstraintLayout>
ShowDataActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class ShowDataActivity extends AppCompatActivity { private TextView namePlace, genderPlace, dobPlace, statePlace; private Button backButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_data); namePlace = findViewById(R.id.nampPlace); genderPlace = findViewById(R.id.genderPlace); dobPlace = findViewById(R.id.dobPlace); statePlace = findViewById(R.id.statePlace); backButton = findViewById(R.id.backButton); String name = getIntent().getStringExtra("name"); String gender = getIntent().getStringExtra("gender"); String dob = getIntent().getStringExtra("dob"); String state = getIntent().getStringExtra("state"); namePlace.setText("Your name is: " + name); genderPlace.setText("Your gender is: " + gender); dobPlace.setText("Your date of birth is: " + dob); statePlace.setText("Your state of residence is: " + state); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
******************************************************************** SCREENSHOT *******************************************************