Textview Gravity not working properly in android
Asked Answered
D

6

12

What's wrong with my code, I'm trying to display my TextView named "invalid", in different locations (left,right,center), but the gravity (left,right,center) won't work! enter image description here

My text.xml is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etext"
        android:hint="@string/comment"
        android:inputType="textPassword"/>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button" 
            android:layout_weight="25"/>

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:text="ToggleButton" 
            android:layout_weight="75"
            android:checked="true"
            android:paddingLeft="15dp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/invalid"
        android:layout_gravity="center"
        android:gravity="center" />
</LinearLayout>

My TextPlay.java is

public class TextPlay extends Activity {
    Button button;
    ToggleButton tbutton;
    TextView tview;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);
        button = (Button) findViewById(R.id.button1);
        tbutton = (ToggleButton) findViewById(R.id.toggleButton1);
        tview = (TextView) findViewById(R.id.textView1);
        et = (EditText) findViewById(R.id.etext);
        tbutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (tbutton.isChecked()) {
                    et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String input = et.getText().toString();
                System.out.println(input);
                if (input.contentEquals("left")) {
                    tview.setGravity(Gravity.LEFT);
                } else if (input.contentEquals("right")) {
                    System.out.println("inside right");
                    tview.setGravity(Gravity.RIGHT);
                } else if (input.contentEquals("right")) {
                    tview.setGravity(Gravity.CENTER);
                }
            }
        });
    }
}
Dews answered 26/9, 2013 at 10:7 Comment(4)
remove android:layout_gravity="center" android:gravity="center" from your layout for @+id/textView1 and try...Empanel
Change the TextView's width to match_parent or use android:layout_gravity in stead.Consubstantiate
Take a look at this questions as well: linkGyatt
Why should you want to use tabs in code they are a waste of code size...Nyeman
F
33

You set this text view a width of "wrap_content" it means, what ever the text is, the view take the size of the text.

and in the LinearLayout , the default gravity (used here) is 'center'

you should try this :

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"   <= change this
android:layout_height="wrap_content"
android:text="@string/invalid"
android:gravity="center"   <= then this gravity will be taken into account
/>
Fletcher answered 26/9, 2013 at 10:10 Comment(0)
S
30

99% of the time, not working properly == not used properly.

You are mistaking gravity and layout_gravity.

gravity is the way the text will align itself in the TextView. The TextView being in wrap_content this does nothing, as the TextView is exactly the size of the text.

layout_gravity is the way the TextView will align itself in its parent, in your case in the vertical LinearLayout

Steep answered 26/9, 2013 at 10:12 Comment(1)
@Harmen gravity is an attribute defined only for TextView.Steep
S
3

You have given your TextView width wrap_content, and that is the problem, Check below code and replace it to your code.

<TextView
android:id="@+id/txtInvalid"
android:layout_width="match_parent"   
android:layout_height="wrap_content"
android:text="@string/invalid"
android:gravity="center"   
/>
Siana answered 26/9, 2013 at 10:17 Comment(0)
C
1

One reason why gravity does not work properly can be if you have gravity and textAlignment

If you put it like this

    <TextView                                                                     
        android:id="@+id/textView1"                      
        android:layout_width="200dp"                                              
        android:layout_height="wrap_content"                 
        android:gravity="center|center_horizontal"                                
        android:textAlignment="viewStart"                                         

Android studio editor will give this warning:

Inconsistent alignment specification between textAlignment and gravity attributes: was center|center_horizontal, expected start (Incompatible direction here)

But if you add textAlignment inside the styles then it doesn't complain.

This can be the issue why your gravity value will not work

    <TextView                                                                     
        android:id="@+id/textView2"                                          
        style="@style/Widget.App.TextView.Label.Default"                          
        android:layout_width="200dp"                                              
        android:layout_height="wrap_content"                 
        android:gravity="center|center_horizontal"                                       

Check this answer for the difference between them:

Text/Layout Alignment in Android (textAlignment, gravity)

Commemorative answered 12/10, 2022 at 15:12 Comment(0)
B
0

set android:layout_width="fill_parent" for textView1

Bulwark answered 26/9, 2013 at 10:21 Comment(0)
S
0

Make sure you don't have something like that hanging around

app:layout_box="left|top"
Salangi answered 29/5, 2016 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.