Reputation: 1
I am currently using Kotlin. I am trying to change the colour of a box in main activity by going to another activity that is the colour setting activity. My code returns no errors but not working. I tried reading different result on this page but none answering my question. Thank u for your help.
mainactivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button_box = findViewById<Button>(R.id.button_box)
button_box.setOnClickListener {
val intent = Intent(this, boxColor::class.java)
startActivity(intent)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
val returnColor = data!!.getStringExtra("colorName")
val boxColorChoice = when (returnColor) {
"green" -> R.drawable.box_green
"grey" -> R.drawable.box_grey
"lblue" -> R.drawable.box_lblue
"purple" -> R.drawable.box_purple
"red" -> R.drawable.box_red
"white" -> R.drawable.box_white
"yellow" -> R.drawable.box_yellow
else -> R.drawable.box_white
}
button_box.setBackgroundResource(boxColorChoice)
}
}
here is boxcolor.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_box_color)
val green = findViewById<Button>(R.id.green_box)
val red = findViewById<Button>(R.id.red_box)
val white = findViewById<Button>(R.id.white_box)
val yellow = findViewById<Button>(R.id.yellow_box)
val grey = findViewById<Button>(R.id.grey_box)
val lblue = findViewById<Button>(R.id.lblue_box)
green.setOnClickListener {
val intent = Intent()
intent.putExtra("colorName", "green")
setResult(Activity.RESULT_OK, intent)
finish()
}
(and the same onclicklistener for all the other colours)
also I know this is small problem but thank you very much for helping. I am 14 year old boy from rajasthan and I want to be a programmer and learning
Upvotes: 0
Views: 70
Reputation: 312
you need to use startActivityForResult your code must change to these :
mainactivity.kt
...
val mRequestCode = 101 //ADD THIS LINE
...
button_box.setOnClickListener {
val intent = Intent(this, boxColor::class.java)
startActivityForResult(intent, mRequestCode) //ADD THIS LINE
}
...
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == mRequestCode) { //CHANGE THIS LINE
...
}
}
and in your boxcolor.kt:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_box_color)
val mRequestCode = 101 //ADD THIS LINE
...
green.setOnClickListener {
val intent = Intent()
intent.putExtra("colorName", "green")
setResult(mRequestCode, intent) //CHANGE THIS LINE
finish()
}
(and the same onclicklistener for all the other colours)
Note that don't use Activity.RESULT_OK as requestCode
Upvotes: 1
Reputation: 2733
Please use Request Code and startActivityForResult
Example:
private val RC = 101
val intent = Intent(this, boxColor::class.java)
startActivityForResult(intent, RC)
Upvotes: 0