Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to shorten custom view names?

Let's say that I created a custom view called MyView and want to use it in the xml and the view is located in com.example package. I need to do something like this:

<com.example.MyView
   ......
/>

I need to write the package name every time I use the view. What if I have a long package name?

<what.a.long.long.long.loooooong.packagename.MyView
   ......
/>

That just looks ugly. Is it possible to shorten the package name? Do I need to do something in the AndroidManifest.xml file?

like image 216
Sweeper Avatar asked Jul 28 '15 08:07

Sweeper


2 Answers

Define your own namespace:

<Layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:x="what.a.long.long.long.loooooong.packagename."
    >
    <x:MyView>
    </x:MyView>
</Layout>

xmlns:x spefifies that all tags that start with <x: should be looked up in package that is defined in the namespace definition (what.a.long.long.long.loooooong.packagename. in this case).

You can even use the namespace within the tag itself:

<x:MyView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:x="what.a.long.long.long.loooooong.packagename."
    />
like image 170
gaborsch Avatar answered Oct 17 '22 18:10

gaborsch


You can use my library XmlTag. It lets you use short name of every View that you annotate with @XmlTag.

XmlTag in action

like image 29
Bartek Lipinski Avatar answered Oct 17 '22 17:10

Bartek Lipinski