How to add custom view to the layout?
Solution 1
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"/>
Solution 2
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.
Solution 3
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);
}
}
Solution 4
you need to do this:
LinearLayout v = (LinearView) findViewById(R.id.linnnnlayout);
GraphicsView myView = new myGraphicsView(this);
v.addView(myView);
Solution 5
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.
AnasBakez
Updated on July 09, 2022Comments
-
AnasBakez almost 2 years
I have a
GraphicsView
class that extends from theView
class and I want to add thisGraphicsView
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>
-
AnasBakez about 12 yearsit is giving me exception! the GraphicsView class is within the activity class
-
Lalit Poptani about 12 yearsCreate a seperate class for GraphicsView class.
-
AnasBakez about 12 yearsit is still giving me exceptions here is the code <com.genie.customfonts.namespace.GraphicsView android:id="@+id/view" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
-
AnasBakez about 12 yearsiam putting this code in the main.xml is it right? and my pakage name is package gen.customfonts.namespace; so is that right to use the view as i had sent to you earlier? thanks for helping
-
AnasBakez about 12 yearsi 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?
-
user about 12 years@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. -
AnasBakez about 12 yearsi 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?
-
user about 12 years@AnasBakez Try:
class="gen.customfonts.namespace.CustomFontsActivity$GraphicsView"
-
user about 12 years@AnasBakez I think I found the problem. Use the code above with the
class
attribute but instead of<View class="...
use<view class="...
(view
without the capital letter). Also don't forget to implement the other two constructors from theView
super class. -
AnasBakez about 12 yearswhen i put view (small letter) it gives exception
-
AnasBakez about 12 yearsit workeed when i put the class in seperate class and put the constructors :)
-
WORMSS about 9 yearsIs 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.
-
SingularityFuture over 7 yearsThanks, this was very helpful.
-
Reejesh PK almost 5 yearsi was getting errors when i used a single constructor, but after using all these three, im not getting errors!
-
wannik over 2 yearstry static inner class.