Home > front end >  Display the text of a button when clicked in Kotlin
Display the text of a button when clicked in Kotlin

Time:07-27

I have a table made of buttons and each button has a text on it. However, I want that text to be invisible unless the button is clicked. The buttons look like this:

<Button
            android:background="@drawable/roundstyle"
            android:backgroundTint="@color/c1"
            android:id="@ id/btnOne"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:text="@string/i"
            tools:ignore="UsingOnClickInXml"
            android:onClick="displayText" />

In MainActivity, I tried to implement a function to display the text:

    fun displayText(view: View) {
        val b = view as Button
        val buttonText = b.text.toString()

I think the easiest thing to do would be to use "if", so that if btnOne is not clicked, then the text should be invisible, else, the text should be visible. But I am not sure where to write this code in the Main Activity and how exactly, so that once the button has been clicked, the text remains on the screen. Could someone help me with this, please?

Thank you.

CodePudding user response:

can try this one by default set the button like this(the most easy solution)

<Button
            android:background="@drawable/roundstyle"
            android:backgroundTint="@color/c1"
            android:id="@ id/btnOne"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:tag = "some text"
            tools:ignore="UsingOnClickInXml"
            android:onClick="displayText" />

and click handler

fun displayText(view: View) {
       val b = view as Button
       val btnTag= b.tag.toString()
       if (b.text.isEmpty()) {
         b.text = btnTag
       }
}

CodePudding user response:

You can do this simply by changing the font size of the button text. you can initially set the font size to 0 and then set the right value after user clicked on it.

 <Button
        android:onClick="displayText"
        android:text="Sample Text"
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:textSize="0sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

To visible the text once

fun displayText(view: View) {       
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP,12f)
    }

This function will toggle the visibility of the text

fun displayText(view: View) {
        if(view.textSize == 0f){
            view.setTextSize(TypedValue.COMPLEX_UNIT_SP,12f)
        }else{
            view.setTextSize(TypedValue.COMPLEX_UNIT_SP,0f)
        }
    }
  • Related