wyoskibum
wyoskibum

Reputation: 1859

Android: Place button on top of drawn line

I'm dynamically creating a custom view which is an interactive flow chart using buttons, images, and lines. I create a child view for each of the elements and then add them to the master view group. ie:

// lines
LineDrawView lineView = new LineDrawView(getActivity(),lineArray);
lineView.setClickable(false);
layout.addView(lineView);

Then my buttons:

Button button = new Button(getActivity());
button.setText(title);
layout.addView(button, new AbsoluteLayout.LayoutParams(width, height, x, y));

All of my layout parameters are predefined and stored in a database which defines each flow chart. I'm getting the effect that I want with the exception of my lines. In some cases, I'm drawing just one line and I would like for the button to be on top of the line. Instead, my line is always in the forefront. ie:

flowchart button

My question is how do I force my line view to be in the background? Here is my line view code:

public class LineDrawView extends View {

private int KMULTIPLIER = 2;
private ArrayList<String []> lineArray;

public LineDrawView(Context context, ArrayList<String []> lineArray) {
    super(context);
    this.lineArray = lineArray;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);;
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);

    // determine the screen size & initialize CONSTANT
    int screenSize = getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK;

    switch(screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            KMULTIPLIER = 4;
            break;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            KMULTIPLIER = 3;
            break;
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
            KMULTIPLIER = 2;
            break;
        default:
    }

    // draw the lines
    if (lineArray.size() > 0) {
        for (int i=0;i < lineArray.size();i++) {
            String[] array = lineArray.get(i);
            int x = Integer.parseInt(array[0].trim())*KMULTIPLIER;
            int y = Integer.parseInt(array[1].trim())*KMULTIPLIER;
            int x2 = Integer.parseInt(array[2].trim())*KMULTIPLIER;
            int y2 = Integer.parseInt(array[3].trim())*KMULTIPLIER;

            canvas.drawLine(x, y, x2, y2, paint);
        }
    }   
}

}

Any suggestions would be greatly appreciated. Thanks in advanced.

Upvotes: 0

Views: 165

Answers (1)

pskink
pskink

Reputation: 24720

try this : layout.addView(lineView, 0);

Upvotes: 1

Related Questions