Mobile App Development
Using the Android Studio for Android Development
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 ?????
Group excercise 2 code
package com.gitobu.groupexercise2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.view.inputmethod.InputMethodManager; 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);
displayEnteredInfo();
}
private void displayEnteredInfo(){ /*The name of the method is "displayEnteredInfo"*/
/*The method is only used in this class (private)
The method does not return any value(void)*/
Button buttonDisplay = findViewById(R.id.buttonSearch);
/*Associated the code with the button named buttonSearch in the layout*/
buttonDisplay.setOnClickListener(new View.OnClickListener(){
/*Declare a listener for the actions of the button*/
@Override
/*Add a method to be executed when a user clicks the button*/
public void onClick(View v) {
hideKeyboard(); /*Calls method to hide the keyboard*/
/*Create a variable to hold the data entered on the EditTexts*/
EditText holdAge = findViewById(R.id.editTextAge);
EditText holdZipCode = findViewById(R.id.editTextZipCode);
EditText holdPreferredVaccine = findViewById(R.id.editTextTextPreferredVaccine);
/*Create a variable for each TextView and associate it with the textview identifier*/
TextView showAgeHere = findViewById(R.id.textViewDisplayAge);
TextView showZipCodeHere = findViewById(R.id.textViewDisplayZipCode);
TextView showPreferredVaccine = findViewById(R.id.textViewDisplayPreferredVaccine);
/*Create a variable for the text on the EditText*/
String ageToDisplay = holdAge.getText().toString();
String zipCodeToDisplay = holdZipCode.getText().toString();
String preferredVaccineToDisplay = holdPreferredVaccine.getText().toString();
/*You may use the res folder to access Strings.xml values to format presentation*/
Resources resources = getResources();
String resAgeToDisplay = resources.getString(R.string.age_message,ageToDisplay);
showAgeHere.setText(resAgeToDisplay);
/*Or you may hard code it here*/
showZipCodeHere.setText("Your zipe code is " + zipCodeToDisplay);
String respreferredVaccineToDisplay = resources.getString(R.string.pref_vaxx_message,preferredVaccineToDisplay);
showPreferredVaccine.setText(respreferredVaccineToDisplay);
}
});
}
/*This method is used to hide the keyboard*/
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editName = findViewById(R.id.editTextAge);
imm.hideSoftInputFromWindow(editName.getWindowToken(), 0);
EditText editAddress = findViewById(R.id.editTextZipCode);
imm.hideSoftInputFromWindow(editAddress.getWindowToken(), 0);
EditText editCity = findViewById(R.id.editTextTextPreferredVaccine);
imm.hideSoftInputFromWindow(editCity.getWindowToken(), 0);
}
}
Group exercise 2 strings file
<resources>
<string name="app_name">Group Exercise 2</string> <string name="age_message">Your age is %1$s!</string> <string name="pref_vaxx_message">Your preferred vaccine is %1$s!</string>
</resources>
Group excercise 2 Main Activity file
<?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/textViewAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="104dp"
android:text="Age"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewZipCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:text="Zip Code"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAge" />
<TextView
android:id="@+id/textViewPreferredVaccine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:text="Preferred Vaccine"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewZipCode" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:ems="10"
android:hint="Enter your age"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@id/textViewAge"
app:layout_constraintStart_toEndOf="@+id/textViewAge" />
<EditText
android:id="@+id/editTextZipCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginLeft="120dp"
android:ems="10"
android:hint="Enter your zip code"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@id/textViewZipCode"
app:layout_constraintStart_toEndOf="@+id/textViewZipCode" />
<EditText
android:id="@+id/editTextTextPreferredVaccine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:ems="10"
android:hint="Enter preferred vaccine"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@id/textViewPreferredVaccine"
app:layout_constraintStart_toEndOf="@+id/textViewPreferredVaccine" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginBottom="445dp"
android:text="Search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPreferredVaccine"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textViewDisplayAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="64dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonSearch" />
<TextView
android:id="@+id/textViewDisplayZipCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewDisplayAge" />
<TextView
android:id="@+id/textViewDisplayPreferredVaccine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewDisplayZipCode" />
</androidx.constraintlayout.widget.ConstraintLayout>
Android Assignment 1
This assignment is based on the class textbook on page 39, question 4
Create a project called TipCalculator
The main activity layout should contain one EditText, three buttons, and one TextVeiw (Figure 3.10) The first button should be labeled 15% and should take the amount entered in the EditText and calculate 15% of that value. The second button should be labeled 18% and should take the amount entered in the EditText and calculate 18% of that value. The first button should be labeled 20% and should take the amount entered in the EditText and calculate 20% of that value. All the buttons should display the tip and total bill in the TextView with the format: Tip: $99.99, Total Bill $99.99 The widgets should be centered horizontally on the screen with the EditText on the top, the button below it in a single row, and the TextView below the button.
To submit your assignment please create a compressed (zipped) file of the entire project folder and upload it
Creating app screen
sections Creating app screen sections item options Creating app screen sections Android operation is centered on Activities, Layouts, and Intents.
The primary components of the app are Activities - An activity contains code that manages the widgets on a layout. Layouts - a layout could have containers such as Recycler Views or Scroll Views. You may add widgets on these
- These provide the framework by which a user interacts with an app.
- Intents are objects that are used to switch between activities and apps
- An activity is a java class while a layout is an extensible markup language (xml) file.;/