How to get a ProgressBar (or a button...) into class in android through koltin?

loading progress bar in android
custom horizontal progress bar with percentage in android
android circular progress bar indeterminate
android progress bar
kotlin progress bar programmatically
custom circular progress bar in android
determinate circular progress bar android
circular progress bar in kotlin

I need to know how to call a ProgressBar (or any other kind of button) into a class, outside of "onCreate" method. I saw in Java I can do it calling findViewById(), however it's been rid of in Kotlin. For example I was trying a thing like this:

 var myProgressBar = findViewById(R.id.myProgressBar) as ProgressBar

But it gives me the unresolved reference for the find view method.

Thanks in advance for help

______________UPDATE____________________

Here's my DownloadManager custom class

class DownloadSoundController(context: Context): Activity() {

val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
.....
.....
      Thread(Runnable{
        while (starter) {
            var query = DownloadManager.Query()
            query.setFilterById(refid)
            var c = downloadManager.query(query)

            if (c.moveToFirst()) {
                var totalSizeIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                var downloadedIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                val fileSize = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
                var totalSize = c.getInt(totalSizeIndex)
                var downloaded = c.getInt(downloadedIndex)
                var progress = 0.0
                var raffronto: Long = -1

                if (fileSize != raffronto) {
                    progress = downloaded * 100.0 / totalSize
                    //println("myTag - downloaded % :$progress, downloaded var: $downloaded")
                    myProgressBar.progress = progress.toInt()
                    if (progress == 100.0) {
                        starter = false
                    }
                } else {
                    //println("myTag - downloaded = 100 progress: $progress, $downloaded")
                    myProgressBar.progress = progress.toInt()
                    if (progress == 100.0) {
                        starter = false
                    }
                }
            }
        }
    }).start()

Here's my manifest:

<application
        android:hardwareAccelerated="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    <activity
            android:name="com.journeyapps.barcodescanner.CaptureActivity"
            android:screenOrientation="fullSensor"
            tools:replace="screenOrientation" />

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
//ATTENTION
   <activity android:name="com.example.myapplication.DownloadSoundController"/> 
// here it returns ERROR, on default constructor

    <receiver android:name=".DownloadBroadcastManager">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
        </intent-filter>
    </receiver>

</application>

_________UPDATE_2__________

I polished the code and I found several issues that sound really "weird" to me:

1) to stop the nullpoint exception about myProgressBar.progress = value, I found out I have to initialize myProgressBar.progress = 0

2) With println() my thread stamps into the log exactly, step by step, the downloaded amount of my file, but if I add the myProgressBar.progress = progress.toInt() (progress var is THE SAME I stamp into the println()!) in the Thread... it starts to be async in a way it EVEN STARTS BEFORE the download has begun.. leading to several errors and multiple downloads"

I really don't know what's going on

findViewById now returns a generic type which extends View (docs).

final <T extends View> T findViewById(...)

You can use

val progressBar = findViewById<ProgressBar>(R.id.myProgressBar)

Android ProgressBar using Kotlin, Kotlin. ProgressBar UI element is used to display the Progress on the app screen. In this tutorial, we'll discuss and implement ProgressBar in our Android Application using Kotlin. Let's look at the MainActivity.kt Kotlin class code. The horizontalDeterminate Kotlin function is triggered when the first button is clicked. By default ProgressBar is in Indeterminate mode. Determinate ProgressBar is used when you can estimate or track the progress of a task. To make ProgressBar determinate, add progress property, android: progress = "0" , in the layout file for ProgressBar View. In this tutorial, we shall see an example for Determinate ProgressBar. In our next

Firstly, it works the same way in Kotlin.

But there's a better way in Kotlin:

Add the following line to your apps build.gradle: apply plugin: 'kotlin-android-extensions'

Now, you can access the View by its id:

e.g. myProgressBar.progress = 30

More: https://kotlinlang.org/docs/tutorials/android-plugin.html

Dynamic ProgressBar in Kotlin, Android ProgressBar is user interface control that is used to show some kind of Here, we will use the RelativeLayout to get the ProgressBar from the Kotlin file. then add the widget in layout using this Button. import android.widget. LinearLayout. import android.widget. class MainActivity : AppCompatActivity() {. We have also defined a button that will show / hide progressBar. The text of the button will change according to the visibility status of the progressBar. For example, if the progressbar is visible, the text of the button will be “HIDE PROGRESSBAR”. 4. Create Android ProgressBar Programmatically / Dynamically

add the following line to build.gradle: apply plugin: 'kotlin-android-extensions

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

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

        // Instead of findViewById<TextView>(R.id.textView)
        textView.setText("Hello, world!")
    }
}

This can be done outside of onCreate as well.

ProgressBar, Classes. AccessibilityButtonController � AccessibilityButtonController. Kotlin | Java bar in your app's user interface or in a notification instead of within a dialog. The following example shows an indeterminate progress bar: By default, the progress bar is full when the progress value reaches 100. A Button is an user interface that are used to perform some action when clicked or tapped. So let us learn how to create a button in android studio using Kotlin. First we create a new project by following the below steps:

Android - Loading Spinner, You can show progress of a task in android through loading progress bar. defining it in xml, you have to get its reference in java file through ProgressBar class. a basic application that allows you to turn on the spinner on clicking the button. Android Button can be defined as below – Android Button is an user interface that are used to perform some action when clicked or tapped. Different attributes of Android Button Widget. Different attributes of Android Button Widget are listed below. However, If you want to know more attributes you can visit android official documentation on

Android Studio : How to Add and Use a Progress Bar, Adding a Progress Bar in Android Studio Source Code: https://github.com/ Salyder Duration: 6:17 Posted: Mar 16, 2017 Kotlin setOnClickListener for Button Android button is a UI element generally used to receive user actions as input. In this tutorial, we shall learn to set OnClickListener for button. A quick look into code What we have done here is, we got the reference to the Button and then used setOnClickListener method to trigger an action when the button is clicked. Now we shall look into the layout xml

Progress Button in Android Studio, And you have may alerady seen a loading/progress button where, how to create a custom Duration: 11:17 Posted: Jan 26, 2020 How to create and use horizontal progress bar dynamically on android using MainActivity.java programming coding file. In this tutorial we are creating progress bar at application run time using MainActivity.java coding file, So there are no need to define progress bar manually on activity layout file.

Comments
  • I don't think Kotlin doesn't have access to findViewById. Maybe you're doing something wrong.
  • I updated again... it's really "weird"
  • It seems it keeps to gimme a mark as "unresolved". I tried and I can call the progressBar in every function in main activity, however I need it in another class.kt file
  • @MicheleRava are you sure you're using the correct SDK version?
  • I added the :Activity() constructor. Now it can see the myProgressBar, as it happens in MainActivity.kt... However it causes a crash with this error now: Process: com.example.myapplication, PID: 20468 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
  • @MicheleRava the Activity is fully constructed only after calling setContentView inside onCreate. You cannot use findViewById prior to that
  • My DownloadManager custom class execute later... in theory. It seems that, instead, when i activate the download the Activity has not been fully constructed... how can I do to make it right?
  • Yes, it works in all functions inside my MainActivity.kt... However it doesn't work in my "CustomClass.kt" file
  • does CustomClass extend Activity? does it say class CustomClass : Activity()? It must extend Activity for that to work.
  • Actually it doesn't. I created a custom class to handle the DownloadManager. Inside it I created a thread to keep track of the download % and now my goal is to send those values to my progressBar
  • I just added a contructor with :Activity() (as you suggested) and now it works
  • Ok, it seems my app crashes now... I think there must be maybe different problems inside Thanks a lot anyway