Showing data in textView after going to next page - java

I am facing difficulties in showing data in textView after going to the next page
I am storing the return value in EMI variable but I am not able to print that value in the next page textview.
public class EmiCalculator extends AppCompatActivity {
public static double emical(double p,double r, double t)
{
double emi;
r = r / (12 * 100); // one month interest
t = t * 12; // one month period
emi = (p * r * (double)Math.pow(1 + r, t)) / (double)(Math.pow(1 + r, t) - 1);
return (emi);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emi_calculator);
EditText getText_1, getText_2, getText_3;
getText_1 = (EditText) findViewById(R.id.emi_editText_1);
getText_2 = (EditText) findViewById(R.id.emi_editText_2);
getText_3 = (EditText) findViewById(R.id.emi_editText_3);
Button calculateButton = (Button) findViewById(R.id.emi_calculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double emi_edit_Text_01 = Double.parseDouble(getText_1.getText().toString());
double emi_edit_Text_02 = Double.parseDouble(getText_2.getText().toString());
double emi_edit_Text_03 = Double.parseDouble(getText_3.getText().toString());
double emi = emical(emi_edit_Text_01, emi_edit_Text_02, emi_edit_Text_03);
String str = String.valueOf(emi);
TextView result = (TextView) findViewById(R.id.result_textView_3);
result.setText(""+emi);
Intent nextPage = new Intent(EmiCalculator.this,Result.class);
startActivity(nextPage);
}
});
}
}

you must use
Intent.putExtra
for send data to another activity.
in first activity:
Intent nextPage = new Intent(EmiCalculator.this,Result.class);
nextPage.putExtra("result",""+emi);
startActivity(nextPage);
in second activity:
Intent intentResult=this.getIntent;
if(intentResult.hasExtra("result")){
textview.setText(intent.getStringExtra("result"));
}

Related

How to add data result for four time into different textView in android?

I have create a calculate program with some condition. I want to calculate and successive result for four times. Ex. the first result will pass to the first payment, then second result will pass data by getting the old result plus the new result from first payment. Finally calculate all result from them. I don't know how to keep the old result to plus the new result. How can I do it. Below is my code:
public class MainActivity extends Activity {
private double result;
private double used;
private double price1;
private double price2;
private double price3;
private double price4;
private double price5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText use_water = (EditText)findViewById(R.id.et1);
Button btn = (Button)findViewById(R.id.btn);
final TextView pay1 = (TextView)findViewById(R.id.payment1);
final TextView pay2 = (TextView)findViewById(R.id.payment2);
final TextView pay3 = (TextView)findViewById(R.id.payment3);
final TextView pay4 = (TextView)findViewById(R.id.payment4);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
used = Double.parseDouble(use_water.getText().toString());
if(used<=15){
result = used * 550;
pay1.setText(Double.toString(result)+" Reils");
}
else if(used<=25){
result = used * 720;
pay1.setText(Double.toString(result)+" Reils");
}
else if(used<=40){
result = used * 1010;
pay1.setText(Double.toString(result)+" Reils");
}
else {
result = used * 1720;
pay1.setText(Double.toString(result)+" Reils");
}
price2 = result + result;
pay2.setText(Double.toString(result)+" Reils");
price3 = price2+(result + result);
pay3.setText(Double.toString(result)+" Reils");
price4 = price3+(price2+(result + result));
pay4.setText(Double.toString(result)+" Reils");
}
});
}
this example images
Simply, use price2, price3 and price4 as your TextViews content:
price2 = result + result;
pay2.setText(Double.toString(price2)+" Reils");
price3 = price2+(result + result);
pay3.setText(Double.toString(price3)+" Reils");
price4 = price3+(price2+(result + result));
pay4.setText(Double.toString(price4)+" Reils");
Use Reactive programming
For example,
TextView textView1 = (TextView)findViewById(R.id.payment1);;
TextView textView2 = (TextView)findViewById(R.id.payment2);;
TextView textView3 = (TextView)findViewById(R.id.payment3);;
textView1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {
//todo let textview2 know
//like textview2.setText(result + Double.parseDouble(s) + "");
}
});

First and Second guessings are the same

I am new to Java and I am developing a Number Guessing Game. When I click the random button , I get a random number that's okay but When I try this second time, second and first random numbers are the same.
How can I solve it? Here is my code:
int KullaniciTahmini;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView Sayi = (TextView) findViewById(R.id.Sayi);
Button Tamam = (Button) findViewById(R.id.Tamam);
final Button Rastgele = (Button) findViewById(R.id.Rastgele);
final EditText Tahmin = (EditText) findViewById(R.id.Tahmin);
final int gizliSayi = 0 + (int)(Math.random() * 100);
Rastgele.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int)(Math.random() * 100);
Sayi.setText("Lutfen 0 ile 100 arasinda bir deger giriniz!");
}
});
Tamam.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int KullaniciTahmini = Integer.parseInt(Tahmin.getText().toString());
if (KullaniciTahmini < gizliSayi) {
Sayi.setText("Degeri Buyult");
}
if (KullaniciTahmini > gizliSayi) {
Sayi.setText("Degeri Kucult");
}
if (KullaniciTahmini == gizliSayi) {
Sayi.setText("Dogru Cevap!");
}
}
});
}
You only assign to gizliSayi once, in onCreate:
final int gizliSayi = 0 + (int)(Math.random() * 100);
I think you want to remove the final there and then update that value in Rastgele.setOnClickListener by changing this line:
int random = (int)(Math.random() * 100);
to this:
gizliSayi = (int)(Math.random() * 100);

Displaying Double in another acitivity

I am trying to display a double from this class in another class..
So here is my code:
public class Calculator extends AppCompatActivity {
Button next;
TextView pPrice;
TextView renovations;
TextView misc2;
TextView util;
TextView rep;
TextView mortage;
TextView misc1;
TextView rent;
public double getStartingCostsResult() {
return startingCostsResult;
}
double startingCostsResult;
double monthlyMinus;
double monthlyPlus;
double monthlyROI;
double yearlyROI;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
// Setting these textviews to those in the xml.
pPrice = (TextView) findViewById(R.id.pPrice);
renovations = (TextView) findViewById(R.id.renovations);
misc2 = (TextView) findViewById(R.id.misc2);
util = (TextView) findViewById(R.id.util);
rep = (TextView) findViewById(R.id.rep);
mortage = (TextView) findViewById(R.id.mortage);
misc1 = (TextView) findViewById(R.id.misc);
rent = (TextView) findViewById(R.id.rent);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent expense = new Intent(getApplicationContext(), Results.class);
if ((pPrice.getText().length() > 0) && (renovations.getText().length() > 0) && (misc2.getText().length() > 0)) {
double price = Double.parseDouble(pPrice.getText().toString());
// double costs = Double.parseDouble(cCosts.getText().toString());
double reno = Double.parseDouble(renovations.getText().toString());
double misc = Double.parseDouble(misc2.getText().toString());
startingCostsResult = price + reno + misc;
if((util.getText().length()>0) && (rep.getText().length()>0) && (mortage.getText().length()>0) && (misc1.getText().length()>0)){
double utilities = Double.parseDouble(util.getText().toString());
double repairs = Double.parseDouble(rep.getText().toString());
double mort = Double.parseDouble(mortage.getText().toString());
double miscsell = Double.parseDouble(misc1.getText().toString());
monthlyMinus = utilities + repairs + mort + miscsell;
if (rent.getText().length()>0){
double monthlyRent = Double.parseDouble(rent.getText().toString());
monthlyPlus = monthlyRent;
monthlyROI = monthlyPlus - monthlyMinus;
yearlyROI = monthlyROI *12;
startActivity(expense);
}else{
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
So I am trying to display the yearlyROI double in another class.
I have tried this:
Calculator calc = new Calculator();
otherClass.setText((int) calc.yearlyROI);
But my app crashes when I click next.
you should put an extra in the expense intent like this.
expense.putExtra("yearlyRoi",yearlyRoi);
then in the nexet activity you can get it like this.
Intent recievedIntent = this.getIntent();
double yearlyRoi = recievedIntent.getDoubleExtra("yearlyRoi", defaultValue);
default value can be 0.0 or anything you want.
as for the crash i think its another problem,you need to give us error log of your app.
If you want to access variables from a different Activity you need to add them to your intent.
In your case:
expense.putExtra("yearlyROI", yearlyROI);
startActivity(expense);
Then in your new Activity:
double yearlyROI = getIntent().getDoubleExtra("yearlyROI");
Hope it helps!

Difficulty saving and storing

I am trying to save and store data in an android app using java. At the moment the data will not save and it causes my app to crash. Can anyone make any suggestions to my code? Part of my page includes a total budget and I am difficulty storing and saving the total budget.
public class Summary extends Activity implements TextWatcher, View.OnClickListener
{
DecimalFormat df = new DecimalFormat("£0.00");
int noOfGifts, giftsPurchased;
double cost;
EditText budgetEntered;
double savedBudget = 0;
String budgetString;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.summary);
budgetEntered = (EditText) findViewById(R.id.s2TotalBudget);
budgetEntered.addTextChangedListener(this);
Button saveBudget = (Button) findViewById(R.id.s2ViewList);
saveBudget.setOnClickListener(saveButtonListener);
if(savedBudget != 0)
{
saveBudget.setText(budgetString);
}
Bundle passedInfo = getIntent().getExtras();
if (passedInfo != null)
{
cost = passedInfo.getDouble("cost");
noOfGifts = passedInfo.getInt("noOfGifts");
giftsPurchased = passedInfo.getInt("giftsPurchased");
}
Button logoutButton = (Button) findViewById(R.id.s2LogoutButton);
logoutButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(Summary.this, MainActivity.class);
startActivity(myIntent);
}
});
Button viewList = (Button) findViewById(R.id.s2ViewList);
viewList.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(Summary.this, GiftList.class);
startActivity(myIntent);
}
});
String [][] summary = {{"Number of Presents to buy: ", (noOfGifts + "")},
{"Number of Presents bought:", (giftsPurchased + "")},
{"Cost: £", (cost + "")},
{"Budget: £", "50"}};
String passedBudget=null;
//convert totalPresents to double from String
String tempPresents = summary[0][1];
int presents = Integer.parseInt(tempPresents);
//convert presentsBought to double from String
String tempBought = summary[1][1];
int presentsToBuy = Integer.parseInt(tempBought);
//Number of presents
TextView s2PresentResult = (TextView) findViewById(R.id.s2PresentsResult);
s2PresentResult.setText(summary[0][1]);
//Number of presents to buy
TextView s2PresentsBuyResult = (TextView) findViewById(R.id.s2PresntsBuyResult);
s2PresentsBuyResult.setText((noOfGifts - giftsPurchased) + "");
Bundle passedId = getIntent().getExtras();
if (passedId != null)
{
passedBudget = passedId.getString("Enter Budget");
}
//EditText s2TotalBudget = (EditText) findViewById(R.id.s2TotalBudget);
//s2TotalBudget .addTextChangedListener((android.text.TextWatcher) this);
//s2TotalBudget .setText(passedBudget, TextView.BufferType.EDITABLE);
//Number of people
//TextView s2TotalBudget = (TextView) findViewById(R.id.s2TotalBudget);
//s2TotalBudget.setText("Enter budget");
//Number of people
TextView s2TotalCost = (TextView) findViewById(R.id.s2TotalCost);
s2TotalCost.setText(df.format(Double.parseDouble(summary[2][1])));
//Output if over or under budget
TextView s2CalculateOverBudget = (TextView) findViewById(R.id.s2CalculateOverBudget);
//convert totalCost to double from String
String temp = summary[2][1];
double totalCost = Double.parseDouble(temp);
//convert totalBudget to double from String
String tempTwo = "14";
double totalBudget = Double.parseDouble(tempTwo);
if((totalCost>totalBudget)&&(totalBudget!=0))
{
s2CalculateOverBudget.setTextColor(Color.rgb(209,0,0));
s2CalculateOverBudget.setText("You are over budget");
}
else if(totalBudget==0){
s2CalculateOverBudget.setText("");
}
else {
s2CalculateOverBudget.setText("You are within budget");
}
}
public View.OnClickListener saveButtonListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(budgetEntered.getText().length()>0)
{
budgetString = budgetEntered.getText().toString();
}
}
};
public void onClick(View v)
{
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
#Override
public void afterTextChanged(Editable s)
{
this it the best way to store and load value in Android:
save values: (put this where you want to save the values, for example in the onStop or onPause method. Or, in your case, in the onClick method)
SharedPreferences settings = getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("testValue", value);
editor.commit();
load values:
SharedPreferences settings = getSharedPreferences("MyPref", 0);
value = settings.getInt("testValue", defValue);

whats wrong with the android code ?

whats wrong with the android code ? I want this code to give Incorrect when the
entered answer is wrong and correct when the answer is correct but every time
get is incorrect
and are my casting variables correct
public class MainActivity extends Activity {
TextView Jlabel1;
TextView Jlabel2;
TextView Jlabel3;
EditText Jtextbox1;
Button b1;
int m;
int ans;
String ans1;
String k;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Jlabel1 = (TextView) findViewById(R.id.textView1);
Jlabel2 = (TextView) findViewById(R.id.textView2);
Jlabel3 = (TextView) findViewById(R.id.textView3);
Jtextbox1 = (EditText) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.button1);
double q = Math.random();
double w = Math.random();
int e = (int) (q * 10);
int z = (int) (w * 10);
ans = e + z;
Jlabel1.setText(Integer.toString(e));
Jlabel2.setText(Integer.toString(z));
ans1 = String.valueOf(ans);
k = Jtextbox1.getText().toString();
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (ans1.equals(k)) {
Jlabel3.setText("Correct");
} else {
Jlabel3.setText("Incorrect");
}
}
});
}
}'
You're getting a copy of the contents of the edittext too early. Move the
k = Jtextbox1.getText().toString();
inside the onClick().

Categories

Resources