0

I currently have a calculation being done in my main activity. The calculation is done in function below. The result displays in a text box on the same screen.

ShowNumber.setText(buClickValue)

 Bill = buClickValue

  fun buCalculateEvent(view: View) {
       var Total = "%.2f".format(Bill.toDouble() * .15)

      ShowNumber.setText("Based on a 15% tip, you would be " + ( Bill.toDouble() + Total.toDouble()))
     }

My issue is trying to get the setText message to appear in another activity. I know how to access the activity via the click of a button. I also know how to generate a toast message in another activity. How can I send information to a textview in another activity?

Thanks in Advance.

PrimeOfKnights
  • 426
  • 5
  • 17
John
  • 1
  • 1
  • Possible duplicate of [How to pass the values from activity to another activity in kotlin](https://stackoverflow.com/questions/45157567/how-to-pass-the-values-from-activity-to-another-activity-in-kotlin) – David Hackro Aug 28 '17 at 20:24

2 Answers2

2

I'm not a Kotlin developer, but I think it does starts Activity trough startActivity, and before starting it, you can add your value into the intent with setIntegerExtra(A_KEY, value) (or any other extra) then in the second activity you get with:

int a = getIntent().getIntegerExtra(A_KEY)
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
1

I want to translate Marcos' answer in kotlin.

Before starting the second activity, put the value in the intent like this;

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("total", theValueTotal)
startActivity(intent)

then in your second activity you can get the value using this;

val total = intent.getDoubleExtra("total", 0.0)
Hohenheim
  • 393
  • 2
  • 16
  • Let me add more context:I have a variable calculation that I want to send to the second activity. However, I do not know how to make that appear inside of a text view. What you described to me above works, but how would I make the 0.0 appear in a text view. Would I used the syntax below? var totalDisplay: Double = resultsBox.display.toDouble() – John Aug 29 '17 at 15:48
  • You can call the view inside your second activity's oncreate then call setText. `secondActivityTextView.setText("base on 15%... $total")`. is that what you mean? – Hohenheim Aug 29 '17 at 16:03
  • Yes. That is exactly what I am trying to do. My second activity is called "resultScreen". The textView has an ID of "resultBox". So according to the syntax above, I should write "resultbox.setText("based on 15%......$total")? – John Aug 29 '17 at 16:18
  • if your not using kotlin-extension then you should get your TextView using `findViewById()` then cast it as textview like this, `val mTextview = findViewById(R.id.resultBox) as TextView` then you can call `mTextView.setText("base$total")` – Hohenheim Aug 29 '17 at 16:37
  • The cast your mentioning is not working. I am using kotlin on the Android Studio 3 beta. It already has the kotlin extension built in. When I type in your suggestion, I get errors. – John Aug 29 '17 at 16:40
  • you cant send the variable, you can send the value of the variable, thats what my answer is doing, put your variable in that `theValueTotal` in you first activity and get it using `getDoubleExtra()` in the second activity. I hope it helps. – Hohenheim Aug 29 '17 at 16:44
  • okay so you can do `resultBox.text = "base... $total"` assuming you assign your `getDoubleExtra()` in varialble `total` – Hohenheim Aug 29 '17 at 16:49
  • Now it makes sense. All I have to do is get the value to return from the function it is being calculated in. Thanks! – John Aug 29 '17 at 17:06