20

I have a GraphicsView class that extends from the View class and I want to add this GraphicsView class to the main layout in my project. How can I do that?

static public class GraphicsView extends View {
        public GraphicsView(Context context) {
        super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {
        // Drawing commands go here
            Path rect = new  Path();
            rect.addRect(100, 100, 250, 50, Direction.CW);
            Paint cPaint = new Paint();
            cPaint.setColor(Color.LTGRAY); 
            canvas.drawPath(rect, cPaint);
        }
    }

and my main.xml:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView 
        android:id="@+id/Customfont"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

     <View 
         android:id="@+id/view"
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
user
  • 86,916
  • 18
  • 197
  • 190
AnasBakez
  • 1,230
  • 4
  • 20
  • 36

7 Answers7

27

You need to give complete path of your class that extends View,

<com.blah.blah.GraphicsView
         android:id="@+id/view"
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
17

If I remember correctly, you need to provide more constructors to use view from xml file (add it to xml file like "Me and We" suggested).

public GraphicsView(Context context) {
    super(context);
}

public GraphicsView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public GraphicsView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

Update: fixed code.

Sver
  • 3,349
  • 6
  • 32
  • 53
8

i finaly got it here is the code the XML code

<com.customfonts.namespace.BreakDownBar
         android:id="@+id/gview"
          android:layout_width="fill_parent"
        android:layout_height="20dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:background="@color/BreakDownBarBack"/>

and the class

package com.customfonts.namespace;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.View;



public class BreakDownBar extends View {

    public BreakDownBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

        @Override
        protected void onDraw(Canvas canvas) {
                //Draw rectangle;
                Path rect = new  Path();
                rect.addRect(0, 0,250, 150,Direction.CW);
                Paint cpaint = new Paint();
                cpaint.setColor(Color.GREEN); 
                canvas.drawPath(rect, cpaint);
        }
}
AnasBakez
  • 1,230
  • 4
  • 20
  • 36
6

you need to do this:

LinearLayout v = (LinearView) findViewById(R.id.linnnnlayout);
GraphicsView myView = new myGraphicsView(this);
v.addView(myView);
thepoosh
  • 12,497
  • 15
  • 73
  • 132
6

Because your custom View is an inner class in your Activity the java compiler will output the name ActivityName$GraphicsView for that class. You can't use that name directly as the View name in the xml layout because of the $ character but you can do it like this:

 <view 
    class="com.package.here.ActivityName$GraphicsView"
    android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

where ActivityName is the name of the activity where your GraphicsView class is declared.

user
  • 86,916
  • 18
  • 197
  • 190
  • i have put the class in seperate file. but iam having exception that says Error inflating class com.gen.customfonts.namespace.GraphicsView i think iam having problem in the class name!! the pakage name is package gen.customfonts.namespace; what should i call the class in the xml? – AnasBakez May 02 '12 at 09:47
  • @AnasBakez Have you tried leaving the `GraphicsView` class where you previously had it, in the activity, and use the code from my answer? Also you should implement the other 2 constructors of the View super class. – user May 02 '12 at 09:57
  • i had tried it but it is not doing any thing! the view in the GraphicsView class is not visible, but it is not giving an exception, i think the class name iam putting is error. the pakage name is package gen.customfonts.namespace; the activity name is CustomFontsActivity what is the class name should be? – AnasBakez May 02 '12 at 10:05
  • @AnasBakez Try: `class="gen.customfonts.namespace.CustomFontsActivity$GraphicsView"` – user May 02 '12 at 10:09
  • @AnasBakez I think I found the problem. Use the code above with the `class` attribute but instead of ` – user May 02 '12 at 10:34
  • when i put view (small letter) it gives exception – AnasBakez May 02 '12 at 11:35
  • it workeed when i put the class in seperate class and put the constructors :) – AnasBakez May 02 '12 at 11:41
  • Is there a reason 'class' does not have a namespace? I know it doesn't work without it, but it just looks odd that no other namespace is defined for the default so not sure how one would expect to know that it is a valid attribute. – WORMSS Mar 09 '15 at 14:01
  • try static inner class. – wannik Jan 24 '22 at 00:47
0
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    LinearLayout v = (LinearLayout) findViewById(R.id.linearLayout);
    MyGraphics myView = new MyGraphics(this);
    v.addView(myView);
}
}


public class MyGraphics extends View {
public MyGraphics(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    // Drawing commands go here
    Path rect = new Path();
    rect.addRect(100, 100, 250, 50, Direction.CW);
    Paint cPaint = new Paint();
    cPaint.setColor(Color.LTGRAY);
    canvas.drawPath(rect, cPaint);
}


}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:id="@+id/linearLayout">

<TextView 
    android:id="@+id/Customfont"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />


</LinearLayout>
Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62
0

This is working for me and add the view in XML with full path and try giving wrapcontent for height and width.

public class RectangleView extends View {
    public RectangleView(Context context) {
        super(context);
    }

    public RectangleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RectangleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.GRAY);
        canvas.drawColor(Color.BLUE);
        canvas.drawRect(10,10,50,50, paint);
    }
}
Antti29
  • 2,953
  • 12
  • 34
  • 36