2

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>
BamsBamx
  • 4,139
  • 4
  • 38
  • 63
  • Check this similar question [Drag & Drop TextView](http://stackoverflow.com/questions/8355798/drag-and-drop-textview) – Imran Rana May 21 '12 at 20:27
  • That works better for controlling, https://stackoverflow.com/a/31094315/10751265 – necip Mar 13 '22 at 13:41

2 Answers2

4

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;
               }
            }
Vishnu Mohan G
  • 622
  • 1
  • 7
  • 14
  • I removed android:gravity and android android:layout_gravity lines from all views .... didnt work, all the numbers are placed in the same site, and cannot be read... Any idea please??? Thanks!! – BamsBamx May 22 '12 at 19:23
  • I investigated more, and i put Log.i funcion into each case... The thing is `drag` is called correctly, and then `action_down` case shows log but acion_move and action_ up dont make any log.... Please help!!! – BamsBamx May 23 '12 at 21:53
  • thats strange! :P are u using onlongclick listener as well? – Vishnu Mohan G May 23 '12 at 22:11
  • No, now i am using ontouchlistener, like in edit2... I am searching why actionup and actionmove are not called... I found something about using invalidate or return true .... What do you think??? – BamsBamx May 23 '12 at 22:18
  • well, not necessary, u may try. – Vishnu Mohan G May 23 '12 at 22:33
  • 2
    Well....YES!!!!!!!!!!!!! IT WORKED, THANKS!!!! Just returned true instead of false inside of ontouch()... – BamsBamx May 23 '12 at 22:49
  • well, return true and layout_gravity = top. – Vishnu Mohan G May 23 '12 at 22:50
0
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;
    }
    }
}

Copy this method to ur class and call it inside your textview's ontouch(). Here i assume u use Framelayout. If not change the layout parameters accordingly.

Vishnu Mohan G
  • 622
  • 1
  • 7
  • 14
  • thanks! but it shows error: The method drag(MotionEvent, View) from the type new View.OnLongClickListener(){} is never used locally, any help? – BamsBamx May 21 '12 at 21:18
  • Please, see the edit in first message, i am using relative layout...so, is this Ok? – BamsBamx May 21 '12 at 23:19
  • if u r using relative layout, u have to relate everything to boundaries, not to any view. then apply margins. Better use framelayout, or well, absolute layout is deprecated.and by the way, call this in onTouch of the views. yeah, u need onTouch here. – Vishnu Mohan G May 22 '12 at 11:16
  • Hi, i am here again... Is this mehod compatible with ICS?? I ask this because i have recently updated my phone to ICS, and now i can only move textviews vertically.. – BamsBamx Jun 13 '12 at 23:41
  • Thats strange! i am not sure abt ICS, is the same apk working fine in lower ones? – Vishnu Mohan G Jun 22 '12 at 03:02