Android studio >> Buttons do not work properly - android-studio

Second and third buttons work only if I click the first button first. If I click on the second or third button before trying first button then the buttons do not work.
Can you please help me to find out my mistakes? IDE: Android Studio, Language: Kotlin.
Codes are here:
package com.example.habiganjkotlin
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
info_button.setOnClickListener {
val intent = Intent(this, habiganjinfo::class.java)
// start your next activity
startActivity(intent)
news_button.setOnClickListener {
val intent = Intent(this, habiganjnews::class.java)
// start your next activity
startActivity(intent)
corona_button.setOnClickListener {
val intent = Intent(this, corona::class.java)
// start your next activity
startActivity(intent)
} } } } }

The problem of your code is that you have put button 2 and button 3 in button1.setOnClickListener. If these three buttons are independent and you can choose any of them, put them in three seperate setOnClickListener. I mean:
Button1.setOnClickListener{...}
Button2.setOnClickListener{...}
But you have written:
Button1.setOnClickListener{...
Button2.setOnClickListener{...}}
And this is wrong.

Related

Program crashes whenever it gets string data from an another activity

I was just making a simple program that sends string that's typed in an EditText into a TextView in another activity, I'm a beginner and was following a kotlin tutorial on the internet, the program would crash whenever it gets the string from the 1st activity, The code of the 1st activity looks like this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener {
val message: String? = findViewById<EditText>(R.id.sameer).text.toString()
val kash = Intent(this, kos::class.java)
intent.putExtra("sameer", message)
startActivity(kash)
}
}
}
and the code for the 2nd activity goes like this:
class kos: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.kos)
val kanye: Bundle? = intent.extras
val shaheer = kanye!!.getString("sameer")
Toast.makeText(this, shaheer , Toast.LENGTH_SHORT)
findViewById<TextView>(R.id.UserisHambola).text = shaheer
}
}
I've tried to check when does the error start to happen, it appears that it starts to happen after declaring variable "shaheer" in the 2nd activity, if I delete that code with all the other codes after it, the program doesn't crash, I don't know why does this crash happen, Thank you for your time <3
Try to replace kanye!!.getString("sameer") with kanye?.getStringExtra("sameer") ?: "no data". This way the program will not crash if no value is passed under "sameer" key and the default "no data" will be stored to val shaheer. If you see no data in the toast, you passed the variable between activities incorrectly.
You do this in MainActivity:
val kash = Intent(this, kos::class.java)
intent.putExtra("sameer", message)
startActivity(kash)
You put the extra with the key "sameer" into the Intent referenced by the variable intent. You probably want to put the extra into the Intent referenced by the variable kash. Try this instead:
val kash = Intent(this, kos::class.java)
kash.putExtra("sameer", message)
startActivity(kash)

Create single page with text variable in Android Studio (Kotlin)

Is it possible to create a single page in Android Studio (using Kotlin) that displays different text depending on the button you hit on the main screen?
Imagine loading the app, on the home screen there are five buttons, each button you click, displays different text. Instead of creating five pages for the five buttons, can I create the one page and do something with the code to show certain text based on which button I hit?
If it is, how would I go about this and code it?
Update 1
This is what I have done, two buttons to navigate to another activity.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<Button>(R.id.btn)
// set on-click listener
btn.setOnClickListener {
val intent = Intent(this, Duas::class.java)
startActivity(intent)
btn1.setOnClickListener {
textView.text = "newText"
}
val btn2 = findViewById<Button>(R.id.btn2)
// set on-click listener
btn2.setOnClickListener {
val intent = Intent(this, Duas::class.java)
startActivity(intent)
btn2.setOnClickListener {
textView.text = "newText"
}
}
}
}
yes, you can do that, you make your XML layout which contains the textView and the buttons,
now in your activity class, you need to assign an onClickListener on each button
now inside each one your gonna change the text to whatever you want like so
Button1.setOnClickListener {
textView.text = "newText"
}
, so whenever you hit the button the text will change to what is assigned to

How can I use navigationRailView together with the Navigation library? (Have a standard switching of fragments, display of all icons)

Rail menu with nav_graph added. All the icons are switched, but the fragments do not change.
P.S I know about the option with Selected, but I want everything to work cleanly and with the Navigation library
My code is now:
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(
R.id.nav_host_container
) as NavHostFragment
navController = navHostFragment.navController
// Setup navigationRailView
val navigationRailView = findViewById<NavigationRailView>(R.id.navigation_rail)
navigationRailView.setupWithNavController(navController)
}
}
Android Studio offers:
// private fun NavigationRailView.setupWithNavController(navController: NavController) {}

While loop blocks UI and wont exit via button press in Android Studio

A while loop is blocking the UI of my app from showing so I have no chance of pushing the cancel button to break out of it. Also if I am able to get the UI to show by calling the setup function from a TextToSpeech OnInitListener block, I still cant register the button press and exit out.
Why is this happening and how can I get normal, non blocking while loop behavior that I can exit out of with a button click from my cancel button in the UI?
This onCreate does not show the UI at all
class MainActivity : AppCompatActivity() {
lateinit var myTestClass: TestClass
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupTheTestClass()
}
This onCreate shows the UI as I call the setupTheTestClass() within the TextToSpeech OnInitListener but won't register the button press to let me exit the while loop
class MainActivity : AppCompatActivity() {
lateinit var myTestClass: TestClass
lateinit var mTTS:TextToSpeech
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mTTS = TextToSpeech(applicationContext, TextToSpeech.OnInitListener { status ->
setupTheTestClass()
})
}
fun setupTheTestClass()
{
myTestClass = TestClass(cancelBtn)
myTestClass.setupOnClickForButtons()
myTestClass.testWhileLoop() // blocks UI in first example
}
class TestClass(val cancelBtn: Button) {
var running: Boolean = true
fun setupOnClickForButtons()
{
cancelBtn.setOnClickListener {
running = false
println("no longer running")
}
}
fun testWhileLoop() {
while (running == true) {
println("running")
}
}
}
Putting the testWhileLoop() function in a coroutine seems to work
fun setupTheTestClass()
{
myTestClass = TestClass(cancelBtn)
myTestClass.setupOnClickForButtons()
// myTestClass.testWhileLoop()
GlobalScope.launch{
myTestClass.testWhileLoop()
}
}

OnClickerListener issue, can open the app in emulator

I'm new to the Android studio and trying to make first steps. I just wanted to do some action while clicking button. When I ran the emulator it told me that app stopped running and every time I clicked it's icon it did the same. Then I removed OnClickerListener Function and then app started perfectly. Even if the body of this Function is empty it does not run. So what should I do?
package com.example.firstapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity;
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
bigButt.setOnClickListener {
}
}
}
So this code can't run the app. Even if the body is empty. Any suggestions are welcome.

Resources