pulancheck1988
pulancheck1988

Reputation: 2064

Android & custom views & custom xml attributes

Say i have a valid custom view, named com.package.MyCustomView (MyCustomView extends LinearLayout or any View)

I can use this in a activity layout like this

<com.package.MyCustomView
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

But i would like to have something like this:

<com.package.MyCustomView
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        MyCustomView_attribute1="100"
        MyCustomView_attribute2="fantastic"/>

I'm imagining that in case of valid android xml attributes like android:layout_width the associated value (match_parent, wrap_content) will be set on a specific attribute of the parent View object (by use reflection or whatever.. ). I also am aware of the existence of the namespace (that line: xmlns:android="http://schemas.android.com/apk/res/android")..

In any case I would like something of the same kind - custom xml attributes for my custom for my view...

Is this possible? If yes - how? please give some details

Upvotes: 0

Views: 171

Answers (2)

s.d
s.d

Reputation: 29436

Declare a stylable resource and some custom attributed with it. Official android tutorial.

Upvotes: 1

Janmejoy
Janmejoy

Reputation: 2731

Yes you can perform like this...it will work

  <com.example.application.Drawer
         xmlns:my="http://schemas.android.com/apk/res/com.example.application"
         android:id="@+id/drawer"
         android:layout_width="301dp"
         android:layout_height="wrap_content"
         my:content="@+id/content"
         my:handle="@+id/handle" >

But one thing is that you should have the above package in src

Upvotes: 0

Related Questions