This article is about fixing lateinit property initialState has not been initialized in leandroBorgesFerreira Loading Button Android
  • 30-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing lateinit property initialState has not been initialized in leandroBorgesFerreira Loading Button Android

lateinit property initialState has not been initialized in leandroBorgesFerreira Loading Button Android

Lightrun Team
Lightrun Team
30-Jan-2023

Explanation of the problem

The user encountered a problem with the CircularProgressButton library in Android. The user attempted to call the startAnimation() method in the onCreate() method, but received the following error: “lateinit property initialState has not been initialized.”

The code block below demonstrates the user’s implementation in the MainActivity class:

class MainActivity : AppCompatActivity() {

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

        button.startAnimation()
    }
}

The user also provided the associated XML layout file, which shows that the CircularProgressButton is being used in the layout.

<?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"
	tools:context=".MainActivity">

	<br.com.simplepass.loadingbutton.customViews.CircularProgressButton
		android:id="@+id/button"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:text="Hello World!"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintLeft_toLeftOf="parent"
		app:layout_constraintRight_toRightOf="parent"
		app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The user noted that the error was not encountered when the startAnimation() method was called in an onClickListener, implying that the issue is related to timing or initialization.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for lateinit property initialState has not been initialized in leandroBorgesFerreira Loading Button Android

The problem with CircularProgressButton is that when the startAnimation() method is called within the onCreate() method, the lateinit property initialState is not initialized. This results in an exception being thrown. To resolve this issue, the recommended solution is to use version 2.1.3 of the library, which has been fixed to address this problem.

If, for any reason, you are unable to upgrade to version 2.1.3, a workaround has been proposed. The solution involves manually initializing the initialState field before calling the startAnimation() method. This is achieved by calling the saveInitialState() method, which is demonstrated in the code block below:

class MainActivity : AppCompatActivity() {

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

        // This will manually initialize the 'initialState' field
        button.saveInitialState()

        button.startAnimation()
    }
}

In this code block, the saveInitialState() method is called before the startAnimation() method, ensuring that the initialState field is initialized before the animation is started. This solves the issue and allows the animation to be started without any exceptions being thrown.

Other popular problems with Loading Button Android

Problem: Incompatible version of AndroidX issue

Another common issue faced while using Loading Button Android is compatibility with different versions of AndroidX. This can result in the absence of required libraries or compatibility errors while building the project.

Solution:

To resolve this issue, it is recommended to use the latest version of Loading Button Android, which has been tested and verified to work with the latest version of AndroidX.

dependencies {
    implementation 'br.com.simplepass:loading-button-android:2.1.3'
}

Problem: Customization of button behavior issue

Loading Button Android provides a default behavior for the button, but there may be instances where the default behavior does not meet the requirement. In such cases, customizing the button behavior can be a challenge.

Solution:

To resolve this issue, Loading Button Android provides a set of methods that can be overridden to implement custom behavior. Additionally, Loading Button Android provides a set of attributes that can be set in the XML file to customize the appearance of the button.

<br.com.simplepass.loadingbutton.customViews.CircularProgressButton
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Problem: Circular animation may not always render correctly when the button is resized

This can happen because the CircularProgressButton view uses a custom drawable to display the animation, which may not automatically adjust its size and position as the button’s dimensions change. This can result in a misaligned or incomplete animation that does not cover the entire button.

Solution:

To resolve this issue, it is recommended to manually re-position and re-size the custom drawable whenever the button is resized. This can be done by listening for size changes on the button, and then calling an appropriate method to update the custom drawable. For example:

button.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
    override fun onGlobalLayout() {
        button.viewTreeObserver.removeOnGlobalLayoutListener(this)
        button.updateProgressBackgroundSize()
    }
})

Here, we are using the viewTreeObserver.addOnGlobalLayoutListener method to listen for changes to the button’s layout, and then calling updateProgressBackgroundSize when the layout has been updated. This will ensure that the custom drawable is re-sized and re-positioned to fit the updated button dimensions.

A brief introduction to Loading Button Android

Loading Button Android is a custom view library for Android platform that enables developers to add circular progress animation to buttons. The library is designed to provide an attractive and customizable animation for the buttons in an Android app. The loading button library is built with Kotlin and AndroidX, making it suitable for modern Android development.

The library offers several customization options to suit the needs of different Android apps. Developers can control the start and end angle of the animation, the size of the loading circle, the color of the loading circle, the color of the text inside the button, and the text size. Additionally, developers can add custom animations to the button and control the speed of the animation. The library also provides support for saving the initial state of the button and restoring it later, which can be useful in cases where the state of the button changes frequently.

Most popular use cases for Loading Button Android

  1. Customizing UI Components: Loading Button Android allows developers to customize various aspects of the UI components, such as the button shape, color, size, and text. This can be done by accessing various properties of the Loading Button class, such as the buttonWidth, buttonHeight, and buttonText properties.
val loadingButton = LoadingButton(this)
loadingButton.buttonWidth = 200
loadingButton.buttonHeight = 50
loadingButton.buttonText = "Submit"
  1. Implementing Loading States: Loading Button Android provides support for implementing loading states within the button. This can be done by setting the loading state of the button, which will change its appearance to indicate that the button is currently in the process of performing a task. The loading state can be set using the setLoading(isLoading: Boolean) method.
loadingButton.setLoading(true)
// Perform background task
loadingButton.setLoading(false)
  1. Event Handling: Loading Button Android allows developers to handle events, such as button clicks, by implementing a listener interface. This allows developers to perform specific actions when the button is clicked or when the loading state of the button changes. The listener interface can be set using the setOnClickListener method.
loadingButton.setOnClickListener {
    // Perform action on button click
}
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.