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

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);

Related

Move android components in a LinearLayout

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

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.

Why can't I set the gravity for my view

I want to set the gravity of a view (in my .java file) but for some reason it can't be done this time (it states that there is no such method). But normally setGravity(Gravity.CENTER); will just be considered valid code.
So why can't it be done all the time? What should I do when I can't set the gravity of a view but I still want to align it to mid( like setGravity(Gravity.CENTER);)?
AScrollView middle = new AScrollView(context);
RelativeLayout.LayoutParams formid =
new RelativeLayout.LayoutParams(width,LayoutParams.WRAP_CONTENT);
middle.setLayoutParams(formid);
middle.setGravity(Gravity.CENTER); //This can't be done..
I can't say exactly why you can't set Gravity on ScrollView but I'm pretty sure you can't. However, if you have a ScrollView then you should have some Layout or content inside of it to scroll. Simply set the Gravity on that Layout and it should give you the effect you want
use this way..First add the view middle then after try to set gravity
formid.addView(middle) ;
middle.setGravity(Gravity.CENTER);
Layout Gravity is not supported by RelativeLayout. It is supported e.g. by LinearLayout where you can set it via the public property gravity of the LinearLayout.LayoutParams class.
try this code.
ScrollView middle = new ScrollView(this);
middle.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.CENTER));
let me know the status.

Remove rule from RelativeLayout

I have bookmark ImageButton with ALIGN_WITH_PARENT set to true in XML.
If I programatically do (I want to remove that rule)
RelativeLayout.LayoutParams params = (LayoutParams) bookmark.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
bookmark.setLayoutParams(params);
The rule just doesn't apply (in my layout I can see that rule is still active) -> layout isn't good. If I manually do it (some sort of "simulation", set ALIGN WITH PARENT to false) in my Layout Editor, my layout is fine which leads to this code up there. Something is wrong.
What?
(I believe temporary) Solution is to wrap up that ImageButton in one dummy RelativeLayout and then take the params of that dummy layout and add or remove rules. That way it's working just fine.
Insead of calling setLayoutParams(), try to use requestLayout() method:
bookmark.requestLayout();
From API docs:
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
simply call removeRule() function on params.
for example,
params.removeRule(RelativeLayout.CENTER_IN_PARENT);

Setting Textview's Attributes Programmatically

I have created 5 TextView programmatically, now i want to set few Parameters/Attributes of their such as Gravity, Layout_Gravity, etc.
I know we can set it in XML layout at Design-time:
android:gravity="center|center_horizontal|center_vertical"
android:layout_gravity="center|center_horizontal|center_vertical"
But, How can we set Gravity/Layout_Gravity kinds of Attributes programmatically?
You can set the gravity of TextView programmatically using setGravity(int))
Probably you can set layout_gravity like this(I've not yet tested this) :
TextView can let its parent know about layout preferences using
setLayoutParams(ViewGroup.LayoutParams params)
LayoutParams params=new LayoutParams(this, attrSet);
tv.setLayoutParams(params);
It's all in the docs. Look at the documentation for View, for example, under "XML Attributes". You'll see all XML attributes, and the corresponding method you'll need to call in code to get the same effect.

Categories

Resources