Display text to next activity kotlin - android

I was looking for my problem on the internet, but the solutions given still do not work.
How to transfer data from first Activity to second?(plain text)
MainActivity
btn.setOnClickListener {
val player1= findViewById<EditText>(R.id.et1) as EditText
val player2= findViewById<EditText>(R.id.et2) as EditText
val intent1= Intent(this, MainActivity3::class.java).apply {
putExtra("player1",player1.getText().toString())
putExtra("player2",player2.getText().toString())
}
startActivity(intent1)
}
Second Activity
fun PlayGame(cellID:Int,buSelected:Button){
val playe1= intent.getStringExtra(EXTRA_MESSAGE)
val playe2= intent.getStringExtra(EXTRA_MESSAGE)
val textView2= findViewById<TextView>(R.id.textView2).apply{
text= playe1
text=playe2
}
if(ActivePlayer==1){
textView2.setText(": $playe1")
buSelected.text="0"
buSelected.setBackgroundResource(R.color.blue)
player1.add(cellID)
ActivePlayer=2
}else{
textView2.setText(": $playe2")
buSelected.text="X"
buSelected.setBackgroundResource(R.color.green)
player2.add(cellID)
ActivePlayer=1
}
buSelected.isEnabled=false
CheckWiner()
}
I want the player's name from MainActivity goes to Second ;p

The problem is in the way you get your values. Try this:
val playe1= intent.getStringExtra("player1")
val playe2= intent.getStringExtra("player2")

Use a sharedpreferences, in your next activity after saving the values to a Val or var delete the shared preferences continue.
It’s not ideal but it just may get you to where you can at least interact with that data.
I used this method for something similar last night. If I have time tonight I was going to post a how to on this on my website. If I can I’ll swing by here and edit my answer. With code and explanation unless you get it figured out by then.

Related

How can get rid of the multiply Intents

I have 2 activities. And I need pass a lot of variables from one activity to another activity:
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(ACCOUNT1, accoun1)
intent.putExtra(ACCOUNT2, accoun2)
intent.putExtra(ACCOUNT3, accoun3)
intent.putExtra(ACCOUNT4, accoun4)
intent.putExtra(ACCOUNT5, accoun5)
intent.putExtra(ACCOUNT6, accoun6)
intent.putExtra(ACCOUNT7, accoun7)
intent.putExtra(ACCOUNT8, accoun8)
intent.putExtra(ACCOUNT9, accoun9)
and so on. How can I make this transfer easier? Maybe is some other ways to pass multiply values from one activity to another activity?
I recently had a similar case.
How I solved:
I created an entity that kept all the information for me, that way, I just passed an object between activities, but I had everything I needed inside that object.
Hope this can help.
There is a bundleOf function in the Jetpack core-ktx library that works like mapOf.
val intent = Intent(this, SecondActivity::class.java)
val extras = bundleOf(
ACCOUNT1 to accoun1,
ACCOUNT2 to accoun2,
ACCOUNT3 to accoun3,
// etc.
)
intent.putExtras(extras)
But that doesn't save much code compared to just using apply unless you use the same bundle of extras in multiple places in your code:
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra(ACCOUNT1, accoun1)
putExtra(ACCOUNT2, accoun2)
putExtra(ACCOUNT3, accoun3)
//...
}
You could make a parcelable data class that stores all your values and pass that instead. See instructions here for creating the class easily: https://developer.android.com/kotlin/parcelize
#Parcelize
data class AccountInfo(
val account1: String,
val account2: String,
val account3: String,
//...
): Parcelable
Then you can put this into your bundles, and you don't have to create a bunch of the same kinds of variables in both activities, because all the variables are inside one class.
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra(ACCOUNT_INFO, myAccountInfoInstance)
}
if all of your variables has the same type you can add all to list and pass the list to second activity/fragment.

For a custom ActivityResultContract, what's the proper way to pass multiple input values?

I'm learning the Activity Results API that replace the onActivityResult. I understand the concept and basic usage, but not sure what's the proper way to setup an intent that takes multiple input extras.
For example, I want to put two Boolean flags and one String to the intent, in the old way, I could do
Val intent = Intent(this, AnotherActivity::java.class)
intent.putExtra(key1, false)
intent.putExtra(key2, true)
intent.putExtra(key3, someString)
But how do I do this in createIntent method? And what would the input type for this custom ActivityResultContract?
One way I can see is using Intent as input type, like class CustomActivityContract : ActivityResultContract<Intent, Unit>(). But that doesn't feel right to me.
Any suggestion is appreciated!
You don't call createIntent() directly. Instead, pass your intent to the ActivityResultLauncher.launch() method.
val launcher = registerForActivityResult(StartActivityForResult()) { result ->
// handle result
}
launcher.launch(
Intent(this, AnotherActivity::class.java)
.putExtra(key1, false)
.putExtra(key2, true)
.putExtra(key3, someString)
)

Sending information between Activities in Kotlin

fun kullaniciOlustur2(view: View){
val intent = Intent(applicationContext,KullaniciOlustur2::class.java)
intent.putExtra("input",makeUsername.text.toString())
intent.putExtra("input2",makeUserphone.text.toString())
startActivity(intent)
}
Mainactivity2 starts here..before this, I was using
val intent = intent
val received: String = intent.getStringExtra("input")
makeUsername.text = received
But this method doesn't work anymore.
I tried using getIntent() but couldn't get anything
val intent = getIntent()
Try this code
Activity 1
val intent = Intent(FirstActivity.this,SecondActivity::class.java) //not application context
intent.putExtra("input",makeUsername.text.toString())
intent.putExtra("input2",makeUserphone.text.toString())
startActivity(intent)
Activity 2
inside onCreate() method use
val stringOne = getIntent().getStringExtra("input")
Or more cleaner way is
val extras = getIntent().getExtras()
if (null != extras) {
val value = extras.getString("input")
//The key argument here must match that used in the other activity
}
and please check similar answers in Java, you may be able to get the Idea here already told in another answer.
I also Use Anko to remove this kind of boilerplate code
I recommend using Kotlin Anko, there are plenty of methods that will help you to remove this boilerplate code
check Anko Intents here
Use Below Code:
The Code is same as Java .Just difference is kotlin do not have
1. No semicolon at the end.
2. to call another activity kotlin use ::
ex.KotlinActivity::class.java
startActivity(Intent(this, KotlinActivity::class.java).putExtra("DataTrasfer", ""))
To Get Value:
intent.getStringExtra("DataTrasfer")

How to trigger the lost focus event for a TextEdit using Robolectric?

I have a 2 TextEdits on the screen. And they all have a OnFocusChangeListener which will check the text in the field, if it's empty it gonna set the error property to the error message. It works according to my ad-hoc test.
But I don't know how to verify this using Robolectric? I tried this:
#RunWith(RobolectricTestRunner::class)
class AuthLogDetailActivityUITest {
#Test
fun should_show_error_when_no_text_input_when_textInput_lost_focus() {
val logDetailsActivity = Robolectric.setupActivity(AuthLogDetailsActivity::class.java)
val quickNameTextEdit = logDetailsActivity.findViewById<TextInputEditText>(R.id.loginDetails_textEdit_quickName)
val passwordTextEdit = logDetailsActivity.findViewById<TextInputEditText>(R.id.loginDetails_textEdit_password)
quickNameTextEdit.performClick()
passwordTextEdit.performClick()
assertNotNull(quickNameTextEdit.error)
}
}
I tried to mimic the real world, where the user just click the text edit one by one without entering anything.
But in the real world, as soon as the user clicks the passwordTextEdit, quickNameTextEdit should show the error message.
But the test will always fail because the quickNameTextEdit.error is null.
I think it's not the way to trigger that onFocusChange event. What is the Robolectric way to test this behaviour?
I was able to get my OnFocusChangeListener triggering by using .performAccessibilityAction(ACTION_FOCUS, Bundle()). Hopefully this solves your problem. Your example would look like:
#RunWith(RobolectricTestRunner::class)
class AuthLogDetailActivityUITest {
#Test
fun should_show_error_when_no_text_input_when_textInput_lost_focus() {
val logDetailsActivity = Robolectric.setupActivity(AuthLogDetailsActivity::class.java)
val quickNameTextEdit = logDetailsActivity.findViewById<TextInputEditText>(R.id.loginDetails_textEdit_quickName)
val passwordTextEdit = logDetailsActivity.findViewById<TextInputEditText>(R.id.loginDetails_textEdit_password)
quickNameTextEdit.performAccessibilityAction(ACTION_FOCUS, Bundle())
passwordTextEdit.performAccessibilityAction(ACTION_FOCUS, Bundle())
assertNotNull(quickNameTextEdit.error)
}
}

i can't make a contact between the activities in Kotlin

Hello this is my first app with kotlin i am trying to make annual rate calculation app the problem is i have 4 activities every activity own button and edit's texts
i wan't when The User click the button, the program get the numbers from Edit's texts and only make the calculation and save it somewhere and same work for the activity 2 and 3.
but when he click the last button of the last activity i want to call all the results and show it in ViewText
The Question is:How to save data Every time somewhere and call when i need it?
First Activity
class st {
var int_P: Double? = null
var ctl_P: Double? = null
public constructor(int_P: Any, ctl_P: Any) {
this.int_P = int_P.toString().toDouble() //Physique
this.ctl_P = ctl_P.toString().toDouble()
public fun GetMP(): Double {
return (this.int_P!! + (this.ctl_P!! * 2)) / 3
}
}
Btn_Next1.setOnClickListener ({
var int_P = java.lang.Double.parseDouble(edit_IP.text.toString()) //Physique
var ctl_P = java.lang.Double.parseDouble(edit_CP.text.toString())
var ss = st(int_P,ctl_P)
val ic = Intent(this, Sec_Act::class.java)
startActivity(ic)
})
(Secend and Third Activity Same)
Activity 4
btn1.setOnClickListener ({
var act1 = MainActivity.st().GetMC()
Textv.text = act1.toString()
})
With this method i got problem (no value passed for parameter int_P , ctl_P)
There are many different ways to send information back to an Activity:
onActivityResult(),
having a singleton class,
use Shared Preferences,
headless fragments,
sqlite database,
store the information in a file.
Intents
receivers
You need to determine which will be the best solution for you. Whether it's kotlin or java, the methodology will be the same.

Categories

Resources