Search code examples
javaandroidviewsetcontentview

Drawing a shape?


I have tried to draw a shape for a long time without an answer. I used setContentView(view) for that view. However, I have another setContentView:

setContentView(R.layout.activity_main);//Buttons, Views from XML...
setContentView( mCustomDrawableView);//Custom Shape

Problem is, it only shows ONE view, the shape. The Buttons defined on XML are gone. Basically whichever setContentView(view) is called last is the only View who is drawed. I've also tried many other ways to draw the shape without an answer that works.

I have seen that multiple setContentView do overlap each other, but this is the only way that partially works. Any way to display both Views or another way to draw a shape and my XML Views?

EDIT:

My shape is made in java, not XML

EDIT 2:

Here is my customShape Class:

 public class CustomDrawableView extends View {
    private ShapeDrawable mDrawable;

    public CustomDrawableView(Context context) {
        super(context);

        int x = 10;
        int y = 10;


        mDrawable = new ShapeDrawable(new RectShape());
        // If the color isn't set, the shape uses black as the default.
        mDrawable.getPaint().setColor(0xff74AC23);
       mDrawable.setBounds(x,y,x+10,y+10);
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.draw(canvas);
    }
}

Solution

  • OK ... setContentView is something which takes a layout/view/whatever and the input defines the contents of the activity/fragment your are setting it to.

    So of course your first call does the whole layout, and the second wipes that and sets the content to what you're sending to it.

    The thing is to have your view and then add it to a layout. If you have any view in your initial setContentView xml named (+id) and defined (Layout x = getfromid(r.id.x) you can then add your view to that layout with x.addview(myview) or x.addview(CustomDrawableView(context)).

    -edit for clarification-

    An Activity/Fragment has a Layout which you set using setContentView. You do this once, normally. A Layout defines where Views are placed.

    So, you setContentView(your.xml) in onCreate. If you have a programmatic View class, you can then do two things: add new YourView to a named and specified layout in your act/frag (using any layout defined in xml, passed to your frag/act in the xml passed in setContentView and then defined by Layout x = findViewById(R.id.idoflayoutinxmlinfield 'android:id="@+id/x") in code.

    Then you can x.addview(new MyView(context)) or Myview z = new MyView(context) and x.addView(z);

    -edit2-

    When you get better, you can also just add your custom View to the original layout by adding it to the original xml definition.

    -edit3-

    Sigh. OK. Here it is:

    In your activities java file:

    RelativeLayout myRellayout;
    
    @Override
    public void onCreate(){
        setContenView(your.xml)
        myRelLayout = findViewByID(R.id.myrellayout);
        myRelLayout.addView(new MyCustomView(this));
        //or define your custom view and the add it by:
        //MyCustomView mcv = new MyCustomView(this);
        //myRelLayout.addView(mcv);
    }
    

    And your xml should contain any layout like this:

    <RelativeLayout
        android:id="@+id/myrellayout"
        android:width="whatever"
        android:height="whatever"
        />