Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a form requiring a persons name, age and date of birth as input. The age should be provided using a NumberPicker and the date
Create a form requiring a persons name, age and date of birth as input. The age should be provided using a NumberPicker and the date of birth via a DatePicker.Use a TextView to display a message such as Hello Joe your DoB is: 20/07/1970 and your age is 46.
I have coded..but struggling to print out the information.
//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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20dp" android:text="Name:" /> <EditText android:id="@+id/nameText" android:layout_width="199dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" /> <TextView android:id="@+id/ageText" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20dp" android:text="Age:" /> <NumberPicker android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="80dp" /> <TextView android:id="@+id/dobText" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20dp" android:text="Date of Birth:" /> <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dob" android:scaleX=".8" android:scaleY=".8" /> <TextView android:id="@+id/information" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20dp" android:text="" /> LinearLayout> ScrollView> android.support.constraint.ConstraintLayout>
//java
package com.user.qs4; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.DatePicker; import android.widget.EditText; import android.widget.NumberPicker; import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { //Declare NumberPicker np; TextView msg; TextView tv2; DatePicker dp; TextView tv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Code np = (NumberPicker)findViewById(R.id.age); tv2 = (TextView)findViewById(R.id.information); np.setMinValue(0); np.setMaxValue(100); np.setWrapSelectorWheel(false); TextView tv3 = (TextView)findViewById(R.id.information); DatePicker dp = (DatePicker)findViewById(R.id.dob); int y = dp.getYear(); int m = dp.getMonth(); int d = dp.getDayOfMonth(); //Print TextView msg = (TextView)findViewById(R.id.information); EditText name = (EditText)findViewById(R.id.nameText); msg.setText("Your name is " + name.getText().toString()+"!"); tv3.setText(np.getValue()); tv2.setText(String.valueOf(d)+String.valueOf(m)+String.valueOf(y)); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started