Click here to Skip to main content
15,883,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
Java
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

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
Comments
Sudendra 18-Sep-12 2:39am    
its like if i get close to batman, the text disappears :)

1 solution

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);

}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900