Difference between revisions of "Mobile App Development"
(Created page with " ==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...") |
|||
| Line 27: | Line 27: | ||
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. | 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 | + | |
| − | + | <resources <!-- The value %1$1 means First placeholder of string value --> | |
<string name="app_name">Display A Vaccine</string> | <string name="app_name">Display A Vaccine</string> | ||
<string name="buttonValue">Show Vaccine</string> | <string name="buttonValue">Show Vaccine</string> | ||
<string name="nameLabel">Vaccine</string> | <string name="nameLabel">Vaccine</string> | ||
<string name="showNameHint">Enter a vaccine</string> | <string name="showNameHint">Enter a vaccine</string> | ||
| − | <string name="header_message">%1$s! That is a Covid19 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 | Here is an example of how this code could be used in a layout.xml file show the text for a button | ||
Revision as of 11:36, 27 June 2021
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:
Double co = Double.parseDouble(holdVaccineName.getText().toString());