Framelayout layout_width on extending - android

I am creating a custom view which extends RelativeLayout. I inflate a layout which I created using xml and contains a single element. Afterwards I add a non fixed amount of other child views to the layout in an init method called from the constructor. I need to have access to the layout_width parameter from the MainActivity xml file, as I arrange the children views in a circle with an offset which should differ according to the width given on the MainActivity xml.
I can't seem to be able to get the width given on the XML via parameter layout_width.
OnMeasure seems to get it but it is called later.
In simpler words, I want to place the children views differently depending on the width of the view.

Related

What is the point of a FrameLayout for single element

I understand the advantage of a FrameLayout when you need to add multiple children to it. But what is the point of adding a FrameLayout as your root element when you only have one child (lets say a TextView)? I mean why wouldn't you simply set that single child (e.g. the TextView) as the root of your layout?
A FrameLayout is - as the name implies - a Layout. It inherits from ViewGroup which is used for positioning and aligning child views.
Of course you would be able to set a TextView alone in your Layout xml. But what are you going to do if you want it to be centered?
Then it would make sense to use a FrameLayout as a sort of "Wrapper" around your TextView which lets you position it the way you want.

How to sort LinearLayout's children programatically

I have Linear Layout with horizontal orientation which has few ImageViews in it (hardcoded in the xml). I have to sort them based on MultiselectListPreference (all the checked first and unchecked later). To put checked ones in the beginning I tried
((LinearLayout)imageView.getParent()).removeView(imageView);
((LinearLayout)imageView.getParent()).addView(imageView,0);
And did nothing with unchecked. But it gives me the "The specified child already exists. You must call removeView(view) on view's given parent" though I've already called it. This may be because all those ImageViews are present in xml. How can I obtain the desired arrangement of child ImageViews. If at all LinearLayout does not suffice, should I use RecyclerView here , would it be heavier?
Cast the parent view to ViewGroup so the removeView method will work.
((ViewGroup)imageView.getParent()).removeView(imageView);

At what point are child views attached to the parent view

I created a custom layout which can have an arbitrary amount of child views.
I figured out that the child views are not available when I try to access them in the constructor of my layout view.
So what I currently do is to access them in onMeassure, but it seems to be a bad idea, since this gets called several times.
What is the best place in my layout to init child views? I wish there was a method like onChildViewsAttached(). Any ideas?
Child views are attached to the parent once layout pass is finished, i.e layout() of the view group is finished.
You can also register OnGlobalLayoutListener or OnPredrawListeneron the ViewTreeObserver of your custom layout.

Dynamically add a view in a custom layout after know layout's size

I want to dynamically add a view in a custom layout (extends RelativeLayout). But the subview must be placed with specific coordinate and size. So I initialize and use addView into the method onMeasure (same problem with onLayout) of my custom layout, like this I can know the size of the layout.
But sometimes (after changing device’s orientation for example) the method onMeasure (or onLayout) are called multiple times (often twice). So the subviews are added many times.
Does onMeasure is the right place to dynamically add subviews ?

Can I use more than one list view in resource layout file?

Can I use more than one ListView in a layout, because my list adapter is considering the ListView with id "list", how to set the adapters for the remaining list views?
You can't do it with a ListActivity. You can do it with your own Activity subclass, but you have to be a little careful about defining the heights for vertically stacked ListViews. One way to do it is to put them in a LinearLayout, set the layout_height of each to 0px, and set the layout_weight of them to non-zero numbers in proportion to how much space you want each to take up. (For equal-height lists, for example, set the layout_weight of each to 1.)

Categories

Resources