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)
}
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 am new to android studio Kotlin.
I want to make a quiz app with multiple levels. When the level button in the LevelActivity is pressed, the key-value pair is passed to ProblemActivity using intent.putExtra("problem1", 1) and the ProblemActivity shows a problem using intent.hasExtra("problem", 1). If I get the problem right, come back to the LevelActivity with intent.putExtra("Clear1", 101) and the next level button is clickable by intent.hasExtra("Clear1").
I want to make the buttons that were clickable with the previous data remain clickable when I close the app and run it again. I studied SharedPreference, but I am not sure how to save and restore intent data because most of the content is about edit_text. I would appreciate it if you let me know.
Thank you.
LevelActivity.kt
class LevelActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_level)
if (intent.hasExtra("Clear1")) {
easy2.isEnabled = true
easy2.isClickable = true
easy2.setTextColor(Color.parseColor("#000000"))
} else {
easy2.isEnabled = false
easy2.isClickable = false
}
easy1.setOnClickListener {
val intent = Intent(this, ProblemActivity::class.java)
intent.putExtra("problem1", 1)
startActivityForResult(intent, 1)
finish()
}
easy2.setOnClickListener {
val intent = Intent(this, ProblemActivity::class.java)
intent.putExtra("problem2", 2)
startActivityForResult(intent, 2)
finish()
}
}
}
ProblmeActivity.kt
var round = 1
class ProblemActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_problem)
if (intent.hasExtra("problem1")) {
img_problem.setImageResource(R.drawable.slide1)
tv_problem.text = "Plus"
tv_problemlevel.text = "1"
round = 1
}
if (intent.hasExtra("problem2")) {
img_problem.setImageResource(R.drawable.slide2)
tv_problem.text = "Minus"
tv_problemlevel.text = "2"
round = 2
}
tv_enter.setOnClickListener {
checkAnswer()
}
fun checkAnswer() {
if (tv_expression.text.toString() == "1" && round == 1) {
val intent = Intent(this, LevelActivity::class.java)
intent.putExtra("Clear1", "Clear1")
startActivityForResult(intent, 101)
finish()
}
if (tv_expression.text.toString() == "2" && round == 2) {
val intent = Intent(this, LevelActivity::class.java)
intent.putExtra("Clear2", 102)
startActivityForResult(intent, 102)
finish()
}
else{
var wrong_toast = Toast.makeText(this, "Wrong! Try again!", Toast.LENGTH_SHORT)
wrong_toast.show()
}
}
}
Use sharedpreferences and get values from it when the app opens.
I do not think there is another way unless you use sqlite
i want to my button can check if the letter is inside the word for example
word: Hai
user pres btnA
result : _a_
user pres btnH
result : Ha_
this what i do at kotlin Android studio
fun guessTry(click : View){
val guessW = txtKata.toString()
txtKata.text = null
if (click == btnA){
val a = btnA.text
if(a in secretWord.toLowerCase() || a in secretWord.toUpperCase() ){
corecGuess.add(guessW)
wordSecret()
}
}
}
word: Hai
user pres btnA
result : _a_
user pres btnH
result : Ha_
I think you can try to change the if function like this
fun guessTry(click : View){
val guessW = txtKata.toString()
txtKata.text = null
if (click == btnA){
val a = btnA.text
if(secretWord.toLowerCase().contains(a) || secretWord.toUpperCase().contains(a) ){
corecGuess.add(guessW)
wordSecret()
}
}
}
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 :)
}
}