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)
{
LayoutParams lp = (RelativeLayout.LayoutParams)tv.getLayoutParams();
left = (int)event.getX()-tv.getWidth();
top = (int)event.getY()-tv.getHeight();
right = (int)event.getX()+tv.getWidth();
bottom = (int)event.getY()+tv.getHeight();
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;
}
});
}
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