Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8874

how to set position of textview

I want to set my my text under the center of my view. I know that I need to do it with Gravity but still dont see the right result I just wanted to do Animation that starts from under the center and go left right and return to started position My code is

        setContentView(R.layout.logo);
        mTextView = (TextView) findViewById(R.id.textLabel);
    mLayout = new LinearLayout(this); 
        mLayout.setGravity(Gravity.CENTER); 
        mTextView.setGravity(Gravity.AXIS_X_SHIFT/2); 
        mTextView.setGravity(Gravity.AXIS_Y_SHIFT/2-Gravity.AXIS_Y_SHIFT/3); 
        mAnimation = new TranslateAnimation(100f, -100f, 0.0f, 0.0f); 
        mAnimation.setDuration(2000); 
        mTextView.setAnimation(mAnimation);

        mAnimation.start();

and the xml looks like:

 <ViewFlipper android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:flipInterval="2000"
        android:layout_marginBottom="20dip" >
<LinearLayout
android:id="@+id/Linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/logonews"
android:baselineAligned="false"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Israel News"
    android:textSize="18sp"
    android:textStyle="bold"

     />

  </LinearLayout>
  </ViewFlipper>

thanks for help

Upvotes: 0

Views: 4460

Answers (2)

Longerian
Longerian

Reputation: 723

  • 1st, Gravity attribute has effects on the postion of text content in TextView, not the TextView.
  • 2nd, TranslateAnimation moves straightly only once. it will not move back and forward, so you may need combine several TranslateAnimation to implements what you want.
  • 3rd, user startAnimation.

Upvotes: 0

Jon O
Jon O

Reputation: 6591

When do you want the Animation to play?

What result are you seeing instead?

If you want it to play immediately, use startAnimation instead of setAnimation.

Edit: Looking at your code in light of the comments you made...

If you want your TextView to be below your logo image, you'll need to remove the background drawable from the LinearLayout, change its orientation to vertical, and put the image into an ImageView instead, before your TextView in the LinearLayout. This allows Android to lay them out in order...

Like this:

 <ViewFlipper android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:flipInterval="2000"
        android:layout_marginBottom="20dip" >
<LinearLayout
android:id="@+id/Linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >

<ImageView
    android:src="@drawable/logonews"
    android:scaleType="fitCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
>


<TextView
    android:id="@+id/textLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Israel News"
    android:textSize="18sp"
    android:textStyle="bold"

     />

  </LinearLayout>
  </ViewFlipper>

or if you need the image to be the background you can create an empty View above the TextView that's the same size as the image.

Upvotes: 1

Related Questions