Android and reordering the children of a ViewGroup - android

How do you exchange the indices of two children in a ViewGroup?
How do I use setLeft on a child view? It doesn't seem to be defined for objects of class View.
Edit.
Answer to #2 is that setLeft is only available from API11. Ditto setX.

You can remove all children and than add by required order. I'm not found another way.
IMHO the best way for reorganize layout is use RelativeLayout.
As I do this:
// prepare rules
lpTopLeft = new RelativeLayout.LayoutParams(minDimension/5, minDimension/5);
lpTopLeft.setMargins(minDimension/50, minDimension/50, minDimension/50, minDimension/50);
lpTopLeft.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lpTopLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
And now use prepared settings:
// rearrange child
bnReset.setLayoutParams(lpTopLeft);

I guess removing the views and re-inflating the layout is the only solution. Maybe you can try to use a ViewFlipper. I don't know if it's suitable for your case.

best solution:
flexbox-layout by Google
FexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.
Then you can set
layout_order
This attribute can change how the ordering of the children views are laid out. By default, children are displayed and laid out in the same order as they appear in the layout XML. If not specified, 1 is set as a default value.

Related

Hide a view when a dependent view is hidden in ConstraintLayout

Is it possible in ContraintLayout to hide/show a view when a dependent view is gone/visible?
Obviously it's possible by using CoordinatorLayout with a custom behavior or by using an wrapped layout, however the mentioned approaches involve additional layouts. I would like to see such an approach that doesn't introduce additional layouts
You should be able to group the views together using the new "group" feature of ConstraintLayout 1.1.x. See this posting on Medium.
Groups, like the Guidelines, are widgets with size 0. But Group helps to apply some action to a set of widgets. The most common case, is to control a visibility of a collection of widgets.
You can do this programmatically.
When you set the vivisiblity of a compounant in your code, change the visibility value of the dependent view.

Need suggestions for dynamically creating views in android

I am going to start one app where my activity page will contain "n" grouped views. Grouped view means "collections of views (i.e. One group can have TextView+Button+ImageView)". So the page will have "n" number of such grouped views.
I need suggestions like what would be the best practice to implement this. I could think of below ones:
1) Should a ScrollView be used (Then I will have to create groups in runtime and place one under another)?
2) Or a ListView be used (Then how can I accommodate the height of each row as grouped views height may differ from each other?)
Or is there any other way I can go along with?
Appreciate the suggestions and any sample examples if have. Advance Thanks.
Both options would work, it really depends on your use case.
Place a vertical LinearLayout inside of a ScrollView and add your grouped-views to the LinearLayout. I would recommend this if you have a relatively small number of such views (not necessarily a fixed number, but small enough that you wouldn't have to scroll many "pages" to see them all). Make sure the ScrollView has android:layout_height="match_parent" and the LinearLayout has android:layout_height="wrap_content".
If the number of grouped-views is not small, you could use a ListView and make an Adapter for it. This lets you take advantage of ListView's automatic view recycling when items get scrolled off screen.
For either case, make an XML file for just the grouped-views. In code, you would get a LayoutInflater object (usually by calling Activity.getLayoutInflater()) and call inflate(R.layout.your_grouped_views, null). If using the LinearLayout, you would add it in code with one of the LinearLayout.addView(..) methods; if using the ListView, your adapter would return the whole thing from getView(...).
create one xml layout containing the constant elements of your group view.
in you main xml layout which will be the contentView of your application, put a ScrollView and a single LinearLayout.
then in the program inflate as many views of your group view as you want.
For your answer i want to give you referance of this website, on this website you can learn create dynamic view in android...

How to place UI elements to the position we want in Android?

I have some buttons, textboxes etc. in my android application, but when i drag them with my mouse in the xml file, their place doesn't change, or changes but they are not placed where i exactly wanted. How can i adjust their positions in the screen?
Thanks
Unfortunatly there is no such thing as absolute positionning in android ( RIP AbsoluteLayout deprecated since years.)
instead you have to position views according to their parents and according to other views in the same parent.
first you have to define wich parent you need ( if you want some viens in a single line go for a LinearLayout. a more custom layout: use a RelativeLayout ...)
then you can drag and drop views inside, but they will always snap a position relative to their parent and/or relative to the other views.
you can of course play with margins.
A list of layout type with some advanced techniques can be found on this page
Hope that helps.
You RelativeLayout as a group layout for your layout so positioning can somewhat easy using mouse.
Best is to arrange them from the xml code. Just Learn about using the Relative layout, LinearLayout and TableLayout
Learn how the XML works. For a LinearLayout, the items come in the order listed. For a RelativeLayout, the items are related by the values of their layout_XXX properties. Then you don't have to worry about the WYSIWYG tool not working.
FYI, the tool bundled with eclipse is extremely buggy. Don't count on whats on there being what's on your phone for anything non-trivial.
Like the others wrote it is easier to edit layout using xml editor. You can read more here http://developer.android.com/guide/topics/ui/declaring-layout.html.

Which layout should I use so my app looks like Google Plus?

I'd like to make a layout similar to the one used in the current version of Google Plus:
I don't know which layout I must use because :
- I can't use a GridView because it doesn't support spanning row.
- I can't use a GridLayout nor LinearLayout because the number of elements to show is dynamic
- I can't use a ScrollView because I want an AdapterView to re-use child elements
- I can use a ListView with two differents cells types but I can't use the built-in OnItemClickListener.
Do you have better ideas than me?
Thank you very much :)
this post suggested that it uses StaggeredGridView . I personally haven't tried it.
You can very well use RelativeLayout for this.
With a single Relative Layout you should be able to create exactly same layout.
Additionally, your content can be dynamically added to Relative Layout pretty easily (by setting proper LayoutParams to the content to be added).

RelativeLayout: Many options if elements are GONE

Isn't there a way to tell declaratively an element, position relative to many elements - if one is GONE then use the other? Of course if all elements are not GONE take only one (e.g. the first one).
A very simple solution is to use a LinearLayout inside of your RelativeLayout. So you can place elements inside the LinearLayout which then align to the previous element in the LinearLayout - either horizontally or vertically.
The framework itself does not support the feature to position an element relative to the visibility of many other elements.
The only thing I see even remotely related to that is android:layout_alignWithParentIfMissing and that says:
If set to true, the parent will be used as the anchor when the
anchor cannot be be found for layout_toLeftOf, layout_toRightOf,
etc. [boolean]
So I believe to get the functionality you want you would have to code it in yourself.

Categories

Resources