I am trying to set gravity of the text view through java in the following way
if(check.equals("left")){
results.setGravity(Gravity.LEFT);
} else if(check.equals("center")){
results.setGravity(Gravity.CENTER);
} else if(check.equals("right")){
results.setGravity(Gravity.RIGHT);
}
But this does not work correctly.
and in XML file the gravity is pre-set to center.
Here the results is TextView type variable .
this is the parent XML code:
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text" android:textSize="30dp" />
Please help me
And whole Layout XML code is
<EditText
android:id="@+id/etCommands"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type some commands"
android:inputType="textPassword" >
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:onClick="cmdClick"
android:layout_weight="20"
android:id="@+id/bResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Try Commond" />
<ToggleButton
android:id="@+id/tgPassword"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:checked="true"
android:paddingBottom="20dp"
android:text="ToggleButton"
android:onClick="togClick"
/>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text"
android:textSize="30dp"
/>
Use a RelativeLayout, and in your TextView XML set
android:layout_centerVertical="true"android:layout_centerHorizontal="true"Depending on your other code, try doing: