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!

1

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

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?
@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.
@piavgh See Edit 1 above.
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?
"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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.