To align a text in a TextView in android,
Layout xml file
Add android:gravity="center_horizontal" to the TextView to make the text center aligned horizontally. For center vertical alignment use android:gravity="center_vertical"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:textSize="22sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/my_text" />
</LinearLayout>
In Java Code (Programmatically)
You can also achieve the same in java code,
myText.setGravity(Gravity.CENTER);
Gravity.CENTER: Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
myText.setGravity(Gravity.CENTER_HORIZONTAL);
Gravity.CENTER_HORIZONTAL: Place the object in the horizontal center of its container, not changing its size.
myText.setGravity(Gravity.CENTER_VERTICAL);
Gravity.CENTER_VERTICAL: Place the object in the vertical center of its container, not changing its size.
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
- Display full website address in Safari macOS Browser - MacOS
- How to clear mysql console screen - MySQL
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- Disable Fading Edges Scroll Effect Android Views - Android
- The default interactive shell is now zsh. [macOS] - MacOS
- Fibonacci series from 1 to 500 table - Html
- Microsoft Teams adds New Conversation Button - Teams
- reCaptcha Verification expired. Check the checkbox again - Html