Marusia
Marusia

Reputation: 13

Drawing a circle in android

I've been learning android for a week.i'm writing a simple program which draws a circle.but when i run it it tells me that the program has stopped. i read the code again and again but couldn't find the error. can you please help me.

package org.example.viewwithlines;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class MainActivity extends Activity {


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

    static public class GraphicsView extends View 
    {
        Paint p;
        public GraphicsView(Context context) {
        super(context);
        p=new Paint();
        p.setColor(Color.MAGENTA);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawCircle(30, 40, 10, p);   
        }
        }
}

and this is the xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <org.example.viewwithlines.MainActivity.GraphicsView
    android:id="@+id/graphics" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</LinearLayout>

Upvotes: 0

Views: 4195

Answers (3)

JPMagalhaes
JPMagalhaes

Reputation: 3621

What are you trying to do?

For backgrounds, and general uses, you can do it simply creating a drawable resource and setting it to a square view:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid android:color="#FF0000"/>
</shape>

Upvotes: 1

Vladimir Mironov
Vladimir Mironov

Reputation: 30864

It's hard to say the exact reason why it doesn't work. I see at least two reasons.

  1. Class name in the layout is wrong. Since GraphicsView is a nested class, it should be org.example.viewwithlines.MainActivity$GraphicsView

    <view
        class="org.example.viewwithlines.MainActivity$GraphicsView"
        android:id="@+id/graphics" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
    
  2. You have to provide a constuctor that takes Context and AttributeSet as arguments

    public GraphicsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        p=new Paint();
        p.setColor(Color.MAGENTA);
    }
    

Upvotes: 1

Geobits
Geobits

Reputation: 22342

When you extend a view, you need to add different constructors to it to make it work in different circumstances. When you use a View in xml, it uses the constructor with AttributeSet included.

Try adding one that looks like this:

    public GraphicsView(Context context, AttributeSet attribs) {
        super(context, attribs);
        p=new Paint();
        p.setColor(Color.MAGENTA);
    }

See this post, also, for a more detailed explanation.

Also, you're trying to reference an inner class in xml. When you do that, you have to use a $ instead of a .. The problem with this is that $ is an illegal character in xml tag names, so you have to do something like this instead:

<view class="org.example.viewwithlines.MainActivity$GraphicsView" 
    ...
    attribs here
    ... />

Upvotes: 0

Related Questions