Android Question Index of a View.

Tom_707

Member
Licensed User
Is it correct to assume that the index of a new View that's just been added, is the total number of Views + 1? As illustrated in the following example:

B4X:
NoOfViews = Activity.GetAllViewsRecursive.Size     'is this syntax correct?
Activity.AddView(lblView, 0, 0, 100dip, 30dip)     'add a new View
lblView.Tag = NoOfViews        'will this be the correct index of the new View?
.
.
.
.
Activity.GetView(lblView.Tag).Top = 100dip        'use the stored index to access the correct view
 

Tom_707

Member
Licensed User
Why don't you use simply:
B4X:
lblVlew.Top  = 100dip

My example is not clear enough. I will rewrite it.

But basically I want to get a View's index. Would my assumption above be correct, that a new View's index is the total number of Views + 1?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Just to clarify.
I suggested
NoOfViews = Activity.NumberOfViews
because in the code example in post#1 the call was made before lblView was added to the Activity.
After adding, the index of lblView will, of course, be Activity.NumberOfViews - 1
 
Upvote 0

Tom_707

Member
Licensed User
Thanks Klaus, thanks Erel.

Another question I have that's related to this is:
If Views don't have indexes, then what is the point of the Activity.GetView(index) method? What index are you supposed to use there?
 
Upvote 0

sfsameer

Well-Known Member
Licensed User
Longtime User
If Views don't have indexes
What do you mean by that? every view added to the activity has an index :
example if the activity has 2 labels the first label index is 1 and second label index is 2
example if the activity has 2 labels the first label index is 0 and second label index is 1

then what is the point of the Activity.GetView(index) method? What index are you supposed to use there?

Depending on your needs the getview is used when you illiterate through the view and get a specific view for example :

B4A:
For i = 0 To p.NumberOfViews -1
            If p.GetView(i) Is Label Then
                Dim lbl As Label
                lbl = p.GetView(i)
                lbl.Text = "changed every label text"
            End If
        Next

The above example is a loop through a panel that has label and every label text will be changed.

it all depends what your needs are.
:)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you have a reference to a view then you don't need to use GetView.

GetView is useful when you don't have a reference to the view and you know the index because you created the layout.
For example:
B4X:
CLV.GetPanel(10).GetView(3).Text = "abc"
Get item number 11 from the CLV. Get the label which is the 4th view from the item and update its Text property.
 
Upvote 0

cklester

Well-Known Member
Licensed User
What do you mean by that? every view added to the activity has an index :
example if the activity has 2 labels the first label index is 1 and second label index is 2

Shouldn't that be, "if the activity has 2 labels the first label index is 0 and second label index is 1"?

I thought B4X does 0-based indexing.
 
Upvote 0

sfsameer

Well-Known Member
Licensed User
Longtime User
Shouldn't that be, "if the activity has 2 labels the first label index is 0 and second label index is 1"?

I thought B4X does 0-based indexing.
exactly it is but it's a figure of speech because i didn't want to him to get confused :)
 
Upvote 0

Tom_707

Member
Licensed User
What do you mean by that? every view added to the activity has an index :
example if the activity has 2 labels the first label index is 1 and second label index is 2

If I use Activity.RemoveViewAt to delete the first label, what will be the index of the second label (ignoring 0-based indexing)? Will it go to 1, or stay at 2?
 
Upvote 0

Tom_707

Member
Licensed User
This has become a long thread.
But, I still don't know why you want or need it?
What is the goal you want to achieve?
My app has a dynamic layout, where Views are added or deleted constantly during run-time, based on various criteria.

I want to be able to access a View directly via its Tag or ID, and then work with it. I don't want to have to keep track of every time the layout changes. And finding a View by cycling through ALL Views, every single time I want to work on a View, seems so haphazard. As in the following code:
B4X:
For Each v As View In Activity.GetAllViewsRecursive
   If v.Tag = 5 Then    'Using 5 as an example
      v.Width = 100dip
   End If
Next


Android has the following method:
Java:
public final T findViewWithTag (Object tag)


So basically my question is this - is there any way to achieve something like this in B4X:
B4X:
Private v As View
.
.
v = Activity.FindViewWithTag(5)
v.Width = 100dip
 
Upvote 0
Top