Move android components in a LinearLayout - android

I have an Activity which has an EditText programmatically added to a LinearLayout inside a ScrollView.
ScrollView <- LinearLayout <- EditText(s)
Is possible to rearrange their position by seting X and Y axis or something as Swing does for Components?
This is my code:
for (Field classField : todoFields) {
CustomEditText field = new CustomEditText(this);
field.setName(classField.getName());
layoutFieldWrapper.addView(field);
}
EDIT:
Can I freely move components around the interface, for instance: Put component next to another by setting the same Y and different X or overlap any of them to other, ecc.. does it's possible?

If I get you right then you can call the method with index parameter like this:
layoutFieldWrapper.addView(field, index);
Hope this is the right suggestion or hint.

You can use RelativeLayout. Replace it with your LinearLayout inside your ScrollView. This layout gives you much accessibility. You can move your views in your user interface freely using this type of layout. You will have to play around with some attributes and try like: gravity, layout_centerVertical, padding, layout_margin, layout_width, layout_height, layout_below, layout_toRightOf, and a lot more...
Reference Link: http://developer.android.com/guide/topics/ui/layout/relative.html

Related

The better way of dealing with RelativeLayout

I would like to know which is more profficient way of placing children in RelativeLayout. There are two approaches of doing this:
1) Place the main view with absolute position (like layout_centerInParent or set margins/paddings correspondent to parent view) After that you add other views and set them attributes like android:layout_above="#id/relative_view_id" and place them below relative view. It is not good way because your views hierarchy in xml does not match to what you see in preview.
2) You assign to children of RelativeLayout attributes with absolute id android:layout_above="#+id/relative_view_id" (+ appeared). It provides the correct views order in xml. BUT when you looking for declaration of view with relative_view_id from java code (by pressing cmd+B) Android Studio suggests all the views where you declared #+id. In our case View with attribute android:layout_above="#+id/relative_view_id" will also appear in search results.
What is your way of placing Views in RelativeLayout?
android:layout_above
Positions the bottom edge of this view above the given anchor view ID.
Accommodates bottom margin of this view and top margin of anchor view.
For your question I would prefer No 1 way .
android:layout_above="#id/relative_view_id"
Its refer the already generated id (relative_view_id) .

How to make a zig-zag layout?

I want to create zig-zag layout same as following attached image:
I tried a lot by creating diagonal lines and arranging them with icon but couldn't make it same.
I implemented diagonal lines with the help of accepted answer from following questions:
Diagonal line across view
How rotate line in Android XML?
However I'm stuck to arrange lines with icons exactly same as in image.
I created this custom ZigZagLayout.java file to cater your requirement. You just have to update the package name in the 1st line.
It basically extends RelativeLayout, so you can use it in your layout-xmls just like any other ViewGroup class. Once you have instantiated this layout, just add child-views to it like it is done for RelativeLayout via addView(View child).
Example code snippet with dynamically created view:
ZigZagLayout zigZagLayout = (ZigZagLayout) findViewById(R.id.layout_zigzag);
Button btn = new Button(this);
btn.setText("Test Button");
btn.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
zigZagLayout.addView(btn);
I've also added few interfaces to this ZigZagLayout for your easy interaction like ability to set the connector-line stroke width, visibility, color, margins, etc.
Try it out and let me know if it suffices your requirement. Cheers.
If you have layout for each circular item , you may use relative layout to align them, using align_below, align_left with margin, align_right with margin tags.
Please provide further detail, what are the lines connecting them and exactly what all are requirements for UI and functionality.

When a layout is nested inside another layout, does it inherit the params of the parent?

Ok so imagine a situation where you have the main layout as LinearLayout (which supports layout_weight) and inside it you have nested RelativeLayout (which does not support layout_weight).
Now since the RelativeLayout is nested inside LinearLayout, will it be able to use layout_weight ? And if the case was reversed (Linear inside Relative), will LinearLayout be able to use layout_below, layout_toParentLeft, etc ?
Now since the RelativeLayout is nested inside LinearLayout, will it be able to use layout_weight ?
No. Since RelativeLayout is nested inside LinearLayout it can be given a weight to be weighted inside the LinearLayout but it does not inherit the property of weightSum to hand out to children.
And if the case was reversed (Linear inside Relative), will LinearLayout be able to use layout_below, layout_toParentLeft, etc ?
Yes, you are inside of a RelativeLayout so any child can be given those properties. However, any child inside of that nested LinearLayout can not use the properties of RelativeLayout as in your example.
WeighSum Docs
a number greater than 0.0f, or a number lower than or equals to 0.0f
if the weight sum should be computed from the children's
layout_weight"
android:weightSUme="aNumber" can be used to "sum" the weight of the children. If all the childrens sums will add up to 1 then you don't need this property. As I recently learned from #RomainGuy through a discussion with #Squonk, it is really only needed if they won't equal 1 and you want some empty space in your layout. Otherwise, the cpu will determine what the weightSum will be.
The answer to your question is not very complicated. I can synthesize it in two letters: NO

Imageview floating left

How can I create ImageView floating to the left of the textView, something like html:
<div>
<img src="src" style="float:left"> Text here.....
</div>
You have two options:
With LinearLayout, setting the orientation to horizontal so the image is first and then come the rest.
With RelativeLayout, there you can indicate the position of a element relative to another with the attributes of the style android:layout_toLeftOft, android:layout_toRightOf, android:layout_below or android:layout_alignParentTop, ...
However, it is not so flexible as CSS for some actions and, for example, wrapping text around an image is not so easy to achieve.
There's really no concept of floating elements in Android, but you can easily put an image to the left of some text using the drawableLeft attribute of the TextView. Example:
Otherwise, for more complicated layouts, parent views determine how their children are laid out. For example, instead of the concept of a div which simply wraps it children and uses the float and display attributes of the children to determine how things look, Android has more complex parent views (ViewGroups as they are called, since that's the super class) to control things.
Check out the docs for LinearLayout and RelativeLayout for some examples.
The positioning of views depend on the kind of layout you use. In case if you are using a RelativeLayout and you want to float your view(in your case the ImageView) which is within this Relativelayout, you can use the attribute of the ImageView (android:layout_toLeftOf="") specifying the view id of your textview between the double quote.

Set attributes (margin, gravity, etc...) to an Android view programmatically (without XML)

I need to create a GUI (layout+views) in my .java activity class (I know it's far more flexible and easier to use .xml layout file, but I don't want to use it for now).
I can't find any setGravity() (but a "Gravity" object I can't figure how to use) or any set setMargin() method for the "View" object.
What is the easiest way to do it ?
Thanx.
For setting the margin on component. The following leaves the existing margins as previously set and sets the left margin as zero.
TextView title = ((TextView)findViewById(R.id.default_panel_title))
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)title.getLayoutParams();
lpt.setMargins(0,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);
title.setLayoutParams(lpt);
You can add gravity to the "layouts" not to the "controls". Try to set gravity to any of your Linear/Relative or Frame layouts using setGravity(); .
Eg:
LinearLayout lll = (LinearLayout) findViewById(R.id.layoutname);
lll.setGravity(Gravity.CENTER);

Categories

Resources