PcAF
PcAF

Reputation: 2007

How to add CustomView to layout

I know that there are lot of questions about "How to add CustomView to Layout" but I read almost all and i have still problem. CustomView in XML

<slccs.drawingapplication.DrawLine
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/drawline"/>

And Java code of this View

package slccs.drawingapplication; import android.content.Context; import android.view.View;

public class DrawLine extends View {

public DrawLine(Context c){
    super(c);
}}

I am getting this error The following classes could not be found:- slccs.drawingapplication.DrawLine

When I Clean project I will get more errors:

Error:Error: Invalid file name: must contain only lowercase letters and digits ([a-z0-9_.]) it is about my @drawable/Brown Image file. So When I change B to b I`ll get new problem in activity_main xml file:

Couldn't resolve resource @drawable/brown

Failed to convert @drawable/brown into a drawable

I think that Today is AndroidStudio hating me :(

I don`t know where is the problem

Thank you for your answers

Upvotes: 0

Views: 65

Answers (3)

TygerTy
TygerTy

Reputation: 803

I found this tidbit here.

To allow the Android Developer Tools to interact with your view, at a minimum you must provide a constructor that takes a Context and an AttributeSet object as parameters. This constructor allows the layout editor to create and edit an instance of your view.

and it seems that part of your issue is covered here.

As for the @drawable/brown I'm guessing it is either incompatible with the system, or you messed up in the file you created it in. But since I don't know what the drawable is (hint hint, code for that please!), I can't really tell you exactly why it won't compile, but yes, you do NEED to have all lowercase letters when naming it, so brown.yourfiletype is correct.

Try restarting Android Studio.

Upvotes: 1

rahul.ramanujam
rahul.ramanujam

Reputation: 5618

public class DrawLine extends View {

public DrawLine(Context c){
    super(c);
}

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

Upvotes: 2

issamux
issamux

Reputation: 1415

First Declaring this custom view in your layout :

 <slccs.drawingapplication.DrawLine

mean you have a class DrawLine located in the package : "slccs.drawingapplication"

Second , android expect file name to start with lowercase and contain only alphanumerical char and '-' or '.'.

so be sure you ve copied brown picture in your drawable folder and tell us if all is ok now

Upvotes: 0

Related Questions