Android_Slip_7A

1. Create the layout file (res/layout/activity_main.xml):
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <EditText
    android:id="@+id/editTextNumber1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Number 1"
    android:inputType="numberDecimal"/>

    <EditText
    android:id="@+id/editTextNumber2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/editTextNumber1"
    android:layout_marginTop="16dp"
    android:hint="Enter Number 2"
    android:inputType="numberDecimal"/>

    <Button
    android:id="@+id/buttonCalculate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/editTextNumber2"
    android:layout_marginTop="16dp"
    android:text="Calculate"/>
    </RelativeLayout>

1. MainActivity.java::
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    import androidx.appcompat.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity {

    private EditText editTextNumber1, editTextNumber2;
    private Button buttonCalculate;

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

    editTextNumber1 = findViewById(R.id.editTextNumber1);
    editTextNumber2 = findViewById(R.id.editTextNumber2);
    buttonCalculate = findViewById(R.id.buttonCalculate);

    buttonCalculate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    calculateAndNavigate();
    }
    });
    }

    private void calculateAndNavigate() {
    // Get the input numbers
    double num1 = Double.parseDouble(editTextNumber1.getText().toString());
    double num2 = Double.parseDouble(editTextNumber2.getText().toString());

    // Calculate power and average
    double powerResult = Math.pow(num1, num2);
    double averageResult = (num1 + num2) / 2;

    // Create an intent to navigate to ResultActivity
    Intent intent = new Intent(this, ResultActivity.class);
    intent.putExtra("powerResult", powerResult);
    intent.putExtra("averageResult", averageResult);

    // Start the ResultActivity
    startActivity(intent);
    }
    }

2. activity_result.xml (layout file for ResultActivity):
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".ResultActivity">

    <TextView
    android:id="@+id/textViewPowerResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <TextView
    android:id="@+id/textViewAverageResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textViewPowerResult"
    android:layout_marginTop="16dp"/>
    </RelativeLayout>

2. ResultActivity.java:
    import android.os.Bundle;
    import android.widget.TextView;

    import androidx.appcompat.app.AppCompatActivity;

    public class ResultActivity extends AppCompatActivity {

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

    // Get the results from the intent
    double powerResult = getIntent().getDoubleExtra("powerResult", 0.0);
    double averageResult = getIntent().getDoubleExtra("averageResult", 0.0);

    // Display the results in TextViews
    TextView textViewPowerResult = findViewById(R.id.textViewPowerResult);
    TextView textViewAverageResult = findViewById(R.id.textViewAverageResult);

    textViewPowerResult.setText("Power Result: " + powerResult);
    textViewAverageResult.setText("Average Result: " + averageResult);
    }
    }

No comments:

Post a Comment