Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next view id as a custom attribute in android

For my custom view I have also defined a custom attribute for keep id of the view. Its format is "reference".

in layout xml it is defined like below, very similar with android:layout_below attr

<mycustomview id="@+id/cv_1" xyz:nextviewId="@id/cv_2"... />
<mycustomview id="@+id/cv_2" xyz:nextviewId="@id/cv_3"... />
...
<LinearLayout ...>
    <mycustomview id="@+id/cv_3" xyz:nextviewId="@id/cv_4"... />
</LinearLayout>
...

it gives me error I think it is because it is not declared yet.

Any suggestion for accessing the next object similar to this approach!!!

I am thinking to use tag attr for the next object find the next one with findByTag function. Is this a good way to do it.

like image 376
Onur Topal Avatar asked Jun 21 '11 14:06

Onur Topal


People also ask

What is a custom view in Android?

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.

What is attr in XML Android?

References in XML Thus, it is possible to combine color resources with the other simple resources in the XML file under one element: <resources>, but I don't recommend this practice. On the other hand ? attr is a reference to the theme attribute.

What is the name of the file which is used to declare custom attributes?

What is the name of the file which is used to declare custom attributes? Create an XML res/values/attrs. xml file to define new attributes alongwith their data type.


1 Answers

Change your xml to:

...
<mycustomview id="@+id/cv_1" xyz:nextviewId="@+id/cv_2"... />
<mycustomview id="@+id/cv_2" xyz:nextviewId="@+id/cv_3"... />
...

(note the @+id in nextviewId)

This will work on Android 1.6+ (Api Level 4+). Exactly the same approach is used in RelativeLayouts.

like image 141
inazaruk Avatar answered Sep 19 '22 00:09

inazaruk