Moving textview through whole screen

BamsBamx picture BamsBamx · May 21, 2012 · Viewed 7.1k times · Source

I have developed an app which shows the time in a full screeen. The hours minutes and seconds are displayed in the middle of the screen. So my objective is, when I long click any of the textviews be able to scrool it through all the screen, and place it where I want...

I tried to find a proper code to apply to my app, and I found a code which makes text to image an then move that image. But the problem is, texts are updating each a second, so I think creating images each second is not a good idea...

Anyone knows a method to be able to move textviews through all the screen??? This is one of my textviews

private TextView txtHour;

txtHour = (TextView)findViewById(R.id.TxtHour);

txtHour.setOnLongClickListener(new OnLongClickListener{
....

I dont know what to add to this.... :( Please HELP!

EDIT: According to first answer, should my code look like this?

         txtHour.setOnLongClickListener(new OnLongClickListener() {
             public void onLongClick(View v){


               public void drag(MotionEvent event, View v)
                {

                    RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();

                    switch(event.getAction())
                    {
                       case MotionEvent.ACTION_MOVE:
                       {
                         params.topMargin = (int)event.getRawY() - (v.getHeight());
                         params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                         v.setLayoutParams(params);
                         break;
                       }
                       case MotionEvent.ACTION_UP:
                       {
                         params.topMargin = (int)event.getRawY() - (v.getHeight());
                         params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                         v.setLayoutParams(params);
                         break;
                       }
                       case MotionEvent.ACTION_DOWN:
                       {
                        v.setLayoutParams(params);
                        break;
                       }
                    }

           }});

EDIT 2: finnaly this is result code, is this ok?

 package com.iamaner.T2Speech;
 //imports
 public class MAINActivity extends Activity{

 private TextView txtHour;
 private TextView txtMinutes;
 private TextView txtSeconds;

 @Override
 public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txtHour = (TextView)findViewById(R.id.TxtHour);
txtMinutes = (TextView)findViewById(R.id.TxtMinute);
txtSeconds = (TextView)findViewById(R.id.TxtSeconds);

txtHour.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        drag(event, v);
        return false;
    }});
 }
 public void drag(MotionEvent event, View v)
{

    FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) v.getLayoutParams();

    switch(event.getAction())
    {
       case MotionEvent.ACTION_MOVE:
       {params.topMargin = (int)event.getRawY() - (v.getHeight());
        params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
        v.setLayoutParams(params);
        break;}
       case MotionEvent.ACTION_UP:
       {params.topMargin = (int)event.getRawY() - (v.getHeight());
        params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
        v.setLayoutParams(params);
        break;}
       case MotionEvent.ACTION_DOWN:
       {v.setLayoutParams(params);
        break;}
       }}
       }

And this framelayout

      <?xml version="1.0" encoding="utf-8"?>
  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/your_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"   >

<TextView
    android:id="@+id/TxtHour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15mm"
    android:textColor="#000000"/>

<TextView
    android:id="@+id/TxtPoints1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/separator"
    android:textSize="15mm"
    android:textColor="#000000"
    android:layout_gravity="center"
    android:gravity="center"/>

<TextView
    android:id="@+id/TxtMinute"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15mm" 
    android:textColor="#000000"
    android:layout_gravity="center"
    android:gravity="center" />

 <TextView
    android:id="@+id/TxtPoints2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/separator"
    android:textSize="15mm"
    android:textColor="#000000"
    android:layout_gravity="center"
    android:gravity="center"/>

 <TextView
    android:id="@+id/TxtSeconds"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15mm"
    android:textColor="#000000"
    android:layout_gravity="bottom|right" />


  </FrameLayout>

Answer

Vishnu Mohan G picture Vishnu Mohan G · May 22, 2012

Add this to your onCreate: Better use FrameLayout itself.

 txtHour.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            drag(event, v);
            return false;
        }
    });

And this to your Activity Class ouutside any methods.

       public void drag(MotionEvent event, View v)
        {

            RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();

            switch(event.getAction())
            {
               case MotionEvent.ACTION_MOVE:
               {
                 params.topMargin = (int)event.getRawY() - (v.getHeight());
                 params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                 v.setLayoutParams(params);
                 break;
               }
               case MotionEvent.ACTION_UP:
               {
                 params.topMargin = (int)event.getRawY() - (v.getHeight());
                 params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                 v.setLayoutParams(params);
                 break;
               }
               case MotionEvent.ACTION_DOWN:
               {
                v.setLayoutParams(params);
                break;
               }
            }