im trying to code a really simple app, it's my first one for android.
In the console i don't get any error messages but when i try to run the app on android emulator i can only press the first button and get to the activity i want, second doenst work. Maybe you can help me.
Thats my code:
val button = findViewById<Button>(Btn)
button.setOnClickListener {
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
val button = findViewById<Button>(Btn2)
button.setOnClickListener {
val intent = Intent(this, MainActivity3::class.java)
startActivity(intent)
}
}
}
}
What am i missing? Thank you in adavance!
After startActivity(intent) you need a "}"
If you write it this way, you can only press the BTN button first before the second btn2 button is set and can be clicked later, so you should write the btn2 code logic outside, like this
val button = findViewById<Button>(Btn)
button.setOnClickListener {
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
val button = findViewById<Button>(Btn2)
button.setOnClickListener {
val intent = Intent(this, MainActivity3::class.java)
startActivity(intent)
}
I see that you've got the answer from community, I'll join))
Move the second button's logic outta first one.
val button = findViewById<Button>(Btn1)
button.setOnClickListener {
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
val button = findViewById<Button>(Btn2)
button.setOnClickListener {
val intent = Intent(this, MainActivity3::class.java)
startActivity(intent)
}
Hope, findViewById method soon will be deprecated.
val button = findViewById<Button>(Btn)
button.setOnClickListener {
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
val button = findViewById<Button>(Btn2)
button.setOnClickListener {
val intent = Intent(this, MainActivity3::class.java)
startActivity(intent)
}
You are defining the second button and setting the Listener to it inside the listener of the first button
val button = findViewById<Button>(Btn)
button.setOnClickListener {
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
val button2 = findViewById<Button>(Btn2)
button2.setOnClickListener {
val intent = Intent(this, MainActivity3::class.java)
startActivity(intent)
}
Related
If the user presses the positive button, they want to move it to MainActivity
fun setPositiveButtonClick(callback: (() -> Unit)?, view: View? = null) {
val intent = Intent(this#ErrorDialog, MainActivity::class.java)
startActivity(intent)
var view = view
if (view == null) view = this.view
this.positiveCallback = callback
view?.let { v ->
v.yes.setOnClickListener {
callback?.let { c ->
c()
}
dismiss()
}
}
}
Here is the error
I'm assuming this is in some sort of Dialog class, so I think you simply need to get the context of it by just replacing
val intent = Intent(this#ErrorDialog, MainActivity::class.java)
to
val intent = Intent(this.context, MainActivity::class.java)
I would like to know is it possible to pass an intent into the button? Because when I used the debug function, it is shown that the currentUser is null. Below are the codes that I tried to use.
R.id.goalsView ->{
intent = Intent(activity, GoalsActivity::class.java
var cUser : String? = activity!!.intent.getStringExtra("currentUser")
intent.putExtra("Current User", cUser)
startActivity(intent)
}
binding.btnView.setOnClickListener{
val intent = Intent(this, ViewGoalActivity::class.java)
var currentUser : String? = intent.getStringExtra("Current User")
intent.putExtra("Current User", currentUser)
startActivity(intent)
}
Please understand my poor English :)
Can I use it as follows?
before
if (binding.heightEditText.text.isNotBlank() && binding.weightEditText.text.isNotBlank()) {
val intent = Intent(this, ResultActivity::class.java).apply {
putExtra("weight", binding.weightEditText.text.toString().toFloat())
putExtra("height", binding.heightEditText.text.toString().toFloat())
}
startActivity(intent)
}
after
val height = binding.heightEditText.text
val weight = binding.weightEditText.text
if (height.isNotBlank() && weight.isNotBlank()) {
val intent = Intent(this, ResultActivity::class.java).apply {
putExtra("weight", weight.toString().toFloat())
putExtra("height", height.toString().toFloat())
}
startActivity(intent)
}
Yes, you can use this way to define those variables.
Just make sure your variables are in the same method that you are calling to retrieve the updated values into defined variables.
I'm updating minor things in your code.
val height = binding.heightEditText.text.toString()
val weight = binding.weightEditText.text.toString()
if (height.isNotBlank() && weight.isNotBlank()) {
val intent = Intent(this, ResultActivity::class.java).apply {
putExtra("weight", weight.toFloat())
putExtra("height", height.toFloat())
}
startActivity(intent)
}
I hope it's helpful to you!
Weird output
I'm getting this output on my alert dialog when I enter text.
Can someone maybe explain why this is happening.
No errors in logcat.
Here is my code for the Alert Dialog
private fun declineDialogBox() {
val dialogBuilder = AlertDialog.Builder(this)
val inflater = layoutInflater
val dialogLayout = inflater.inflate(R.layout.alert_dialog, null)
val toBeCorrected = dialogLayout.findViewById<EditText>(R.id.editText)
dialogBuilder.setMessage("Reason is?")
.setCancelable(false)
.setPositiveButton("Proceed") { dialog, id ->
finish()
Toast.makeText(applicationContext, "Reason is $toBeCorrected", Toast.LENGTH_SHORT)
.show()
}
.setNegativeButton("Cancel") { dialog, id ->
}
val alert = dialogBuilder.create()
alert.setView(dialogLayout)
alert.setTitle("To Be Corrected")
alert.show()
}
You are printing the instance of EditText instead the value, add .text to get the value from EditText
Toast.makeText(applicationContext, "Reason is ${toBeCorrected.text}", Toast.LENGTH_SHORT).show()
My current project downloading data from a server time I want to display a progress bar so the user knows what is going on. I have a simple alert dialog , l am try to add alert dialog when data json loading for user . when l launching app the alert dialog stay stick on screen even if the data loaded already and doesn't hide .
AsyncTask code :
inner class Arr : AsyncTask<String, String, String>(){
val progressDialog = AlertDialog.Builder(this#MainActivity)
val dialogView = layoutInflater.inflate(R.layout.progress_dialog,null)
val message = dialogView.findViewById<TextView>(R.id.message_id)
val dialog = progressDialog.create()
override fun onPreExecute() {
super.onPreExecute()
progressDialog.setMessage("loading")
progressDialog.setCancelable(false)
progressDialog.show()
}
// for build connection
override fun doInBackground(vararg url: String?): String{
var text : String
val connection = URL(url[0]).openConnection() as HttpURLConnection
try {
connection.connect()
text = connection.inputStream.use { it.reader().use{reader -> reader.readText()} }
} finally{
connection.disconnect()
}
return text
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
handleJson(result)
dialog.dismiss()
}
override fun onProgressUpdate(vararg text: String?) {
dialog.dismiss()
}
private fun handleJson (jsonString: String?){
dialog.dismiss()
val jsonObj = JSONObject(jsonString)
val result = jsonObj.getJSONObject("result")
val response = result.getJSONObject("response")
val airport = response.getJSONObject("airport")
val pluginData = airport.getJSONObject("pluginData")
val schedule = pluginData.getJSONObject("schedule")
val arrivals = schedule.getJSONObject("arrivals")
// val data = arrivals.getJSONObject("data")
val jsonArray = JSONArray(arrivals.get("data").toString())
val list = ArrayList<FlightShdu>()
var x = 0
while (x < jsonArray.length()){
val jsonObject = jsonArray.getJSONObject(x)
list.add(FlightShdu(
jsonObject.getJSONObject("flight").getJSONObject("identification").getJSONObject("number").getString("default"),
jsonObject.getJSONObject("flight").getJSONObject("airline").getString("name"),
jsonObject.getJSONObject("flight").getJSONObject("status").getString("text"),
jsonObject.getJSONObject("flight").getJSONObject("airline").getJSONObject("code").getString("icao"),
jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("scheduled").getString("arrival")
))
x++
}
list.forEach(::println)
val adapter = ListAdapte(this#MainActivity,list)
flight_arrivel_list.adapter = adapter
}
} //
any solution please ?
Change to this:
override fun onPreExecute() {
super.onPreExecute()
dialog.setMessage("loading")
dialog.setCancelable(false)
dialog.show()
}
and hide it with:
dialog.dismiss();
you must refer to the AlertDialog object and not the AlertDialog.Builder.
In your onPreExecute()
override fun onPreExecute() {
...
progressDialog.show()
}
But in onPostExecute() you call different dialog to dismiss
override fun onPostExecute(result: String?) {
...
dialog.dismiss()
}
So you need to call progressDialog.dismiss() in onPostExecute().
progressDialog.dismiss() instead of dialog.dismiss()