CEIS320 Lab 3 – User Inputs – Code completed in Android Studio – With Screenshots – Perfect Solution – Instant Delivery

Lab Price = $14
Please feel free to send us your queries at: [email protected]
Payment methods

Add to Cart

Buy Now

View Cart


Problem Statement

Week 3: Lab Overview
TABLE OF CONTENTS

Lab 3: User Inputs (50 points)
Lab Overview
Scenario/Summary
In this lab, you will be capitalizing on the previous lab by creating user inputs and adding functionality. You will start by creating functions to handle the temperature conversion and accept user input to convert the user input. We will also add an alert to the user. At this point, you must pay close attention to keeping your code organized and readable, so it is best to have a portion of your activity code that is just dedicated to inputs only. When you are finished creating your application, submit all of your open-source files.
Your project will include the following.
• Open-source .java files
• A zipped project file
Deliverables
• Completion of Steps 1–3
Grading Rubric Points
Completion of Steps 1–3 50 points
TOTAL 50 points
Required Software
This lab will use the following Lab Resources.
• Android Studio
Use a personal copy of the software or to access the Lab Resources, go to the Lab Resources section of the Course Resources page.
Lab Steps
Step 1: Accepting User Input
Open the IntroAndroidApp from last week. You can do this by opening Android Studio and clicking File-> Open. You should see the AndroidStudioProjects folder. Your project should be in there. In this lab, we will allow the user to enter a temperature in a text box, then choose the units for their temperature, press the convert button, and see the converted temperatures.
Open strings.xml and add a new string.
Please enter the temperature and select the correct units then press Convert:
Go back to content_main.xml and add a TextView and an EditText below the title Choose Your Temperature. You may need to move the radio group down. The text view should display the text above from the strings.xml file.
The id of the Edit Text text box should be txtInputTemp.
Below the button, add three more text views for the output. These text views will display the converted temperatures. The ids should be lblOutputF, lblOutputC, and lblOuptutK.
Design View
Now we need to add the event handlers to the Java Code. Open MainActivity.Java. Import the OnClickListener.
import android.view.View.OnClickListener;
Then use the current class as a listener by adding the code in bold.
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, OnClickListener{
Now we need to create variables for the radio buttons, text boxes, and labels. Create these variables in the class as private variables. Add all the variables listed below.
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, OnClickListener{

//Declare variables
private RadioButton btnFah;
private RadioButton btnCel;
private RadioButton btnKel ;
private EditText txtInputTemp;
private TextView lblOutputDegF;
private TextView lblOutputDegC;
private TextView lblOutputDegK;
private Button btnConvert;

Then implement the interface for the listener. This will go below the onCreate method. In this method, we will display the temperature based on what the user picks. The code below is very long but is necessary to handle each case. The Math.round is necessary to round to two decimal places.
@Override
public void onClick(View v) {

//Get Resources for radio buttons, text boxes, and labels
btnFah = (RadioButton) findViewById(R.id.btnDegF);
btnCel = (RadioButton) findViewById(R.id.btnDegC);
btnKel = (RadioButton) findViewById(R.id.btnDegK);
txtInputTemp = (EditText) findViewById(R.id.txtInputTemp);
lblOutputDegF = (TextView) findViewById(R.id.lblOutputF);
lblOutputDegC = (TextView) findViewById(R.id.lblOutputC);
lblOutputDegK = (TextView) findViewById(R.id.lblOutputK);
//Convert the input temperature to double
double temp = Double.parseDouble(String.valueOf(txtInputTemp.getText()));
double answer=0;

//If the user choose Fahrenheit, convert to Celsius and Kelvin and display in those text boxes
if(btnFah.isChecked()) {
lblOutputDegF.setText(temp+” degrees F”);
lblOutputDegC.setText((Math.round((temp-32)*5/9*100.0)/100.0)+” degrees C”);
lblOutputDegK.setText((Math.round((temp+459.67)*5/9*100.0)/100.0)+” degrees K”);
}

//If the user choose Celsius, convert to Fahrenheit and Kelvin and display in those text boxes
if(btnCel.isChecked()) {
lblOutputDegF.setText((Math.round(((temp*9)/5+32)*100.0)/100.0)+” degrees F”);
lblOutputDegC.setText(temp+” degrees C”);
lblOutputDegK.setText((Math.round((temp+273.15)*100.0)/100.0)+” degrees K”);
}

//If the user choose Kelvin, convert to Fahrenheit and Celsius and display in those text boxes
if(btnKel.isChecked()) {
lblOutputDegF.setText((Math.round((temp*9/5-459.67)*100.0)/100.0)+” degrees F”);
lblOutputDegC.setText((Math.round((temp-273.15)*100.0)/100.0)+” degrees C”);
lblOutputDegK.setText(temp+” degrees K”);
}

}
Finally we need to set the listener.
Add the code in bold below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set listener
RadioGroup tempGroup = (RadioGroup) findViewById(R.id.tempGroup);
tempGroup.setOnCheckedChangeListener(this);

btnConvert =(Button) findViewById(R.id.btnConvert);
btnConvert.setOnClickListener(this);
In Week 2, we displayed a message when the user chose a radio button. You can keep the message, comment it out, or delete it.
Save your work and then start the emulator and run the program. Type in a temperature using the keyboard.

Once you are finished typing in a temperature, use the arrow on the emulator (to the left of the home button) to hide the keyboard. Then choose one of the radio buttons and press convert. You should see the converted temperatures.

Step 2: Adding a Toast

Transcript
Next we want to display a toast. A toast is a brief message displayed on the user interface. They are useful for certain situations and can be very helpful for debugging.
Displaying a toast requires three steps.
1. Import the toast class. Enter the following code below the other import statements.
import android.widget.Toast;
2. Create a toast object by calling the makeText method of the toast class. We need to pass this object three parameters.
1. Context, which determines where the toast is displayed
2. Message, which is any string you want can be displayed for this parameter
3. Length of time to display, which is where you can choose LENGTH_SHORT or LENGTH_LONG
We will do this by creating a private helper method to display the toast. The code is listed below.
3. Call the show method from the toast object that you created. Toasts are helpful for debugging, as well. You can use a toast to display a message to the user.
We need to create a private helper method. In our method, we want the toast to display a message based on the temperature in Celsius. So we will pass the temperature to the toast method, as well. Add the following method.
private void displayToast( double temperature) {
if(temperature>50)
Toast.makeText(this, “Wow it’s hot outside!”, Toast.LENGTH_LONG).show();
else if(temperature>20)
Toast.makeText(this, “Nice weather we are having”, Toast.LENGTH_LONG).show();
else
Toast.makeText(this, “Brrrrr – it’s cold out!”, Toast.LENGTH_LONG).show();
}
Now we need to call this method. Because we are calling this method based on the temperature in Celsius, we need to set that temperature equal to a double variable. We have declared a variable named answer above in the public void onClick() method. We will use that variable to grab the Celsius temperature. Add the bold code to your onClick method.
if(btnFah.isChecked()) {

lblOutputDegF.setText(temp+” degrees F”);

lblOutputDegC.setText((Math.round((temp-32)*5/9*100.0)/100.0)+” degrees C”);

lblOutputDegK.setText((Math.round((temp+459.67)*5/9*100.0)/100.0)+” degrees K”);

answer = (temp-32)*5/9;

}

//If the user choose Celsius, convert to Fahrenheit and Kelvin and display in those text boxes

if(btnCel.isChecked()) {

lblOutputDegF.setText((Math.round(((temp*9)/5+32)*100.0)/100.0)+” degrees F”);

lblOutputDegC.setText(temp+” degrees C”);

lblOutputDegK.setText((Math.round((temp+273.15)*100.0)/100.0)+” degrees K”);

answer = temp;

}

//If the user choose Kelvin, convert to Fahrenheit and Celsius and display in those text boxes

if(btnKel.isChecked()) {

lblOutputDegF.setText((Math.round((temp*9/5-459.67)*100.0)/100.0)+” degrees F”);

lblOutputDegC.setText((Math.round((temp-273.15)*100.0)/100.0)+” degrees C”);

lblOutputDegK.setText(temp+” degrees K”);

answer = temp-273.15;

}

displayToast(answer);
Save your work. We have one more feature to add to our code before running it. It would be much nicer for the user if the number keypad showed rather than the text, because we know we only need to enter numerical values. Go to the content_main.xml and the EditText element to enter in the temperature. Add the bold code below (don’t worry if some of the formatting elements look different in your code).

Save your work. Start your emulator and run the program. Try multiple different temperatures and note the toasts that appear.
Number Keypad

Output with toast
Step 3: Submission
Save and zip your entire project workspace and directory files (which must include all of your open-source files), and submit them.
Don’t forget to submit your lab.

Relevant Material
Screenshots
Lab 3: Step 1
Lab 3: Step 1
Lab 3: Toast
Lab 3: Toast
Lab 3: Toast
Lab 3: Toast
Instructions
* If you want to purchase multiple products then click on “Buy Now” button which will give you ADD TO CART option.Please note that the payment is done through PayPal.
* You can also use 2CO option if you want to purchase through Credit Cards/Paypal but make sure you put the correct billing information otherwise you wont be able to receive any download link.
* Your paypal has to be pre-loaded in order to complete the purchase or otherwise please discuss it with us at [email protected].
* As soon as the payment is received, download link of the solution will automatically be sent to the address used in selected payment method.
* Please check your junk mails as the download link email might go there and please be patient for the download link email. Sometimes, due to server congestion, you may receive download link with a delay.
* All the contents are compressed in one zip folder.
* In case if you get stuck at any point during the payment process, please immediately contact us at [email protected] and we will fix it with you.
* We try our best to reach back to you on immediate basis. However, please wait for atleast 8 hours for a response from our side. Afterall, we are humans.
* Comments/Feedbacks are truely welcomed and there might be some incentives for you for the next lab/quiz/assignment.
* In case of any query, please donot hesitate to contact us at [email protected].
* MOST IMPORTANT Please use the tutorials as a guide and they need NOT to be used for any submission. Just take help from the material.
******************************************** Good Luck ***************************************************
Privacy Policy
We take your privacy seriously and will take all measures to protect your personal information.
Any personal information received will only be used to fill your order. We will not sell or redistribute your information to anyone.
Refund Policy
Incase you face any issues with the tutorial, please free to contact us on [email protected]
We will try our best to resolve the issue and if still persists we can discuss for a refund in case its required.
Payment Details
Lab Price = $14
Please feel free to send us your queries at: [email protected]
Payment methods

Add to Cart

Buy Now

View Cart