0

I'm sure this is a stupid question but I just can't find an answer anywhere. I'm trying to create a custom View XML element in Android (using Xamarin, so it's technically C#, although I don't think that matters here). I've found a bunch of tutorials, but no one seems to explain where on earth the path comes from. For example, the google example looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 <com.example.customviews.charting.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>

Where did com.example.customviews.charting come from? None of the examples I've found explain how this path is created. I found someone saying it was the package name, but my package name doesn't look anything like that (maybe I did something wrong when generating the file?):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AndroidDemo.AndroidDemo" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">

Right now I've got this in my layout:

<AndroidDemo.AndroidDemo.DragRectView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dragRect" />

Which results in an Android.View.InflateException and a java.lang.ClassNotFoundException.

I'm sure I've got the path to DragRectView (my class) wrong; can anyone give me some direction on how to figure out what it is?

gmaster
  • 692
  • 1
  • 10
  • 27
  • **Where did com.example.customviews.charting come from?** that is the package name where you have CustomView PieChart – Raghunandan Jul 16 '15 at 14:57
  • also why do you have package name as `AndroidDemo.AndroidDemo` . You should have batter names to avoid confusion – Raghunandan Jul 16 '15 at 14:59
  • @Raghunandan I have no idea, that is what it defaulted to when I created the project. Will just changing that one line change the package name or do I have to do something else. – gmaster Jul 16 '15 at 15:06

2 Answers2

6

When you use a custom view in an XML layout, you use the fully qualified class name (which includes the package name). Use the package name where your custom View class resides.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
5

com.example.customviews.charting is the package your custom view lives in, so com.example.customviews.charting.PieChart is the class PieChart in the package com.example.customviews.charting.

If you don't actually have the class PieChart in com.example.customviews.charting you will get your ClassNotFoundException.

In your custom view class file look for this line of code to know what package you are in:

package com.mypackage;

If it's not there you're in the default package and I would suggest adding it to make life easier.

Your class should be something like this

package com.mypackage;
public class PieChart extends View {
    ...Your Implementation of PieChart goes here...
}
JohanShogun
  • 2,956
  • 21
  • 30
  • I understand that, my point is that I don't know how to get the equivalent path for MY code. – gmaster Jul 16 '15 at 15:07
  • At the top of your java class file there will be a line of code with the package: package com.mypackage; To access a specific file in your com.mypackage package use com.mypackage.myfile – JohanShogun Jul 16 '15 at 15:10