How to align View vertically to the text inside TextView?

android studio text alignment missing
android textview align center linearlayout
android textview align center programmatically
android center text in textview
android textview center text multiline
android:text alignment justify
android studio center text in textview
textview text alignment android

I'm trying to achieve this result:

What I have right now it is ConstraintLayout which includes three Views: TextView, ImageView, ImageView

Design requirements:

  1. Text centered inside TextView;
  2. TextView has fixed size 80dp
  3. Size of text should be AutoSizeable;
  4. ImageViews should change position horizontally depending on text position inside TextView. See images with case #1/#2 for reference.

Is it possible to achieve item 4 in XML? What about programmatically?

[Additional info]

Autosizing doesn't work properly with width wrap_content, see autosizing documentation:

Note: If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results.

android, So after an investigation found two solutions for this: Calculation of text width and applying a horizontal bias to ImageView s;; Custom text view  If the TextView's height and width are wrap content then the text within the TextView always be centered. But if the TextView's width is match_parent and height is match_parent or wrap_content then you have to write the below code:

Playing around with some set values for sizing that you can adjust to suit your app, I achieved this look using xml alone.

The images a gammy, I cut them from you original image. I've utilised the constraint layout and a relative layout. The text responds to resizing and rotation.

A vertical view

A horizontal view

Small text

Large text

Of course play around with the sizing to suit your needs.

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.yvettecolomb.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="10dp"
        android:padding="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:autoSizeTextType="uniform"
            android:gravity="center"
            android:padding="2dp"
            android:text="TEST"/>

        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="-10dp"
            android:layout_toLeftOf="@+id/tv1"
            android:src="@drawable/a"/>

        <ImageView
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_toRightOf="@+id/tv1"
            android:src="@drawable/b"/>


        <ImageView
            android:id="@+id/image3"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_below="@+id/tv1"
            android:layout_margin="-10dp"
            android:layout_toLeftOf="@+id/tv2"
            android:src="@drawable/c"/>

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:autoSizeTextType="uniform"
            android:gravity="center"
            android:padding="2dp"
            android:text="test test test test test"/>

        <ImageView
            android:id="@+id/image4"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_below="@+id/tv1"
            android:layout_toRightOf="@+id/tv2"
            android:src="@drawable/d"/>
    </RelativeLayout>


</android.support.constraint.ConstraintLayout>

Center align text in TextView Android Programming, While gravity attribute is used to position view's content e.g. “text” in a view or its content e.g. TextView or Button horizontally and vertically? Set text alignment vertically center automatically on button click. Vertical center means to set your textview text into vertically middle of activity screen. Developer can set text alignment using xml file but some times there are special needs so you can also set text alignment using programming file. So here is the complete step by step tutorial for Set TextView Vertically center in android programmatically .

Use AppCompatTextView. Change autoSizeMaxTextSize, autoSizeMinTextSize and autoSizeStepGranularity values.

                    <RelativeLayout
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/rl_flashcard_back"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <android.support.v7.widget.AppCompatTextView
                        android:id="@+id/tv_text"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerInParent="true"
                        android:gravity="center"
                        app:autoSizeMaxTextSize="16sp"
                        app:autoSizeMinTextSize="8sp"
                        app:autoSizeStepGranularity="1sp"
                        app:autoSizeTextType="uniform"
                        />

android.support.v7.widget.AppCompatTextView tv_text = mRootView.findViewById(R.id.tv_text);

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

How to center the elements in ConstraintLayout, How do I center text horizontally and vertically in a TextView of Android? android:text="Text in Center Vertical" /> <TextView device as an option and then check your mobile device which will display your default screen −. Android align textview to right; How to vertically align text in a TextView with Compound Drawable; How to left-align text in a SWT ToolItem? How to align text under picture? [duplicate] Android align textview and edittext; How does a Android TextView breaks the text into lines? Justifying text inside a TextView in android; Android: multiple

Android - Center Textview in LinearLayout, Nov 11, 2016 · Vertical-center text inside grid layout with vertical-align stretch on 2016 · Set TextView Vertically center in android programmatically; Align view  Set TextView text alignment center (middle of screen) inside textview using layout xml. TextView inside text by default starts from left side of textView area bu some times application developer want to start text from middle of area like setting up textview alignment center. So developer can starts text from center using gravity attribute. So here is the complete step by step tutorial for Center textview text horizontally and vertically inside TextView android.

Android, 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. If you want to center text horizontally and vertically in a TextView? Using LinearLayout if you are using textview height and width match_parent You can set the gravity text to center and text alignment to center text horizontal if you are using textview height and width wrap_content then add gravity center attribute in your LinearLayout.

How do I center text horizontally and vertically in a TextView of , However, centering child views inside a RelativeLayout works differently. Instead of For centering a TextView within a ContraintLayout horizontally you could do the following: <TextView android:text="Hello World!" I have a multiline TextView in Android. It is a child of a LinearLayout. It is already centered horizontally and I also want to have the text inside it centered. Here's what I have now: _ _ _ _ _

Comments
  • You answered your own question which is great. It looks like you applied the bounty after answering your question. If that's right, what about your answer are you not satisfied with?
  • My solution works well, but I'm not sure about efficiency. And I wanna get more views and opinions on that. We have such simple thing like layout_constraintBaseline_toBaselineOf to align views horizontally. But what about vertically? Maybe in some future, it will be added :) What is the best way to do it? @Cheticamp