In an android app I want to let the user check a radio button and depending on what the user checked I want it to change things in the next activity (e.g. hiding a button or change the text of a button). How am I doing this?
I already found out how to let it change a textview in the next activity, but probably there is a better way for it too.
val rg1 = findViewById<RadioGroup>(R.id.rg1)
val rb1 = findViewById<RadioButton>(R.id.rb1)
val rb2 = findViewById<RadioButton>(R.id.rb2)
val tv1 = findViewById<TextView>(R.id.tv1)
val btnNext = findViewById<Button>(R.id.btnNext)
rg1.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { _, i ->
when (i) {
R.id.rb1 -> tv1.text = "something"
R.id.rb2 -> tv1.text = "something else"
}
})
btnNext.setOnClickListener {
val result = tv1.text.toString()
val intent = Intent(this, Activity2::class.java)
intent.putExtra("Result", result)
startActivity(intent)
}
tv1.isGone = true
After doing this in Activity2:
val result = intent.getStringExtra("Result")
val tv2 = findViewById<TextView>(R.id.tv2)
tv2.text = result
it changes tv2 in Activity2 (tv1 is only there to get the text string and shouldn't be displayed in first activity, as I said before, there is probably a better solution too).
But most important what to do when I want to hide a button or do something else in the next activity depending on radio buttons?
not sure if this is an answer to your question or not, but consider this:
you can change
rg1.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { _, i ->
when (i) {
R.id.rb1 -> tv1.text = "something"
R.id.rb2 -> tv1.text = "something else"
}
})
to something like this
rg1.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { _, i ->
when (i) {
R.id.rb1 -> OneSetupOfYourUI()
R.id.rb2 -> AnotherSetupOfYourUI()
}
})
then define what you want to change inside functions and call those functions, instead of changing every single component.
fun OneSetupOfYourUI(){
//change stuff in here
}
Maybe this helps ?
Then when you start the new activity :
val result = tv1.text.toString()
val intent = Intent(this, Activity2::class.java)
intent.putExtra("Result", result)
startActivity(intent)
Consider adding several intent.putExtra() statements based off of your configured UI or what the user selected
Edit
Just for anyone interested or unsure, you can also simply do this in a when statement:
when (i) {
R.id.rb1 -> {
tv1.text = "something"
etc.
//now you can do several statements here :)
}
}
Related
I'm kinda new with Kotlin but i'm trying to make my TextView visibility gone or visible based on the live input on Edit Text. Basically, When the user start to input something, i want this text to visible and when they delete until empty, this text is dissapear. I tried to fix it but i still couldn't found any good results. Anyway, thank you.
this is my code:
val Email = findViewById<EditText>(R.id.EmailBox)
val sEmail = Email.text.toString()
val Password = findViewById<EditText>(R.id.PasswordBox)
val sPassword = Password.text.toString()
val emailtext = findViewById<TextView>(R.id.EmailText)
val passwordtext = findViewById<TextView>(R.id.PasswordText)
if(sEmail.isEmpty()) {
emailtext.visibility = View.VISIBLE
} else if (sPassword.isEmpty()) {
passwordtext.visibility = View.VISIBLE
} else {return
}
}
You need to set a listener to check for text changes, rather than getting the current value of the EditText once during setup.
You can do this using the doOnTextChanged method. This would look like:
val email = findViewById<EditText>(R.id.EmailBox)
val emailtext = findViewById<TextView>(R.id.EmailText)
// This will hide "emailtext" when "email" is empty, and show
// it when it is not empty
email.doOnTextChanged { newText, _, _, _ ->
emailtext.visibility = if (newText.isNullOrEmpty()) View.INVISIBLE
else View.VISIBLE
}
I have created my first app in the android studio it is running well and I can randomly choose a picture. My intent is when it choose a picture I'd like it back to the start screen.
I have tried the restart button but I do not know how to write the code.
'''
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val lemonOption: ImageView = findViewById(R.id.imageView)
lemonOption.setOnClickListener{
lemonChosen()
}
}
private fun lemonChosen() {
val lemon = Lemon()
val chosenLemon = lemon.pick()
val lemonOption: ImageView = findViewById(R.id.imageView)
val resultTextView: TextView = findViewById(R.id.textView)
if (chosenLemon != 1) {
if(chosenLemon == 2){
resultTextView.text = "The Chosen lemon is the number 2"
}else {
resultTextView.text = "The Chosen lemon is the number 3"
}
} else {
resultTextView.text = "The Chosen lemon is the number 1"
}
when (chosenLemon) {
1 -> lemonOption.setImageResource(R.drawable.limao_1)
2 -> lemonOption.setImageResource(R.drawable.limao_2)
3 -> lemonOption.setImageResource(R.drawable.limao_3)
}
}
}
/**
* The class lemon call the method to pick a lemon image determined randomized
*/
class Lemon() {
fun pick(): Int{
return (1..3).random()
'''
To restart your app '
Create an intent and start activity. Here flag is set to clear the top which means remove all the any other activities which are running in the app, this will prevent the multiple instances of the same activity as you want to reopen the same activity again.
val intent = Intent(this#MainActivity, MainActivity::class.java)
intent.flag = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
private fun restart() {
val lemonOption: ImageView = findViewById(R.id.imageView)
lemonOption.setImageResource(android.R.color.transparent)
}
I implemented a "Don't show again" option to my Alert Dialog, but it's not working as intended:
All the other questions are 6-7 years old and in java, and since I am completely new to programming I only know the basics of kotlin as of right now..
fun showDialog() {
val dialogBuilder = AlertDialog.Builder(context)
val inflater = requireActivity().layoutInflater;
dialogBuilder.setView(inflater.inflate(R.layout.alert, null))
dialogBuilder.setMessage("Please always put the total on the bottom right corner. An example is shown below.")
dialogBuilder.setPositiveButton("Alright, got it!",
DialogInterface.OnClickListener { dialog, whichButton ->
pb.visibility = View.VISIBLE
checkPermission(Manifest.permission.CAMERA,CAMERA_PERMISSION_CODE)
startActivityForResult(receiptsViewModel.cameraIntent(requireActivity()),REQUEST_CODE_KAMERA)
})
val mainView: View = inflater.inflate(R.layout.alert, null)
checkBox = mainView.findViewById<View>(R.id.checkBox) as CheckBox
val b = dialogBuilder.create()
b.show()
checkBox.setOnCheckedChangeListener { compoundButton, b ->
if (compoundButton.isChecked) {
storeDialogStatus(true)
} else {
storeDialogStatus(false)
}
}
if (dialogStatus) {
b.hide()
} else {
b.show()
}
}
private fun storeDialogStatus(isChecked: Boolean) {
val mSharedPreferences = requireActivity().getSharedPreferences("CheckItem", AppCompatActivity.MODE_PRIVATE)
val mEditor = mSharedPreferences.edit()
mEditor.putBoolean("item", isChecked)
mEditor.apply()
}
private val dialogStatus: Boolean
private get() {
val mSharedPreferences = requireActivity().getSharedPreferences("CheckItem",
AppCompatActivity.MODE_PRIVATE
)
return mSharedPreferences.getBoolean("item", false)
}
The problem I see is that the checkbox that you're adding a listener to is for a layout that you're not using. You inflated a layout and set that as your dialog view. Then you inflate a second copy of the layout and set a listener on the checkbox in that unused second layout.
A few other tips, but these aren't things that are preventing it from working. They'll just make your code clearer:
You can chain Builder calls so you don't have to keep putting dialogBuilder. and you don't even have to store it in a variable.
findViewById<View> can be changed to findViewById<CheckBox> so you don't have to cast its result to CheckBox.
You can return early from the function if the dialog isn't needed rather than creating the dialog, showing it, and then immediately hiding it.
Instead of using if (someBoolean) doSomething(true) else doSomething(false) you can simplify to doSomething(someBoolean).
private fun startCamera() {
pb.visibility = View.VISIBLE
checkPermission(Manifest.permission.CAMERA,CAMERA_PERMISSION_CODE)
startActivityForResult(
receiptsViewModel.cameraIntent(requireActivity()),
REQUEST_CODE_KAMERA
)
}
fun showDialog() {
if (dialogStatus) {
startCamera()
return
}
val mainView = requireActivity().layoutInflater.inflate(R.layout.alert, null)
checkBox = mainView.findViewById<CheckBox>(R.id.checkBox)
checkBox.setOnCheckedChangeListener { compoundButton, b ->
storeDialogStatus(compoundButton.isChecked)
}
AlertDialog.Builder(context)
.setView(mainView)
.setMessage("Please always put the total on the bottom right corner. An example is shown below.")
.setPositiveButton("Alright, got it!") { _, _ -> startCamera() }
.create()
.show()
}
I also find it odd that you have a property for checkBox that couldn't possibly be useful outside this function. You should probably remove the property.
Also, you should look into using DialogFragment instead of a bare Dialog. There are issues with bare Dialogs, such as them disappearing after a screen rotation. The official documentation explains how to use DialogFragment. I will admit, though, that I think DialogFragment is kind of convoluted to use, especially for a new Android programmer.
Your logic seems to be faulty. This piece of code does not work as intended:
if (dialogStatus) {
b.hide()
} else {
b.show()
}
You should not create the dialog at all if the user has opted out. Please try this modified code:
fun showDialog() {
if (dialogStatus) {
return
}
val dialogBuilder = AlertDialog.Builder(context)
val inflater = requireActivity().layoutInflater
dialogBuilder.setView(inflater.inflate(R.layout.alert, null))
.setMessage("Please always put the total on the bottom right corner. An example is shown below.")
.setPositiveButton("Alright, got it!",
DialogInterface.OnClickListener { dialog, whichButton ->
pb.visibility = View.VISIBLE
checkPermission(Manifest.permission.CAMERA, CAMERA_PERMISSION_CODE)
startActivityForResult(
receiptsViewModel.cameraIntent(requireActivity()),
REQUEST_CODE_KAMERA
)
})
dialogBuilder.create().show()
val mainView: View = inflater.inflate(R.layout.alert, null)
checkBox = mainView.findViewById<CheckBox>(R.id.checkBox)
checkBox.setOnCheckedChangeListener { compoundButton, b ->
if (compoundButton.isChecked) {
storeDialogStatus(true)
} else {
storeDialogStatus(false)
}
}
}
I have multiple option select and I need to get array of selected options but all I get is latest option selected.
Code
class PublishActivity : AppCompatActivity() {
var selectedTags: List<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_publish)
pTags.setOnClickListener {
var tagIds = ArrayList<String>()
val tagOptions = ArrayList<String>()
for (i in tags) {
tagOptions.add(i.title)
tagIds.add(i.id)
}
var checkedItems = ArrayList<Int>()
checkedItems.forEach{ index -> tagIds[index + 1] }
MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.project_tags))
.setMultiChoiceItems(tagOptions.toTypedArray(), null) { dialog, which, checked ->
if (checked) {
checkedItems.add(which)
} else if (checkedItems.contains(which)) {
checkedItems.remove(Integer.valueOf(which))
}
// Respond to item chosen
pTags.setText("${checkedItems.size} tags selected")
}
.setPositiveButton(resources.getString(R.string.ok)) { dialog, which ->
for (i in checkedItems) {
Log.d("eeee1", tagOptions[i])
selectedTags = listOf(tagOptions[i])
}
}
.setNeutralButton(resources.getString(R.string.clear)) { dialog, which ->
pTags.text = null
pTags.hint = "0 tag selected"
if (checkedItems.size > 0) {
checkedItems.clear()
}
}
.show()
}
}
}
Log.d("eeee1", tagOptions[i]) returns such data in logcat
D/eeee1: 3D Printing
D/eeee1: 3D Architecture
D/eeee1: .NET/Mono
D/eeee1: ActionScript
but in my selectedTags I get only D/eeer1: [ActionScript]
It supposed to give me something like this D/eeer1: ["3D Printing", "3D Architecture", ".NET/Mono", "ActionScript"]
PS: what I'm actually look to achieve here is to get id of those selected items instead of their names that's why I have var tagIds = ArrayList<String>() but if that's not possible to achieve as long as it just return array of all names (like sample above) it's fine by me as well.
Any idea?
The following code sets your variable to a list with a single item. So you just overwrite your variable over and over again
selectedTags = listOf(tagOptions[i])
you need:
//Declaration
var selectedTags: MutableList<String> = mutableListOf()
...
// In loop
selectedTags.add(tagOptions[i])
You could also do it with a more functional approach:
//Declaration
var selectedTags: List<String>? = listOf()
...
// Skip the loop and use the map function
.setPositiveButton(resources.getString(R.string.ok)) { dialog, which ->
selectedTags = checkedItems.map{ tagOptions[it] }
}
To get the Id's instead of the titles you should just be able to use your tagIds instead of tagOptions. Just make sure that you get your typing right. The selectedTags list needs to be of the same type as tag.id.
You are getting only last inserted value because you are creating fresh list when ok button is clicked and assigning it to selectedTags. Problem at selectedTags = listOf(tagOptions[i]) line of your code.
Solution:
Declare a single list and put selected values into it. Like :
val selectedTags = arrayListOf<String>()
then use below code inside ok button click:
.setPositiveButton("Ok") { dialog, which ->
for (i in checkedItems) {
//selectedTags = listOf(tagOptions[i])
selectedTags.add(tagOptions[i])
}
}
Are Name and Age null if there are no inputs in the EditText field when the App runs? Also, I can't seem to get the conditional syntax down. Any tips? I'm new to Kotlin.
val button = findViewById<Button>(R.id.button4)
button.setOnClickListener {
when{
Name.val==null && Age.val==null && gender.val=="" -> button.isEnabled=false
}
val intent = Intent(this, fourth::class.java)
startActivity(intent)
}