Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Custom View Constructor

I'm learning about using Custom Views from the following:

http://developer.android.com/guide/topics/ui/custom-components.html#modifying

The description says:

Class Initialization As always, the super is called first. Furthermore, this is not a default constructor, but a parameterized one. The EditText is created with these parameters when it is inflated from an XML layout file, thus, our constructor needs to both take them and pass them to the superclass constructor as well.

Is there a better description? I've been trying to figure out what the constructor(s) should look like and I've come up with 4 possible choices (see example at end of post). I'm not sure what these 4 choices do (or don't do), why I should implement them, or what the parameters mean. Is there a description of these?

public MyCustomView() {     super(); }  public MyCustomView(Context context) {     super(context); }  public MyCustomView(Context context, AttributeSet attrs) {     super(context, attrs); }   public MyCustomView(Context context, AttributeSet attrs, Map params) {     super(context, attrs, params); }  
like image 533
Mitch Avatar asked May 21 '10 18:05

Mitch


People also ask

What are custom views in Android?

Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View and ViewGroup . To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses — called widgets and layouts, respectively — that you can use to construct your UI.

Can you create custom views How?

Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.

What is Attrs XML in Android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.


2 Answers

You don't need the first one, as that just won't work.

The third one will mean your custom View will be usable from XML layout files. If you don't care about that, you don't need it.

The fourth one is just wrong, AFAIK. There is no View constructor that take a Map as the third parameter. There is one that takes an int as the third parameter, used to override the default style for the widget.

I tend to use the this() syntax to combine these:

public ColorMixer(Context context) {     this(context, null); }  public ColorMixer(Context context, AttributeSet attrs) {     this(context, attrs, 0); }  public ColorMixer(Context context, AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle);     // real work here } 

You can see the rest of this code in this book example.

like image 118
CommonsWare Avatar answered Oct 04 '22 22:10

CommonsWare


Here's a my pattern (creating a custom ViewGoup here, but still):

// CustomView.java  public class CustomView extends LinearLayout {      public CustomView(Context context) {         super(context);         init(context);     }      public CustomView(Context context, AttributeSet attrs) {         super(context, attrs);         init(context);     }      public CustomView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         init(context);     }      private void init(Context ctx) {         LayoutInflater.from(ctx).inflate(R.layout.view_custom, this, true);             // extra init     }  } 

and

// view_custom.xml  <merge xmlns:android="http://schemas.android.com/apk/res/android">     <!-- Views --> </merge> 
like image 39
yanchenko Avatar answered Oct 04 '22 20:10

yanchenko