I have a custom view consisting of imageview and textview. Lets call it CustomViewA. I would like to add several instances of this view to another custom view (CustomViewB) in random positions like so:
How can I say in CustomViewB, something like canvas.drawLayout(new CustomViewA(text, imageBackground), x,y)??? I.e. draw CustomViewA in specific (x,y) position inside CustomViewB?And then manipulate this CustomViewA like I would any Shape on touching,dragging, etc? If I would make CustomViewB custom Layout how can I overwrite onLayout(), onMeasure(), taking into account that CustomViewA-s don't relate to each other in any way?
Related
I can create this by using XML. But here my requirement is if I set attribute show_stepCount=3, then 3 views should be drawn on the canvas. I should be able to select one item at a time. I am not able to understand should I inherit my custom class by View or with some other view type to achieve the below UI.
I have a .xml file(picture below). In my .xml, the second row's views create while running.
I want my new created views get the same properties from the existing views, like textsize, maxlenght, text-alighn , etc
I tried to change the things that I set for first row views in xml, but some of my codes don't work. for example, this line:
b2.setTextSize(b.getTextSize());
doesn't change the TextSize of the second Button.
what can I do?
enter image description here
Edit:
Is there a function that could be able to get the whole attributes together and set them for new views?
Suppose that I create an XML for an image, can the same id be used for creating multiple instances of the image in the screen?
That is, suppose I want to create 10 balls on the screen, is it enough that I define the XML for one ball and then can it be used repetitively for creation of 10 balls?
You can inflate the same view as many times as you want. The only problem is that all views have same id and if they belong to the same parent view then you can only access the first view using findViewById() inside your code. You might want to use ListView in that case.
I have defined two views ExampleView1, ExampleView2, ExampleView3 and ExampleView4 in resources.
In my Activity I have an empty ViewFlipper. Based on doing some logic I want to add either ExampleView1 and ExampleView2 to the ViewFlipper and show the view.
Later I want to add based on internal logic either ExampleView3 and ExampleView4.
How do I do this? Is there some tutorial or can someone help me with example code?
Just use the addView method, which ViewFlipper inherits from ViewGroup. If your views are custom ones, you will have something like this:
flipper.addView(new ExampleView1());
On the other hand, if the views are defined inside an XML layout, you will have to inflate them first:
View view = LayoutInflater.from(context).inflate(R.layout.your_view, null);
flipper.addView(view);
I have to dynamically add a list of views (the views use RelativeLayout). Can I do this by specifying the view definition in xml and instantiate multiple objects off it? This gives the usual benefits of separating the view part from the code (for e.g., making it easy for the UI guys to alter things) or Is going the ArrayAdapter the suggested/only route?
are you saying that you want to do this?
View v1 = (View) findViewById(R.id.someView);
View v2 = (View) findViewById(R.id.someView);
If you do this, you will merely have 2 references to the same view; it does not create two separate View objects. However, if you want to make a vertical list of views, look into ListActivity. in this case you will make a layout xml that will be used for every item in the list. you will need to implement a ListAdapter, or use a SimpleArrayAdapter.
does that help?