1

I've create a Custom View. Now I want to create a class that has some Custom View as components (array of Custom View maybe). For example, something like Button b = new Button(this), how can I apply it for my custom view?

Because the constructor of custom view is CustomView(Context context, AttributeSets attrs), and in the new class that I created, I don't have context or attrs?

Thank you!

0xh8h
  • 3,271
  • 4
  • 34
  • 55
  • possible duplicate of [android save programmatically created views](http://stackoverflow.com/questions/14033619/android-save-programmatically-created-views) – FredTheWebGuy Aug 17 '13 at 00:47

1 Answers1

1

Add this constructor to your custom view class:

public CustomView(Context context) {
    mContext = context
}

This is how you would use the custom view:

If you need the custom view to be the only view:

CustomView cv = new CustomView(this);
setContentView(cv);

If you want to add custom view to a parent view:

// inflate mainXML
View mainView = getLayoutInflater().inflate(R.layout.mainXML, null);

// find container
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);

// initialize your custom view
CustomView view = new CustomView(this);

// add your custom view to container
container.addView(view);

setContentView(mainView);

By the way, this should work too:

CustomView cv = new CustomView(this, null);

Edit 1:

Use nested for-loops:

LinearLayout childLL;
CustomView cv

for (int i = 0; i < 8; i++) {
    childLL = new LinearLayout(this);
    for (int j = 0; j < 8; j++) {
        cv = new CustomView(this);
        // set LayoutParams
        childLL.addView(cv);
    }
    container.addView(childLL);
}

setContentView(container);
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thank you for quickly answer! But my problem is, I try to create a new View that contains 10 or 20 of this Custom View. You can imagine like a chessboard, each square is a CustomView and the whole chessboard (64 squares) is the new View that I try to create in my situation. And then that chessboard is what I will add to the mainView, not a separate CustomView. In order to do that, I have to create some CustomView object (an array of CustomView) in my chessboard, right? – 0xh8h Aug 17 '13 at 01:04
  • @piavgh Correct. Create a layout xml file and place a `LinearLayout` inside it. If you wish to use the code above, give the `LinearLayout` an `id=container` and `orientation=vertical`. To create the view you want, use a `nested for-loop`. In the outer loop, you will create a LinearLayout with horizontal orientation. In the inner loop, you will create an instance of CustomView and add it to the LinearLayout. – Vikram Aug 17 '13 at 01:12
  • @piavgh See **Edit 1** above. – Vikram Aug 17 '13 at 01:18
  • Thank you very much, I know your code will work perfectly, but I still have problem with my onDraw() method override. It basically draws all of my custom view at the same position. So I can only saw 1 Custom View @Override protected void onDraw(Canvas canvas) { paint.setColor(this.backgroundColor); canvas.drawRect(x, y, x + width, y + height, paint); paint.setColor(this.letterColor); paint.setTextSize(30); canvas.drawText(letter, x + width / 2, y + height / 2, paint); } How can I tell the onDraw just to put my Custom View in the LinearLayout one after one regardless of (x, y) coordinate? – 0xh8h Aug 17 '13 at 02:16
  • "You must have 20 reputation on Stack Overflow to talk here. See the faq." Sorry but my reputation is not enough to chat. I've google searched for a while but every onDraw method must provide a coordinate, do I have to override other method? – 0xh8h Aug 17 '13 at 04:42
  • yes, it's exactly what I want. Do you use the same method that you posted on Edit 1? – 0xh8h Aug 17 '13 at 07:38