1

[android] I have a LinearLayout which contains 2 views, 1st is imageView and th 2nd one have canvas on it. if I do mLinearLayout.addView(i); and then mLinearLayout.addView(c); it shows both but if I do it in reverse oder (mLinearLayout.addView(c) and then mLinearLayout.addView(i)) the it only shows canvas. I wanted to share the screen between these 2 views. Can anybody help me on this?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLinearLayout = new LinearLayout(this);

    mLinearLayout.setBackgroundColor(0xff74AC23);
    ImageView i = new ImageView(this);
    View c = new DrawView(this);

    i.setImageResource(R.drawable.bol_groen);
    i.setAdjustViewBounds(true); 

    mLinearLayout.addView(i);
    mLinearLayout.addView(c);
    setContentView(mLinearLayout);

}

}

public class DrawView extends View {

private ShapeDrawable mDrawable;

public DrawView(Context context) {
    super(context);
    setFocusable(true); //necessary for getting the touch events

    mDrawable = new ShapeDrawable(new OvalShape());
    mDrawable.getPaint().setColor(0xff74AC23);
    mDrawable.setBounds(x, y, x + width, y + height);

}

@Override protected void onDraw(Canvas canvas) {

    canvas.drawColor(0xFFCCCCCC);
    mDrawable.draw(canvas);

}

goodnames
  • 11
  • 1
  • 2
  • What do you mean it only shows canvas? The DrawView takes up the whole screen, or is the right size but the ImageView just does not show? Also is there a reason you are not doing this in xml? – Idistic Jul 21 '11 at 02:02
  • Please have a look on this article; hope will help you- http://stackoverflow.com/questions/11071258/scale-a-canvas-to-fit-a-view-programmatically – Kumar Shorav Jul 20 '12 at 04:56

1 Answers1

0

Give the following a try.

Take your draw statement out of ondraw then call DrawView or some shape or what ever.

//some shape// 

Also I Dont See your X And Y, or height or width.

public DrawView(Context context) {     
    super(context);
    setFocusable (true); //necessary for getting the touch events
     mDrawable = new ShapeDrawable(new OvalShape());
     mDrawable.getPaint().setColor(0xff74AC23);

     mDrawable.setBounds(x, y, x + width, y + height);  
     mDrawable.draw(canvas); 
}  
Rui Marques
  • 8,567
  • 3
  • 60
  • 91
Fritz
  • 1