I am a new in the android and java universe, but i have project that i have construct a app like a "Tip Calculator".
The guides for this project are:
Write an app that will have two activities.
FIRST ACTIVITY
Label "Meal Amount", "Tax", "Tip", "Total" and corresponding text views
User can enter meal amount
Tax, tip and total are calculated (Note: you may need to set certain property to make these TextViews read only)
There is a button called "Calculate" that when pressed will spawn the SECOND ACTIVITY
Once the calculations are done, display the amounts
SECOND ACTIVITY
- Label "Tax Percentage" and corresponding TextView where the user will enter the tax percentage
- Buttons denoting service quality "Poor 8%", "Average 10%", "Good 15%", and "Outstanding 20%"
- When one of these buttons is pressed, the app needs to calculate the required amounts and pass the results back to the FIRST ACTIVITY
My problem is when i start the first activity and i need do a function that run only when return from the second activity and no when start the application. But i can't do.
Below I pasted my code of the first and second activity.
MainActivity.java
package com.example.viniciuscardoso.tipcalculator;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public Button mCalculateButton;
public double Tax = 7;
public double Tip = 10;
public TextView TextViewTax;
public TextView TextViewTip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextViewTax = (TextView)findViewById(R.id.TextViewTax);
TextViewTip = (TextView)findViewById(R.id.TextViewTip);
mCalculateButton = (Button)findViewById(R.id.calculate);
TextViewTax.setText(String.valueOf(Tax));
TextViewTip.setText(String.valueOf(Tip));
final Intent intent = new Intent(this, ActivityTip.class);
mCalculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
}
ActivityTip.java
package com.example.viniciuscardoso.tipcalculator;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ActivityTip extends ActionBarActivity {
Button mPoorButton;
Button mAverageButton;
Button mGoodButton;
Button mOutstandingButton;
EditText mTaxEdit;
String tax;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_tip);
mPoorButton = (Button)findViewById(R.id.poor_button);
mAverageButton = (Button)findViewById(R.id.average_button);
mGoodButton = (Button)findViewById(R.id.good_button);
mOutstandingButton = (Button)findViewById(R.id.outstanding_button);
mTaxEdit = (EditText)findViewById(R.id.edit_tax);
final Intent intent = new Intent(this, MainActivity.class);
tax = mTaxEdit.getText().toString();
mPoorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("tax_percentage", tax);
intent.putExtra("tip_percentage", 8.0);
startActivity(intent);
}
});
mAverageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("tax_percentage", tax);
intent.putExtra("tip_percentage", 10.00);
startActivity(intent);
}
});
mGoodButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("tax_percentage", tax);
intent.putExtra("tip_percentage", 15.00);
startActivity(intent);
}
});
mOutstandingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("tax_percentage", tax);
intent.putExtra("tip_percentage", 20.0);
startActivity(intent);
}
});
}
}
Someone can help with this problem.
If you need the .xml with the layouts let me know.
Thank you!