title: Textview Gravity not working properly in android
body: <p>What's wrong with my code, I'm trying to display my TextView named  <code>"invalid"</code>, in different locations (left,right,center), but the gravity (left,right,center) won't work! <img src="https://i.stack.imgur.com/SEEGc.png" alt="enter image description here"></p>
  
  <p><strong>My text.xml is</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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" &gt;
  
      &lt;EditText 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/etext"
          android:hint="@string/comment"
          android:inputType="textPassword"/&gt;
  
      &lt;LinearLayout 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:weightSum="100"&gt;
          &lt;Button
              android:id="@+id/button1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/button" 
              android:layout_weight="25"/&gt;
  
          &lt;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"/&gt;
      &lt;/LinearLayout&gt;
  
      &lt;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" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>My TextPlay.java is</strong></p>
  
  <pre><code>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);
                  }
              }
          });
      }
  }
  </code></pre>