Setting Textview's Attributes Programmatically - android

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.

Related

What LayoutParams do or pass in Android?

I read often something like this:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
What exactly LayoutParams do?
I've read the Documentary but I wasn't smarter after reading!
Hope someone can explain me what LayoutParams do or pass!
Kind Regards!
LayoutParams are the Java Object representation of all the params you give to your View in the .xml layout file, like layout_width, layout_height and so on. Getting this object from a View allows you to look up those params on runtime, but also to change them in your Java code, when you need to move the View, change it's size etc.
LayoutParams are used by views to tell their parents how they want to be laid out.
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content.
That's all folks.
LayoutParams is use for the dynamically change the layout width and height. and also use the create custom view without the xml by using the directly by use of the LayoutParams for Relative or Linear type layout.
Basically when you set an xml with 'match_parent' or anything like layout_something
the android inflator will set the layout param for the child with the appropriate Layout params with the type matching the parent control, you could also do this in code and if you forget or set the wrong type you will get an exception in runtime.
the parent control needs this information to layout the child control correctly and to his liking.
Please see the following: Android Developer site - Layout Params
I think this picture says it all

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

Set default gravity for children

I have a LinearLayout which contains about 10 TextViews. I want the text of each TextView to be right justified. For now I have added android:gravity="right" for each of the TextViews, but I would like to have a better way out.
I would like to specify the alignment (or even style) of the all the children in the parent itself, so that all the children in the layout have the same style (instead of specifying the style separately for each children). Is that possible?
See the below article. You can declare styles in order to save yourself from headache
http://developer.android.com/guide/topics/ui/themes.html
Use a LinearLayout and specify: android:layout_gravity="right"
More info here: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity

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