Android parsing styles for custom widget not working

I have created custom widget for my android application and i want to create custom styles for it. But while parsing it in the class returns always null. Gone through several links and couldn't figure out what the problem is ? Can anyone help ?

My atttr.xml is

<resources>

    <declare-styleable name="Widget">
        <attr name="headers" format="reference" />
        <attr name="height" format="integer" />
    </declare-styleable>

</resources>

Widget class

public Widget(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray attr = context.obtainStyledAttributes(attrs,
            R.styleable.Widget);
    String[] columns = (String[]) attr
            .getTextArray(R.styleable.Widget_headers);

    int height = attr.getInt(R.styleable.Widget_height, 0);
}    

And the layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:widget="http://schemas.android.com/apk/lib/com.sample.custom"
    android:id="@+id/statistics_fragment_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.sample.custom.Widget
        android:id="@+id/widget"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        widget:headers="@array/headers" >
    </com.sample.custom.Widget>
</LinearLayout>

Arrays.xml is

<resources>

    <string-array name="headers">
        <item>Header1</item>
        <item>Header2</item>
        <item>Header3</item>
    </string-array>

</resources>

Answers


did you try recycling the array at the end of view constructor? this link covers most of stuff - creating custom views


After go through the several samples i figured out the problem.

Just replaced the line

TypedArray attr = context.obtainStyledAttributes(attrs,
        R.styleable.Widget);

with this

TypedArray attr = context.getTheme().obtainStyledAttributes(attrs,
        R.styleable.Widget,0,0)

Need Your Help

Using DataTrigger to apply different Views to the same ViewModel

wpf mvvm datatemplate datatrigger

I have a ViewModel where I created a bool DisplaySummary property. When this is true, a SummaryView is to be use to render that ViewModel, otherwise a DatailedView is to be used.

Random is returning the same output every time

c++ random vector

I wrote this code which is working fine but everytime the output is the same. So nothing random about it. Curious to know why!