5

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Onur Topal
  • 3,042
  • 1
  • 24
  • 41

1 Answers1

6

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Learned something today. So, in a relative layout, you can use that approach to refer to a view that is defined below the current one? That's awesome! – Steve Prentice Jun 21 '11 at 14:39
  • 1
    I tried but for some reason it is not working in my layout. API Level 7 and code structure is ; It does not give any error but the nextviewId returns 0. – Onur Topal Jun 21 '11 at 19:16
  • thank for your comment I was using ´TypedArray.getInt()´. After your comment I checked the alternatives and apparently I had use ´TypedArray.getResourceId´. – Onur Topal Jun 22 '11 at 06:46