Skip to content Skip to sidebar Skip to footer

How To Pass Attributeset As A Parameter

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 para

Solution 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.

Post a Comment for "How To Pass Attributeset As A Parameter"