androidkotlinmethodsprogressdialog

How to change the text of a progress bar


// Guys, I'm a beginner android developer and I am trying to change the dialogue text from "Please Wait" to other string text, below is my XML caption

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="horizontal"
    android:padding="23dp">


    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="64dp"
        android:layout_height="64dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tvProgressBar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.hardextech.caramel.utils.MyCustomFont
        android:id="@+id/tvProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Wait......"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@+id/progressBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/progressBar"
        app:layout_constraintTop_toTopOf="@+id/progressBar" />

</androidx.constraintlayout.widget.ConstraintLayout>

// Below is my code in Kotlin

package com.hardextech.caramel.activities

import android.app.Dialog
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
import com.hardextech.caramel.R



open class BaseActivity : AppCompatActivity() {

    private lateinit var progressBar: Dialog

// Method to show the progressBar
        fun showProgressDialogue(text:String){
        progressBar = Dialog(this)
        progressBar.setContentView(R.layout.progress_dialogue)
        progressBar.tvProgressBar.text = text
        progressBar.setCancelable(false)
        progressBar.setCanceledOnTouchOutside(false)
        progressBar.show()

    }

    fun dismissProgressDialogue(){
        progressBar.dismiss()

    }
}

// I expect the line of code progressBar.tvProgressBar.text = text to change dialogue text to the constructor text: String but not implementing. I don't know how to go about it.


Solution

  • To be able to change the text you have to put the layout in a view object so that you can have a reference to the TextView before setting the view object as the context of the Progress Dialog.

        fun showProgressDialogue(text:String){
            progressBar = Dialog(this)
            val inflater = (context as Activity).layoutInflater 
            val view = inflater.inflate(R.layout.progress_dialogue, null)
            view.findViewById(R.id.tvProgressBar).text = text
            progressBar.setContentView(view)
            progressBar.setCancelable(false)
            progressBar.setCanceledOnTouchOutside(false)
            progressBar.show()
    
        }