Setting Textview's Attributes Programmatically

10,215

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);
Share:
10,215
Paresh Mayani
Author by

Paresh Mayani

Mobile Development & Project Management/Delivery Consultant | Co-Founder | Start-up Consultant | Community Organiser @ GDG Ahmedabad | Certified Scrum Master Exploring the horizon of the software industry since 11+ years. Let's connect over twitter: @pareshmayani OR LinkedIn What I do: Co-Founder / CEO @ SolGuruz Founder/Organiser, Google Developers Group, Ahmedabad Blog @ TechnoTalkative Achievements: 11th person to earn Android gold badge 4th person to earn android-layout bronze badge. 20th person in the list of Highest reputation holder from India

Updated on June 05, 2022

Comments

  • Paresh Mayani
    Paresh Mayani almost 2 years

    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?