LucaSC
LucaSC

Reputation: 792

How to create a view on an activity

So I have this class which extends an activity. But I want to draw something on the screen, so I need to make a canvas. However I can't extends View, because it's an activity allready. What should I do?

My activity has the onClick method which I use to do some stuff, but what I wanna do is draw a simple image when I call the onClick method as well.

Thanks.

public class Stuff extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
(...)
}

@Override
public void onClick(View arg0) {
(...)
}

Upvotes: 4

Views: 10369

Answers (3)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

STEP 1: create a class by extends View as:

public class DrawView extends View {  
    public float currentX=40;  
    public float currentY=50;  

    public DrawView(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  

    @Override  
    protected void onDraw(Canvas canvas) {        
        super.onDraw(canvas);   
        Paint paint=new Paint();  
        paint.setColor(Color.RED);  
        canvas.drawCircle(currentX, currentY, 25, paint);  
    }  

} 

STEP 2: In Your Stuff Activity :

public class Stuff extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);  
setContentView(R.layout.main);  
LinearLayout root=(LinearLayout) findViewById(R.id.root); 
(...)
}

@Override
public void onClick(View arg0) {
//DRAW YOUR VIEW ON BUTTON CLICK
final DrawView drawView=new DrawView(this);  
drawView.setMinimumWidth(300);  
drawView.setMinimumHeight(500);
drawView.currentX=200;  
drawView.currentY=200;  
drawView.invalidate(); 
root.addView(drawView);
(...)
}

STEP 3: Your Activity main.xml as :

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical"   
    android:background="#99FFCC"  
    android:id="@+id/root">  
</LinearLayout>

and finally try to search on google before asking question here.thanks

Upvotes: 6

shailbenq
shailbenq

Reputation: 1470

You can declare an inner class within ur activity for ex refer this code:

    public class GraphicsTest extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GraphicsTestView(this));
  }


  private static class GraphicsTestView extends View
  {
    private ShapeDrawable mDrawable =
        new ShapeDrawable();
    /* Drawable's are objects which can be drawn on a Canvas
       ShapeDrawable is used to draw primitive shapes such as:
       ArcShape, OvalShape, PathShape, RectShape, RoundRectShape
       A Canvas is the object provided by Android on which
       a view tries to draw itself. In addition to ShapeDrawable,
       there are other subclasses of Drawable like PictureDrawable,
       RotateDrawable, ScaleDrawable, ClipDrawable,GradientDrawable, etc
       Some of these we will see when we consider the XML approach to
       graphics 
     */

    public GraphicsTestView (Context context) 
    {
       super(context);
       setFocusable(true);
       this.mDrawable.getPaint().setColor(0xFFFF0000); 
           //argb where a is alpha (transparency)
    }

    @Override
    protected void onDraw(Canvas canvas)
    /* the onDraw method is where a view draws itself
           this is our first time overriding it.
         */
    {
        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;
        this.mDrawable.setBounds(x, y, x + width, y + height);
        this.mDrawable.draw(canvas);

        ArcShape arc = new ArcShape(45,90); //start angle, sweep angle
        ShapeDrawable test = new ShapeDrawable(arc);

        Paint p = test.getPaint();
        p.setColor(0xFF00FFFF);

        p.setStyle(Paint.Style.STROKE);

        test.setBounds(10, 70, 310, 370); 
            //Top-Left, Bottom Right of rectangle to draw into
        test.draw(canvas);

        }
    }
}

Upvotes: 1

scotch
scotch

Reputation: 475

Are you saying you want to get the layout view in your XML file? You can draw the views in it, get your code to call it and view it, and then set the images to respond when clicked.

In your onCreate method, after super.onCreate(savedInstanceState); add this setContentView(R.id.layoutname)

For example

@Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

As for the onClickListener, you can set the views to implement it like this since you already implement it in the Activity.

    // set this after "setContentView(R.layout.main);"
b1 = (Button)findViewById(R.id.main);
b1.setOnClickListener(this);

Upvotes: 0

Related Questions