How to add custom view to the layout?

47,331

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.

Share:
47,331
AnasBakez
Author by

AnasBakez

Updated on July 09, 2022

Comments

  • AnasBakez
    AnasBakez almost 2 years

    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>
    
  • AnasBakez
    AnasBakez about 12 years
    it is giving me exception! the GraphicsView class is within the activity class
  • Lalit Poptani
    Lalit Poptani about 12 years
    Create a seperate class for GraphicsView class.
  • AnasBakez
    AnasBakez about 12 years
    it 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
    AnasBakez about 12 years
    iam 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
    AnasBakez about 12 years
    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?
  • user
    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
    AnasBakez about 12 years
    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?
  • user
    user about 12 years
    @AnasBakez Try: class="gen.customfonts.namespace.CustomFontsActivity$Graphic‌​sView"
  • user
    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 the View super class.
  • AnasBakez
    AnasBakez about 12 years
    when i put view (small letter) it gives exception
  • AnasBakez
    AnasBakez about 12 years
    it workeed when i put the class in seperate class and put the constructors :)
  • WORMSS
    WORMSS about 9 years
    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.
  • SingularityFuture
    SingularityFuture over 7 years
    Thanks, this was very helpful.
  • Reejesh PK
    Reejesh PK almost 5 years
    i was getting errors when i used a single constructor, but after using all these three, im not getting errors!
  • wannik
    wannik over 2 years
    try static inner class.