Android Kotlin cannot change a text string within a function

how to set text to textview in android dynamically
textview android example
kotlin textview
kotlin string to enum
gettext method in android
android studio textview set text
how to add text to textview in android
kotlin enum extension function

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>

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"
}


TextView, Kotlin |Java setText(R.string.user_greeting); android:drawableBottom, The drawable to be drawn below the text. android:editable, If set, specifies that this TextView has an input method. Autofill type for views that cannot be autofilled. Change "hint" locales associated with the text view, which will be reported to an� In game_fragment.xml, update the text attribute of the word_text text view to use the quote_format string resource. Pass in gameViewModel.word . This passes the current word as an argument to the formatting string.

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"
        }
    }
}

Inline Functions and Reified Type Parameters, The functions shown below are good examples of this situation. is forbidden inside a lambda, because a lambda cannot make the enclosing function return:. A String is a piece of text made up of characters surrounded by quotes. You can think of it as beads lined up on a string to form a necklace, like, characters lined up to form words and text. Specifying that the argument has to be a String helps the system enforce that your argument is text, and not, for example, a number. Run your code.

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"



}

}

Enum Classes, Kotlin for Android FAQ. FAQ � Comparison to Java � Full Kotlin Reference � Edit Page Enum entries cannot contain nested types other than inner classes valueOf(value: String): EnumClass EnumClass.values(): Array<EnumClass>. The valueOf() method throws an IllegalArgumentException if the specified name does � Returns a copy of this string having its first letter titlecased preferring Char.toTitleCase (if different from Char.toUpperCase) or by String.toUpperCase using the specified locale, or the original string, if it's empty or already starts with an upper case letter.

remove this import

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

Hope this will work

Asynchronous Flow, Suspending functions asynchronously returns a single value, but how can we return multiple We can replace delay with Thread.sleep in the body of simple 's flow { . For example, using transform we can emit a string before performing a the call to requestFlow itself is fast, not-suspending, and cannot be cancelled. Create and run a Kotlin program that uses variables and functions, and prints a result to the console. Format numbers within text using a string template with the ${variable} notation. What you'll learn. How to programmatically generate random numbers to simulate dice rolls. How to structure your code by creating a Dice class with a variable

toString, Returns a string representation of this Byte value in the specified radix. Exceptions. IllegalArgumentException - when radix is not a valid radix for number to� A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String XML resource that provides a single string. String Array XML resource that provides an array of strings. Quantity Strings (Plurals)

Working with the TextView, This doesn't");. And you should be all set. Storing Long HTML Strings. If you want to store your HTML text within res/� In Python, function names must be unique within a module or a class. In Kotlin, we can overload functions: there can be multiple declarations of functions that have the same name. Overloaded functions must be distinguishable from each other through their parameter lists.

Kotlin Android TextView and EditText, Kotlin Android TextView and EditText with introduction, architecture, class, interface, generics, delegation, functions, mixing java and kotlin, java vs kotlin etc . setText("string"). While using edit text in XML layout, we must specify its inputType attribute makeText(this,"please input data, edit text cannot be blank", Toast. Run your app. Above the star image, you see an edit text with default text "Name". In this task, you style your EditText view by adding a hint, changing the text alignment, changing the style to the NameStyle, and setting the input type.

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.