Android: Custom view class with custom parameters -


well, not able find anywhere. might written somewhere may due poor skill of searching not able find it.

so want is, want create class in pass string array , array class return me view number of button number of elements in string array.

something like,

public class customview extends view {         public customview(context context, attributeset attrs, string[] array) {             super(context, attrs, array);         } } 

but not able this. because view class not support string array in constructor parameter. 1 have solution this? should move new approach achieve this?

thanks,

jay stepin.

first, declare attributes follows:

<resources>    <declare-styleable name="piechart">        <attr name="showtext" format="boolean" />        <attr name="labelposition" format="enum">            <enum name="left" value="0"/>            <enum name="right" value="1"/>        </attr>    </declare-styleable> </resources>   

once define custom attributes, can use them in layout xml files built-in attributes. difference custom attributes belong different namespace. instead of belonging http://schemas.android.com/apk/res/android namespace, belong http://schemas.android.com/apk/res/[your package name].

something custom piechart class:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">  <com.example.customviews.charting.piechart      custom:showtext="true"      custom:labelposition="left" /> </linearlayout>   

in code, need along these lines:

public piechart(context context, attributeset attrs) {    super(context, attrs);    typedarray = context.gettheme().obtainstyledattributes(         attrs,         r.styleable.piechart,         0, 0);      try {         mshowtext = a.getboolean(r.styleable.piechart_showtext, false);        mtextpos = a.getinteger(r.styleable.piechart_labelposition, 0);    } {         a.recycle();    }  }    

source:

http://developer.android.com/training/custom-views/create-view.html

further. reading:

defining custom attrs


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -