1

I have some really simple Kotlin code for changing a text string within a function generated from a button press, but it does not work. I have a single button and two text strings, one the button press the first text string changes but the text string within the function does not change.

I am sure the problem is with the function call and not passing the right information about the activity, but just cannot work out what is wrong.

MainActivty.kt

package com.example.sandpit9

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import org.w3c.dom.Text

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imageButton1.setOnClickListener {v: View -> toast(v) }

        imageButton1.setOnClickListener {
            imageButton1.setImageResource(R.drawable.greenbutton)
            textView1.text = "1234"
        }
    }

    public fun toast(v: View) {
        v.textView2.text = "1234"
    }
}

MainActivty.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/imageButton1"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:textSize="34sp"/>
<TextView
        android:text="textvar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textSize="34sp"
        android:layout_marginTop="108dp"
        app:layout_constraintTop_toBottomOf="@+id/imageButton1"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintHorizontal_bias="0.535"/>
<ImageButton
        android:layout_width="174dp"
        android:layout_height="154dp"
        app:srcCompat="@drawable/download"
        android:id="@+id/imageButton1"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.542"
        app:layout_constraintVertical_bias="0.187"/>

</android.support.constraint.ConstraintLayout>

4 Answers 4

2

You're overwriting the click listener. The OnClickListener is a single property - not a list.

imageButton1.setOnClickListener {v: View -> 
  toast(v)
  imageButton1.setImageResource(R.drawable.greenbutton)
  textView1.text = "1234"
}


Sign up to request clarification or add additional context in comments.

Comments

1
imageButton1.setOnClickListener {v: View -> toast(v) }

imageButton1.setOnClickListener {
    imageButton1.setImageResource(R.drawable.greenbutton)
    textView1.text = "1234"
}

Problem: ImageButton have only one OnClickListener to listen the event when there is a click event on it own. You can set the listener by using setOnClickListener. Because in your code, you use setOnClickListener two times, so the second one will override the first one.

Solution: Change your code to

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imageButton1.setOnClickListener {
            imageButton1.setImageResource(R.drawable.greenbutton)
            textView1.text = "1234"
            textView2.text = "1234"
        }
    }
}

Comments

1

Many thanks, how simple is the solution many thanks for all the help, the setOnClickListener is setup only once to trigger the function. This is the finial code that works

package com.example.sandpit9

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import org.w3c.dom.Text

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    imageButton1.setOnClickListener{v: View -> toast(v)}

    }



private fun toast(v: View) {
    imageButton1.setImageResource(R.drawable.greenbutton)
    textView1.text = "1234"
    textView2.text = "1234"



}

}

Comments

-2

remove this import

import kotlinx.android.synthetic.main.activity_main.view.*

Hope this will work

3 Comments

@GiorgosNeokleous Removing the import will not fix the issue. But it is still worth noting that importing anything with .* is not suggested.
@theThapa Thanks for pointing out. I know that's not recommended. Personally I am not a big fan of kotlin synthetics. However, if that was the answer, there should be a reason explaining why.
@GiorgosNeokleous I agree with you. If removing that import is the answer, it would be good to get some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.