Skip to content Skip to sidebar Skip to footer

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-

Solution 1:

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

Solution 2:

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.

Post a Comment for "Setting Textview's Attributes Programmatically"