I created a TextView object, set all the necessary properties for it. Now I need to generate many of the same objects, but with different coordinates. Is it possible to create a copy of this object? I only know how to get a link to the object.

  • In OOP, the creation of new objects, as a rule, occurs not by cloning existing ones, but by creating a new instance of the object myObject =new Object() . - pavlofff

2 answers 2

It is possible to call the object's clone() method. You can read here

    Create a method that will return an object, in your case a TextView , with the properties you need, common to all created objects - in the method body, changing - pass the method arguments:

     private TextView myTextView (int x, int y){ TextView textView = new TextView(this); // установка необходимых свойств объекта return textView; } 

    using:

     TextView textView1 = myTextView (15, 20); TextView textView2 = myTextView (40, 20); 

    PS: Setting some absolute coordinates for widgets in android development is bad practice - this is not done, due to the fact that there are a large number of devices with different screen sizes and resolutions - on different devices this approach will give a different arrangement of widgets.

    • And how in this situation to add event handling? For example, to call the method when clicking on this object? - Alexander