Android Development
TextView
Java
android:gravity
Android UI

How set the androidgravity to TextView from Java side in Android

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Android development, UI design is an integral part of crafting a compelling user experience. One of the common requirements when designing a user interface is controlling the alignment of text within a TextView . The android:gravity attribute plays a significant role here, as it defines the alignment of text within the view's boundaries.

While you can set this attribute directly in XML, there might be cases where you need to adjust the text alignment from Java code at runtime. This article details how to set the android:gravity attribute programmatically, explains its technical considerations, and provides a comprehensive example.

Understanding android:gravity

in TextView

The android:gravity attribute in the TextView class determines how the text should be aligned within its bounds. It's different from layout_gravity , which is used to position the view within its parent layout. The android:gravity can be used to align text both horizontally and vertically.

Here are some common properties you can set for gravity:

  • Horizontal Alignment:
    • Gravity.START : Aligns text to the start of the view. Typically the left for left-to-right languages.
    • Gravity.CENTER_HORIZONTAL : Centers text horizontally within the view.
    • Gravity.END : Aligns text to the end of the view. Typically the right for left-to-right languages.
  • Vertical Alignment:
    • Gravity.TOP : Aligns text to the top of the view.
    • Gravity.CENTER_VERTICAL : Centers text vertically within the view.
    • Gravity.BOTTOM : Aligns text to the bottom of the view.

You can combine these values using the pipe operator (| ) to set both horizontal and vertical text alignment.

Setting Gravity from Java

To set the android:gravity attribute from Java code, you must call the setGravity method on an instance of TextView .

Example:

The following example demonstrates how to programmatically set the text alignment of a TextView .

  • Compatibility: Always consider the minimum API level your app supports. Most gravity constants are available since API level 1, but Gravity.START and Gravity.END require at least API level 14.
  • Performance: As previously mentioned, setting gravity involves recalculating layouts. Avoid frequent changes in cases where performance is critical.
  • Localization: Use gravity constants that adapt to user locales to ensure a consistent experience across different languages and regions.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.