Question

In: Computer Science

Android Studio. Java. Please create an application that -> An activity that allows user to enter...

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

Solutions

Expert Solution

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 *******************************************************


Related Solutions

IN ANDROID STUDIO, you will create a mortgage calculator appthat allows the user to enter...
IN ANDROID STUDIO, you will create a mortgage calculator app that allows the user to enter a purchase price, down-payment amount, and an interest rate.Based on these values, the app should calculate the loan amount (purchase price minus down payment) and display the monthly payment for 10, 20, and 30-year loans.Allow the user to select a custom loan duration (in years) by using a SeekBar and display the monthly payment for that custom loan duration.Assignment deliverables (all in a ZIP...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used by the customers to book cinema tickets. Only users of 15 years old and above are allowed to book cinema tickets for a particular film. The user can book more than one ticket where ticket prices vary between 20 and 50 AED. Your app contains tree Activities. The first activity is a launching activity containing a logo and a button start. When the user...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
the code base you will be working with involves an Android application. (Android Studio) application name...
the code base you will be working with involves an Android application. (Android Studio) application name " SharingApp" In the application’s current state: A user of the app is able to create and edit a profile with a unique username and an email address. A user of the app is able to login and logout. An owner is able to record the items they own and wish to share. A bidder is able to place bids on items they wish...
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
Android Studio Code: Provide a working android studio code i.e java and xml code for the...
Android Studio Code: Provide a working android studio code i.e java and xml code for the activity below: Develop an application that is capable to turn pages back and forth. Detailed Instructions: For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT