Android Studio Center Text – Simple Steps to Set the Right Alignment

Sometimes, you can see Android apps with messy text displays. You can align the texts to a certain fashion, but it’s a bit challenging. Fortunately, Android Studio center text in a way that is very convenient for developers. There are different ways to align text —center, vertical, horizontal, center_vertical, center_horizontal, and so much more. It’s very easy to figure out if you only want to display all texts to the right, for example. But what if you want to display some texts at the center — center vertically, center horizontally, or center vertically and horizontally — that’s where the fun begins.

First, know your design and from there, learn about the layouts. This is essential so you can appreciate how a one-line code can make all the difference. Depending on your application and how you want the text to appear, you can use either of these layouts — relative, linear, and constrained layout.

Android Studio Layouts

Depending on your design, either of the layouts is perfect. If you have a simple design, you can use linear or relative layout. But, if you have a complex design, you might need to use the constrained layout. Layouts affect the UI performance of your Android app. High-level layouts are called parents. The nested layouts inside it are called children. The more complicated your layout is, the heavier your app will be — the slower the app. So, which layout is perfect?

As mentioned, they all are, depending on your application and each has its benefit.

Linear Layout

This type of layout aligns all elements in a single direction — from parent to child. The simplest among the types of layout available in Android Studio. Whether vertically or horizontally, you can simply define the orientation. Set the LinearLayout attribute android:orientation to vertical or horizontal. This will put every child in a single orientation, either vertically or horizontally.

If you want to align all views at the center (either vertical center or horizontal center) use android:gravity and set it to center. Write it next to the android:orientation. Actually, if you have several nested LinearLayout, it’s recommended to use RelativeLayout to flatten the layout hierarchy for more optimal performance.

Relative Layout

With this layout, you’ll get more variety with your text layouts and other elements. You’ll have the option to use LayoutParam for each child. They can either be relative to their parent or the other children. Depending on what you want to do, the following attributes should be set as true to center text relative to certain elements.

android:layout_centerHorizontal— centers the child horizontally relative to its parent

android:layout_centerInParent — centers the child horizontally and vertically within its parent

android:layout_centerVertical— centers the child vertically relative to its parent

Constrained Layout

This is the most flexible way to position texts and other elements for your Android app. It has become the default layout of Android Studio because you can easily and simply create layouts even for complex designs. There are different types of constraints and one of them is centering position and bias. This is very useful if you need to deal with “impossible” constrains.

You can put the elements to be centered in the parent container or you can tweak the positioning more concretely with bias attributes. With either of the following bias attributes, you are more flexible to position elements in a way that is more versatile to adapt to screen size changes.

  • layout_constraintHorizontal_bias
  • layout_constraintVertical_bias

Android Studio Center Text

With the explanation of each layout above, you may find it easier to center text on Android Studio. However, if you still find it challenging to center texts, there are still a few attributes that you can use. Remember, the layout depends on the complexity of your design. And, each of these layouts has unique attributes so you can center texts easily.


This seems to be the very basic attribute if you want to align your text to the center. It’s a member of the View class which is, of course, to define the alignment of the text. It receives other values, but for this article, we use “center.”

android:gravity="center" Instead of android:layout_gravity="center"

Gravity comes to your rescue when the textAlignment falls short. When textAlignment has no effect, gravity should work. It’s a member of the TextView class, which has more text-aligning features and more versatile.

Perhaps, you have read some answers to use android:layout_gravity="center". This also works, but for the entire layout. If you want to align texts (and other subviews) you need to use the android:gravity="center". So, for this article, the latter is recommended.

Usingandroid:gravity="center", but Not Text Not Centered?

The “center” value is just one of the constants you can use, which positioned the texts/objects at the center vertically and horizontally. Yet, there are still other options —center_horizontal and center_vertical. The former places objects in the horizontal center, while the latter at the vertical center.

The Final Say

Android app development isn’t easy, but it’s fun. With Android Studio, center texts can be done in different fashions — could be centered vertically, horizontally, or both. The good news is, positioning your texts is made easier if you know what layout to use. Some layouts have specific attributes to make centering texts quicker.

Even so, you can use textAlignment and android:gravity to position texts (and other objects) in a way you need them to be. Simply add this to your project — whether you need the parent or child — and set the value to “center”. Still, not getting the texts at the center? Use android:gravity and use appropriate constants. Happy coding!