Difference between revisions of "Mobile App Development"
| Line 151: | Line 151: | ||
Remember that if the EditText held a value of the type double you would need to parse it as below: | Remember that if the EditText held a value of the type double you would need to parse it as below: | ||
| + | ==Layout group exercise 1== | ||
Double co = Double.parseDouble(holdVaccineName.getText().toString()); | Double co = Double.parseDouble(holdVaccineName.getText().toString()); | ||
| + | |||
| + | This is a group exercise | ||
| + | |||
| + | In your group create a form with three text and label fields and a submit button. As an option, you may chose not to use EditTexts for label and use a hint instead. | ||
| + | |||
| + | Assume that the text fields are used to search for a location for a Covid19 vaccine. A user needs to enter a zip code, preferred vaccine (Pfizer, Moderna, J&J, Astrazeneca etc), and age. Make sure you label your widgets clearly. | ||
| + | |||
| + | At the end of the exercise, one team member will show the layout to the class. | ||
| + | |||
| + | ==Layout group excercise 2 == | ||
| + | This is a group exercise that builds on exercise 1. | ||
| + | |||
| + | In this exercise, you will code the button to display the information that the user has entered in three sentences below the submit (search) button. | ||
| + | |||
| + | The three sentences should read: | ||
| + | |||
| + | You are in zip code ????? | ||
| + | You are ?? years old | ||
| + | Your preferred vaccine is ????? | ||
Revision as of 19:09, 27 June 2021
Contents
Dimensions File
The dimensions file is named dimens.xml
This file contains dimension values used on the layout widget. The example below shows some dimensions used on the layout file
<?xml version="1.0" encoding="utf-8"?> <resources>
<dimen name="label_text_size">24sp</dimen>
</resources>
Here is an example of how this file could be used to set the dimensions for a label. The label will have 24sp as its text size
<TextView android:id="@+id/textViewShowNameHere" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="67dp" android:textSize="@dimen/label_text_size" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
Strings File
The String.xml file is used to hold string type of data. The example below shows a String.xml file and how it may be used.
<resources <string name="app_name">Display A Vaccine</string> <string name="buttonValue">Show Vaccine</string> <string name="nameLabel">Vaccine</string> <string name="showNameHint">Enter a vaccine</string> <string name="header_message">%1$s! That is a Covid19 vaccine</string>
</resousrces>
Here is an example of how this code could be used in a layout.xml file show the text for a button
<Button
android:id="@+id/buttonShowName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="62dp" android:layout_marginBottom="520dp" android:text="@string/buttonValue" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextName" app:layout_constraintVertical_bias="0.0" />
Layout File
This is the code used in a layout with one two labels, a text box (EditText) and a button. The layout code is in xml format. It describes the view
<?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">
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="@string/showNameHint"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:textSize="@dimen/label_text_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewShowNameHere" />
<Button
android:id="@+id/buttonShowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="62dp"
android:layout_marginBottom="520dp"
android:text="@string/buttonValue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextName"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textViewShowNameHere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="67dp"
android:textSize="@dimen/label_text_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Coding App Behavior
The code below implements a way to display data entered on a text box to a label
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /*Layout used in this class*/
displayName(); /*Call the method to display the name*/
}
/*Method to make the button take the text and display it*/
private void displayName(){ /*The name of the method is "displayName"*/
/*The method is only used in this class (private)
The method does not return any value(void)*/
Button buttonDisplayName = findViewById(R.id.buttonShowName);
/*Associated the code with the button named buttonShowName in the layout*/
buttonDisplayName.setOnClickListener(new View.OnClickListener(){
/*Declare a listener for the actions of the button*/
@Override
public void onClick(View v) {
/*Add a method to be executed when a user clicks the button*/
EditText holdVaccineName = findViewById(R.id.editTextName);
/*Create a reference for the EditText identified by editTextName*/
TextView showNameHere = findViewById(R.id.textViewShowNameHere);
/*Create a reference for the label identified by textViewShowNameHere*/
String vaccineNameToDisplay = holdVaccineName.getText().toString();
/*Create a string variable to hold the text entered*/
showNameHere.setText(vaccineNameToDisplay + " is a Covid19 Vaccine");
/*Assign the text to the label*/
}
});
}
}
Remember that if the EditText held a value of the type double you would need to parse it as below:
Layout group exercise 1
Double co = Double.parseDouble(holdVaccineName.getText().toString());
This is a group exercise
In your group create a form with three text and label fields and a submit button. As an option, you may chose not to use EditTexts for label and use a hint instead.
Assume that the text fields are used to search for a location for a Covid19 vaccine. A user needs to enter a zip code, preferred vaccine (Pfizer, Moderna, J&J, Astrazeneca etc), and age. Make sure you label your widgets clearly.
At the end of the exercise, one team member will show the layout to the class.
Layout group excercise 2
This is a group exercise that builds on exercise 1.
In this exercise, you will code the button to display the information that the user has entered in three sentences below the submit (search) button.
The three sentences should read:
You are in zip code ????? You are ?? years old Your preferred vaccine is ?????