2

I'm trying to implement updated solution by Pedram from this answer, but I don't know to create a new instance of CircleProgressBar. It requires AttributeSet to be passed as a parameter, but how to get it?

CircleProgressBar circleProgressBar = new CircleProgressBar(MainActivity.this, ?);
Community
  • 1
  • 1
user3051755
  • 1,497
  • 2
  • 16
  • 27

1 Answers1

1

The AttributeSet constructor is used when a view is inflated through a layout defined in XML. If you're constructing one in code, you should use the single argument constructor (e.g. new CircleProgressBar(MainActivity.this)). If the single-argument constructor is not defined, you just need to add it. You'll just need to add some getters/setters for the properties if you want to be able to construct it entirely from within Java code.

Alternately, just define a layout XML (example name view_progress_circle.xml) with a single item:

<com.your.packagename.CircleProgressBar
    android:layout_width="100dp"
    android:layout_height="100dp"
    // etc. add other attributes here
    />

Then in code, create it with:

CircleProgressBar bar = (CircleProgressBar) LayoutInflater.from(MainActivity.this)
        .inflate(R.layout.view_progress_circle, parent, false):

where parent is the ViewGroup you're going to attach the view to.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274