• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android Button setOnClickListener call function to change text

I have an app with multiple buttons and text lines, I want to change the text depending on the language set in a menu. Ideally I want all the changes within a function to make the code easier, but the code does not work. I am not sure even if the function is been called, how do I change the text within the function when trigged by the button?

MainActivity.kt

Code:
package com.example.sandpit9

import android.content.Intent
import android.support.v7.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)

        imageButton1.setOnClickListener{toast(R.)}

        imageButton1.setOnClickListener{
            imageButton1.setImageResource(R.drawable.greenbutton)
            textView1.setText("test 1")

        }

    }

    private fun toast() {

        val text: Any = textView2
        textView2.setText("test 2")


    }

}

MainActivity.xml

Code:
<?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"/>
    <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"/>
    <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"/>
</android.support.constraint.ConstraintLayout>
 
Last edited:
Please use [code][/code] tags to nicely format your code.
 
Back
Top Bottom