0

enter image description here

When the triangle pressed (selected), and then if I press right or left button it will add another triangle on the right or left side of the triangle I pressed.

For example, lets say I clicked the main triangle (ID 100) and then I pressed the Right button, it should add a triangle on my Right, also it must assign a unique address (ID) to the new triangle

*when generating an ID for the triangle it must be 3-digit (from 101-999)

For example, lets say I clicked the main triangle (ID 100) and then I pressed the Left button, it should add a triangle on my Left, also it must have a random 3-digit ID.

enter image description here

Let's say I clicked the main triangle (ID 100) and then I pressed the Left button and the Right button, it should add a triangle on my Left and also my right.

If there is already a left triangle and then the user pressed Left button again, it should not add anything to the left, the same thing on the Right button.

enter image description here

User clicked the triangle (111) then pressed the Left Button

enter image description here

Then User clicked the new triangle (511) and pressed the Right and Left buttons And so on, I hope you got the Idea

enter image description here

How is it possible to make such layout. I thought to do it making many triangle layouts and putting them to single ConstraintLayout then making them android:visibility="gone" but how can I make a specific layout visible on click. I'm unable to do it in Java. What should I do??

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Better create a [Custom-View](https://developer.android.com/guide/topics/ui/custom-components). To get started you can check source code of some custom view available open source. – ADM Jan 05 '21 at 07:00

2 Answers2

1

you should set id for every view, these placed in XML should have android:id attr, these created during runtime should have setId(View.generateViewId())

keep reference (some array? or list?) to every View and for changing visibility use view.setVisibility(View.VISIBLE)

btw. I doubt that you achieve what you want (triangles) with separated Views, this should be done using custom drawing on some custom, extended View inside onDraw method (some DOC in here)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I'm unable to understand that how do I place these triangles on left and right sides of selected triangle by pressing the left or right button. – Mubashar Hussain Jan 05 '21 at 07:02
  • that should be your custom logic, which would be tracking all added triangles by keeping their position (e.g. corners points) and when you should add new triangle then calculate place for it (position) basing on currently stored/added triangles - thats clearly maths problem, not coding... – snachmsm Jan 05 '21 at 07:09
  • Okay, got it. But how can I calculate the position of placed view ?? – Mubashar Hussain Jan 05 '21 at 07:14
  • `getX` and `getY` methods will give you position of top left corner of every displayed `View`, also [check out more useful methods](https://stackoverflow.com/questions/30202379/android-views-gettop-getleft-getx-gety-getwidth-getheight-meth) for you. but still I think it should be one `TrainglesView extends View` with overriden `onDraw` method. default layout building in Android doesnt provide not-rectangle `View`s and sooner or later you will have problems with using square `View`s as triangles, which are neighbors with shared edges/corners – snachmsm Jan 05 '21 at 07:20
  • Actually it is a ```drawable```, it's not ```view```. I've to place more drawables in left and right of it – Mubashar Hussain Jan 05 '21 at 07:22
  • ok, how do you show `drawable` on screen if you are not setting it to a `View` (e.g. `ImageView`)? you can draw multiple `drawable`s (and other: lines, shapes, text etc.) with custom methods of `Canvas` and `Paint` classes inside `onDraw` method. for simple traingles you won't even need drawables, all can be drawn programmatically, you still must track position/corners of all added triangles – snachmsm Jan 05 '21 at 07:34
0

This kind of design typically wouldn't be a layout. You'd use a single custom view and do the drawing yourself.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127