Question

I have some canvas that contains text (in canvas) I want to add below this text a button. I know that Canvas can't hold a button and I don't want to add the button to the canvas.

How can I add the button to the RelativeLayout below the text that I draw in canvas?

EDIT:

The canvas is not declared in any xml or layout I have a class that extends RelativeLayout and I get the canvas from dispatchDraw function

Was it helpful?

Solution

Your canvas is contained in a view, so place the button below that view.

    android:layout_below="@+id/yourCanvasViewId" 

Check out the RelativeLayout developer guide.

To do this in code, you could do something like this:

    int canvasViewId = 1;
    myCanvasView.setId(canvasViewId);

    Button button = new Button(this);
    button.setText("Button");

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    //set view to be laid out below canvas view
    params.addRule(RelativeLayout.BELOW, canvasViewId); 

    myRelativeLayout.addView(button, params);

OTHER TIPS

You can use the layout parameter android:layout_above in your layout XML file. Give each component an id and use android:layout_above or android:layout_below to do this. You could also do it in a LinearLayout

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top