0% found this document useful (0 votes)
15 views6 pages

Temperature Converter

The document contains an Android layout XML and corresponding Java code for a temperature conversion app. It includes an input field for temperature, buttons to convert between Celsius and Fahrenheit, and a TextView to display the result. The Java code handles user input and performs the conversion calculations, displaying results or error messages as needed.

Uploaded by

anantrathod1221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Temperature Converter

The document contains an Android layout XML and corresponding Java code for a temperature conversion app. It includes an input field for temperature, buttons to convert between Celsius and Fahrenheit, and a TextView to display the result. The Java code handles user input and performs the conversion calculations, displaying results or error messages as needed.

Uploaded by

anantrathod1221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">

<!-- Temperature Input Field -->


<EditText
android:id="@+id/temperature_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Temperature"
android:inputType="numberDecimal"/>

<!-- Celsius to Fahrenheit Button -->


<Button
android:id="@+id/btn_to_fahrenheit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert to Fahrenheit" />

<!-- Fahrenheit to Celsius Button -->


<Button
android:id="@+id/btn_to_celsius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert to Celsius" />

<!-- Result TextView -->


<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
android:layout_marginTop="20dp"
android:gravity="center"/>

</LinearLayout>
Java file
package com.example.edittext;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText temperatureInput;


private Button btnToFahrenheit, btnToCelsius;
private TextView resultText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initializing UI elements
temperatureInput = findViewById(R.id.temperature_input);
btnToFahrenheit = findViewById(R.id.btn_to_fahrenheit);
btnToCelsius = findViewById(R.id.btn_to_celsius);
resultText = findViewById(R.id.result_text);

// Button click listener for converting Celsius to Fahrenheit


btnToFahrenheit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double celsius = Double.parseDouble(temperatureInput.getText().toString());
double fahrenheit = (celsius * 9/5) + 32;
resultText.setText("Result: " + fahrenheit + " °F");
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Please enter a valid number",
Toast.LENGTH_SHORT).show();
}
}
});
// Button click listener for converting Fahrenheit to Celsius
btnToCelsius.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double fahrenheit = Double.parseDouble(temperatureInput.getText().toString());
double celsius = (fahrenheit - 32) * 5/9;
resultText.setText("Result: " + celsius + " °C");
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Please enter a valid number",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like