Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android Drag drop
Hi All,
 
I am facing a rather weird problem in Relative layout
 
Objective:
Drag and drop text view in a layout(I have used relative layout)
The text view must not go beyond the dimensions of the image
 
Problem:
1)I tried several methods to constraint text view within image size, yet not successful
2)At some places in the view when the text view is dragged, it gets clipped off or call it disappears (I have no clue whats happening)
 

Here is my main activity(DragActivity)
package com.example.myapp8draganddrop;
 
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
 
public class DragActivity extends Activity implements OnTouchListener  {
 
	TextView tv = null;
	RelativeLayout mRelativeLayoutText;
	boolean TOUCH_STATUS=false;
	int mLayoutLeft,mLayoutTop,mLayoutRight,mLayoutBottom; 
	int mLayoutWidth,mLayoutHeight;
	
	
	
	
	public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.activity_drag);
	 
	 mRelativeLayoutText = (RelativeLayout)findViewById(R.id.relLayout);
	 getDimensions();
	
	tv = (TextView)findViewById(R.id.textView1);
	 tv.setTextColor(Color.GREEN);
	 
	 
	 
	 tv.setOnTouchListener(new OnTouchListener() {
 
		 public boolean onTouch(View v, MotionEvent event) {
			 TOUCH_STATUS=true;
			 return false;
		 }
	  });
	
	
	 mRelativeLayoutText.setOnTouchListener(new OnTouchListener() {
 
		public boolean onTouch(View v, MotionEvent event) {
		
			int left=0,top=0,right=0,bottom=0;
				if(TOUCH_STATUS==true) // any event from down and move
				 {
				 LayoutParams lp = (RelativeLayout.LayoutParams)tv.getLayoutParams();
				 
				 left = (int)event.getX()-tv.getWidth();
				 //left = left > mRelativeLayoutText.getLeft() ? left : mRelativeLayoutText.getLeft();
				 top = (int)event.getY()-tv.getHeight();
				 //top  = top > mRelativeLayoutText.getTop()? top : mRelativeLayoutText.getTop();
				 
				 right = (int)event.getX()+tv.getWidth();
				 //right = right < mRelativeLayoutText.getRight() ? right : mRelativeLayoutText.getRight();
				 bottom = (int)event.getY()+tv.getHeight();
				 //bottom = bottom < mRelativeLayoutText.getBottom() ? bottom : mRelativeLayoutText.getBottom();
				 
				 
				 lp.setMargins(left,top,right,bottom);
				 tv.setLayoutParams(lp);
				 tv.setTextColor(Color.RED);
				 
				 }
				if(event.getAction()==MotionEvent.ACTION_UP){
				 TOUCH_STATUS=false;
				 tv.setTextColor(Color.GREEN);
				 String desc = "Left:"+left+"\tTop:"+top+"\nRight"+right+"\tBottom:"+bottom;
				 Toast.makeText(getApplicationContext(), desc , Toast.LENGTH_SHORT).show();
				 }
			return true;
		 	}
	 	});
	 }	//End onCreate()

	private void getDimensions() {
		
		mLayoutWidth = mRelativeLayoutText.getWidth();
		mLayoutHeight = mRelativeLayoutText.getHeight();
		
		mLayoutLeft = mRelativeLayoutText.getRight() - mLayoutWidth;
		mLayoutTop = mRelativeLayoutText.getBottom() - mLayoutHeight;
		
		mLayoutRight = mRelativeLayoutText.getLeft() + mLayoutWidth;
		mLayoutBottom = mRelativeLayoutText.getTop() + mLayoutHeight;
		
	}
 
	public boolean onTouch(View arg0, MotionEvent arg1) {
 
		return false;
	}
}
 

 

 
Here is my Layout.xml
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"	
    android:layout_height="fill_parent" 	
    android:id="@+id/dragActivityLayout" >
      
  		 <RelativeLayout
			 android:id="@+id/relLayout"
			 android:layout_width="400dip"
			 android:layout_height="200dip"
			 android:removed="@drawable/dark_knight_rises"
			 > 
			 <TextView
				     android:id="@+id/textView1"
				     android:layout_width="wrap_content"
			     	 android:layout_height="wrap_content"
			     	 android:layout_alignParentLeft="true"
			     	 android:layout_alignParentTop="true"
			     	 android:singleLine="true"
			      	 android:text="@string/textView1"/>
		 </RelativeLayout> 
 
</RelativeLayout>
 

 

Drag and drop screen shot
Posted 17-Sep-12 21:38pm
Comments
Sudendra at 18-Sep-12 2:39am
   
its like if i get close to batman, the text disappears :)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Replace with this code you will get correct output...and this will be only for absolute layout
 
<pre lang="java">
if(TOUCH_STATUS==true) // any event from down and move
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
img.setLayoutParams(lp);
 
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 16 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100