0

I'm new to Android Studio. And I want to align a TextView to the center of the activity and use margins. I have tried all the layouts available but none worked.

This is what I get like a preview in the Design tab of Android Studio.

picture

And that is what I get when testing on my phone.

image

My code is:

<FrameLayout 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">

<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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Welcome to my app"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

If I change something in the code it shows the text at the max left and it shows only half of the text. If I try to align it to the right it shows like a white block that blocks viewing the text. I just want to align it horizontally so I can add other items. But if a textview cannot be placed like I want, how can adjust other items?

If someone has a good solution for that, please share it so I can adjust my code.

And thanks in advance.

2
  • only remove your frame layout in your xml. Commented Apr 3, 2018 at 10:16
  • Try changing android:layout_width of TextView to match_parent Commented Apr 3, 2018 at 10:31

5 Answers 5

1

Remove FrameLayout and make ConstraintLayout as root view it will work

try this

<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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Welcome to my app"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

RESULT

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I wanted to create a navigation drawer app so I watched this video youtube.com/watch?v=ju837bQOBfg and had to add FrameLayout
@Unknown check this it will help you androidhive.info/tag/navigation-drawer
1

try this and let me know if its work.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Welcome to my app"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

2 Comments

you want to display it in Center or something else.
yeah I want to center it horizontally
0

Though your code seems perfect

1) Remove Left and Right Margin from TextView

if still remains same

2) Remove FrameLayout

Comments

0
<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">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="30dp"
    android:gravity="center"
    android:text="Welcome to my app"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Try this code to see if it works.

3 Comments

Do you want to center the textView in whole screen or only horizontally?
Only horizontally because I want to add more elements and center them too. And they will be below each other.
Okay, then remove app:layout_constraintBottom_toBottomOf="parent" this line from the above code and also change android:layout_marginBottom="30dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" this lines to android:layout_marginBottom="8dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="8dp"
0

you can try this :

<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"
        tools:layout_editor_absoluteY="81dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Welcome to my app"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold"
            android:padding="20dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
    </android.support.constraint.ConstraintLayout>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.