Hello. Please help with writing the first widget. I do not count on massive use, I want to do it purely for myself . The widget will inherit from FrameLayout . In xml you will need to write at least one view inside the widget. And if the developer writes one layout , then he will be counted for one view , regardless of the number of view inside it.

Please help in one. How can I get all the view inside it, if their number is unknown?

    1 answer 1

    The number of View inside the ViewGroup can be obtained using getChildCount () , a link to each of them can be obtained using getChildAt () .

     int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); ... } 
    • Thank you)) And such a question, where to write it? The widget has three constructors now, so you need to create a method to get all the view and call it in each constructor? Or do not need all three constructors? - Flippy
    • one
      In the sense of where? To any place where you need to do something with the views inside your FrameLayout. If you don’t need to do anything with them, then don’t write anywhere. - tse
    • @ SergeyGrushin you can call one constructor through the other through the auxiliary word this . You can also make a separate initialization method and call it in all constructors, depending on the task. Almost all guides on the custom View have it all - pavlofff