Question

Simple question, i have this code

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    ...

    <LinearLayout
        android:id="@+id/insideLinearLayout1"
        ...
        <TextField>
          android:text='beforeClick'
          ...

         <Button
            android:id="@+id/button1"
            android:onClick="updateExpression"
            ...


   </Linear Layout 

   <LinearLayout
        android:id="@+id/insideLinearLayout2"
        ...

          <TextField>
          android:text='beforeClick'
          ...
         <Button
            android:id="@+id/button1"
            android:onClick="updateExpression"
            ...


   </Linear Layout 

    </LinearLayout>
</HorizontalScrollView>

The idea, is to change the textField text property, when clicking the button on the same layout!

Ok for 2 is easy, just need the reference of each button and each textfield and change use the get and set method! But i want to add more layouts dynamically, and grab each one's reference would be a hard task.

So two questions :

  1. How can i grab the id of the Linear layout, that have the clicked button?

    I want to handle the 'insideLinearLayout(1 or 2 depending on the clicked button)'id in the 'updateExpression'!

  2. How can i add more layouts with the same widgets as the ones created manually?

Thank you in advance. Best of codings!

Was it helpful?

Solution

1)Your onClick function is passed the view that was clicked. Every view has a getParent() function. You can use it to get the LinearLayout, then get the id.

2)Create them with the new keyword then add them to the parent layout. For something like this I would probably make a custom compund view holding everything you want to instantiate at once, so you can treat the entire set of widgets as one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top