0

Well, this is something that i am not able to find anywhere. Its might be written somewhere but my may be due to my poor skill of searching i am not able to find it.

So basically what i want to do is, i want to create a class in which i pass String array and from that array the class will return me a view with number of button as the 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 i am not able to do this. Because View class does not support String array in constructor parameter. Do any one have any solution for this? Should i move to any new approach to achieve this?

Thanks,

Jay Stepin.

Jay Pandya
  • 507
  • 6
  • 26
  • Your requirements seems like you need a custom adapter along with a listview or gridview which is customized as your need. – Anuj Sharma Nov 13 '14 at 07:03
  • @AnujSharma OP can create a new view and define an XML attribute that decides the number of buttons. Another XML attribute that links to a string array or something that provides the text for the buttons. :) – An SO User Nov 13 '14 at 07:13

1 Answers1

5

First, declare your attributes as 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 you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name].

Something like this for a 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 your code, you need something along these lines:

public PieChart(Context context, AttributeSet attrs) {
   super(context, attrs);
   TypedArray a = 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);
   } finally { 
       a.recycle();
   } 
}   

SOURCE:

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

FURTHER. READING:

Defining custom attrs

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Hi Little Child. Sorry about incomplete question here. But i want to implement this class programmatically, so don't want to use xml here. So with attributes yes we can achieve. But dont know the way how to include this attributes to class programmatically, i mean in Java file. – Jay Pandya Nov 13 '14 at 08:15
  • I don't understand what you mean by "include this attribute to class programatically" :) give me an example – An SO User Nov 13 '14 at 08:24
  • What if i don't want to use any xml file for my custom class here. Still i be able to use this attribute in my custom class? – Jay Pandya Nov 13 '14 at 09:11