I tried my best can anyone help to drag and drop textview on imageview in android
my xml with frame layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
i tried drag and drop text as below as i have used here linear layout the text is dragging in some part only not on image
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DragNDropActivity extends Activity {
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup vg = (ViewGroup)findViewById(R.id.vg);
vg.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{
case MotionEvent.ACTION_MOVE:
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
if(x > w)
x = w;
if(y > h)
y = h;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(x, y, 0, 0);
selected_item.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});
TextView img = (TextView)findViewById(R.id.img);
img.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
break;
default:
break;
}
return false;
}
});
}
}
Try to use Frame Layout it should figure out what you want.
Use RelativeLayout like this. Note:You may place it inside your ParentView
<RelativeLayout android:layout_width="100dp" android:id="#+id/photoframe" android:layout_height="320dp" android:layout_weight="1.0">
<ImageView android:layout_height="fill_parent" android:scaleType="fitXY" android:id="#+id/framephoto" android:src="#drawable/icon" android:layout_width="fill_parent"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="#+id/textView1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="188dp"></TextView>
</RelativeLayout>
inside your layout.xml. By using Relative you can drag your textview over your image.
Please try this one....
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="#drawable/ic_launcher" android:scaleType="center" />
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#AA000000" android:textColor="#ffffffff" android:text="Golden Gate"/>
</FrameLayout>
I checked it with my eclipse.And let me know is it working or not.
Related
I would like to drag a view to any location on screen
I have implemented the touch listener and the only problem is that views that precede others in the hierarchy tend to drag them along. I want each view to be able to be dragged anywhere independent of the others.
I made sure I removed the view then added it with different parameters at the end of the drag.
public class MainActivity extends Activity {
int clickCount;
private float dx, dy;
private boolean isDragging;
private ViewGroup RootLayout;
private int Position_X;
private int Position_Y;
private TextView txtV01, txtV02, txtV03;
long startTime = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RootLayout = (ViewGroup) findViewById(R.id.main_layout);
txtV02 = (TextView) findViewById(R.id.txt02);
txtV02.setOnTouchListener(myTouchListener());
txtV01 = (TextView) findViewById(R.id.txt01);
txtV01.setOnTouchListener(myTouchListener());
txtV03 = (TextView) findViewById(R.id.txtV03);
txtV03.setOnTouchListener(myTouchListener());
}
private View.OnTouchListener myTouchListener() {
return new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View view, MotionEvent event) {
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams =
(LinearLayout.LayoutParams)
view.getLayoutParams();
dx = x - lParams.leftMargin;
dy = y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = x - (int) dx;
layoutParams.topMargin = y - (int) dy;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
RootLayout.invalidate();
return true;
}
};
}
}
the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="space">
<Space
android:layout_width="match_parent"
android:layout_height="387dp"
android:tag="space_top" />
<TextView
android:id="#+id/txt01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="cursive"
android:tag="line1"
android:text="This is line one ONE ONE ONE"
android:textAlignment="center"
android:textSize="33dp">
</TextView>
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:id="#+id/txt02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:tag="line2"
android:text="This is line TWO TWO NOT Line 1"
android:textAlignment="center"
android:textSize="18dp">
</TextView>
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:id="#+id/txtV03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:tag="line2"
android:text="This is line THREE THREE Leave me"
android:textAlignment="center"
android:textSize="18dp">
</TextView>
</LinearLayout>
Your help will be appreciated.
I have a Relative layout holds a surface view and another Relative layout (Nested)..
1 ) I want to receive touch events separately for Surface view, if touch events are out of child relative layout. ??
2) if my touch events falls on child relative layout, then the current touch event should not go to surface view.
How to achieve this toggle ???
Thanks in advance..!
Below is my Activity:
public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
private RelativeLayout letterView;
private RelativeLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
mainLayout.setOnTouchListener(this);
letterView = (RelativeLayout) findViewById(R.id.mlKnobView);
letterView.setOnTouchListener(this);
}
private boolean dragging = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == letterView) {
dragging = true;
eventConsumed = false;
}
} else if (action == MotionEvent.ACTION_UP) {
dragging = false;
eventConsumed = false;
} else if (action == MotionEvent.ACTION_MOVE) {
if (v != letterView) {
if (dragging) {
setAbsoluteLocationCentered(letterView, x, y);
}
}
}
return eventConsumed;
}
private void setAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}
private void setAbsoluteLocation(View v, int x, int y) {
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) v.getLayoutParams();
alp.leftMargin = x;
alp.topMargin = y;
v.setLayoutParams(alp);
}
}
Below my Xml File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/mlKnobView"
android:layout_width="260dip"
android:layout_height="260dip"
android:layout_below="#+id/txtView"
android:background="#drawable/border" >
<Button
android:id="#+id/button_down"
style="?android:attr/buttonStyle"
android:layout_width="140dip"
android:layout_height="60dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dip"
android:background="#drawable/button_down"
android:text="Down" />
<com.example.RotaryKnob
android:id="#+id/jogView"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_above="#id/button_down"
android:layout_below="#+id/button_up"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button_left"
style="?android:attr/buttonStyle"
android:layout_width="60dip"
android:layout_height="80dip"
android:layout_above="#id/button_down"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/jogView"
android:background="#drawable/button_left"
android:text="Left" />
<Button
android:id="#+id/button_right"
style="?android:attr/buttonStyle"
android:layout_width="60dip"
android:layout_height="80dip"
android:layout_above="#id/button_down"
android:layout_alignBaseline="#id/button_left"
android:layout_alignBottom="#id/button_left"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/jogView"
android:background="#drawable/button_right"
android:text="Right" />
<Button
android:id="#+id/button_up"
style="?android:attr/buttonStyleSmall"
android:layout_width="140dip"
android:layout_height="60dip"
android:layout_above="#+id/button_left"
android:layout_alignLeft="#+id/button_down"
android:layout_below="#+id/windowHeader"
android:background="#drawable/button_up"
android:text=" UP " />
</RelativeLayout>
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Hai" />
</RelativeLayout>
In my onDraw I have all code needed to build my entire view but how can I detect if I want to perform only a partial redraw.
I guess a partial redraw should be triggered by calling canvas.invalidate(Rect rect);
Right?
In developer settings of my device I enabled “Show screen updates” but this always tells me that my entire screen is redrawn…
Below you see a screenshot of my app:
As you can see it is a calendar and I want to give the user a visual feedback when an entry is clicked (let’s say a red border around)…
I’ve already seen some samples but they either use a bitmap or lots of member variables to execute just the code needed for redrawing specific region in onDraw…
Can anyone tell me what’s the best way to implement such a feature?
UPDATE:
On my first draw Canvas.getClipBounds() returns the following rect:
Rect(0, 0 - 1200, 1800)
when I call invalidate(new Rect(304, 748 - 529, 902)) and check getClipBounds() again in onDraw() it still has the same value.
UPDATE2 (my code):
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
_pTouchDown = new PointF(event.getX(), event.getY());
downX = event.getX();
downY = event.getY();
entrySelected = hasTimeRecordAt(downX, downY);
if (entrySelected != null) {
Rect rInvalidate = new Rect((int) entrySelected.get_Selected().left, (int) entrySelected.get_Selected().top, (int) entrySelected.get_Selected().right,
(int) entrySelected.get_Selected().bottom);
invalidate(rInvalidate);
}
return false;
}
UPDATE 3 (my Layout):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MultiDayCalendarActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rlStatusline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvStatusline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="asdf" >
</TextView>
<TextView
android:id="#+id/tvStatusline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="1234" >
</TextView>
</RelativeLayout>
<com.mxp.time.calendar.DayHeader
android:id="#+id/dayHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ScrollView
android:id="#+id/m_svMultiRoot1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<com.mxp.time.calendar.Calendar
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/brushBackgroundLight" >
</LinearLayout>
<RelativeLayout
android:id="#+id/rlMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
<ImageButton
android:id="#+id/ibtCreateNewTimeRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu" />
<ImageButton
android:id="#+id/ibtCalendarStopwatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stopwatch" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ibtCalendarBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/previous" />
<ImageButton
android:id="#+id/ibtCalendarForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/next" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<ImageButton
android:id="#+id/ibtCalendarToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/today" />
<ImageButton
android:id="#+id/ibtGotoJobs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/jobs" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" >
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
UPDATE4:
setContentView(R.layout.test_calendar);
// _cal = (Calendar) findViewById(R.id.calendar);
_cal = new Calendar(this);
_dayHeader = (DayHeader) findViewById(R.id.dayHeader);
final ScrollView sv = (ScrollView) findViewById(R.id.m_svMultiRoot1);
sv.addView(_cal);
same result:
I in onTouch I pass Rect(172, 748 - 265, 902) and in onDraw I get Rect(0, 0 - 720, 1800)
UPDATE 5:
package com.example.testclip;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
class V extends View {
private static final String TAG = "null";
Rect clip = new Rect();
public V(Context context) {
super(context);
int[] colors = { 0xff000000, 0xffff0000, 0xffffffff };
Drawable d = new android.graphics.drawable.GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM, colors);
setBackgroundDrawable(d);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
StringBuilder sb = new StringBuilder();
sb.append("left: ");
sb.append(x);
sb.append(", top: ");
sb.append(y);
sb.append("right: ");
sb.append(x + 10);
sb.append(", bottom: ");
sb.append(y + 10);
Log.d(TAG, "onTouchEvent clip rect: " + sb.toString());
invalidate(x, y, x + 10, y + 10);
return false;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(w, w * 4);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.getClipBounds(clip);
StringBuilder sb = new StringBuilder();
sb.append("left: ");
sb.append(clip.left);
sb.append(", top: ");
sb.append(clip.top);
sb.append("right: ");
sb.append(clip.right);
sb.append(", bottom: ");
sb.append(clip.bottom);
Log.d(TAG, "onDraw clip rect: " + sb.toString());
}
}
Activity:
package com.example.testclip;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ScrollView;
public class TestClipMainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_test_clip_main);
ScrollView sv = new ScrollView(this);
V v = new V(this);
sv.addView(v);
setContentView(sv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_clip_main, menu);
return true;
}
}
This code produces the following output
02-15 10:47:54.011: D/OpenGLRenderer(833): Enabling debug mode 0
02-15 10:47:54.926: D/dalvikvm(833): threadid=1: still suspended after undo (sc=1 dc=1)
02-15 10:48:03.806: D/null(833): onDraw clip rect: left: 0, top: 0right: 720, bottom: 2880
02-15 10:48:05.381: D/null(833): onDraw clip rect: left: 0, top: 0right: 720, bottom: 2880
02-15 10:48:07.181: D/null(833): onTouchEvent clip rect: left: 409, top: 358right: 419, bottom: 368
02-15 10:48:09.806: D/null(833): onDraw clip rect: left: 0, top: 0right: 720, bottom: 2880
you must be doing something wrong, see this custom View:
class V extends View {
Rect clip = new Rect();
private int cnt = 20;
public V(Context context) {
super(context);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
cnt++;
Log.d(TAG, "calling invalidate " + cnt);
invalidate(10, 10, cnt, cnt);
return false;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.getClipBounds(clip);
Log.d(TAG, "onDraw clip " + clip);
}
}
UPDATE: custom view inside a ScrollView:
class V extends View {
Rect clip = new Rect();
public V(Context context) {
super(context);
int[] colors = {0xff000000, 0xffff0000, 0xffffffff};
Drawable d = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
setBackgroundDrawable(d);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
invalidate(x, y, x + 10, y + 10);
return true;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(w, w * 4);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.getClipBounds(clip);
Log.d(TAG, "onDraw clip height: " + clip.height());
}
}
add this to onCreate:
ScrollView sv = new ScrollView(this);
V v = new V(this);
sv.addView(v);
setContentView(sv);
I am new to Android. The present code can drag and drop multiple Image Views on a single ImageView but I am not able to drop them on multiple Image Views. Kindly help as in how can I modify my code or any other existing code.
MainActivity.java
package n.f.letters;
import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
Boolean touchFlag=false;
boolean dropFlag=false;
LayoutParams imageParams;
ImageView imageDrop,image1,image2,image3,image4;
int crashX,crashY;
Drawable dropDrawable,selectDrawable;
Rect dropRect,selectRect;
int topy,leftX,rightX,bottomY;
int dropArray[];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ViewGroup container = (ViewGroup) findViewById(R.id.container);
imageDrop=(ImageView) findViewById(R.id.ImgDrop);
image1=(ImageView) findViewById(R.id.img1);
image2=(ImageView) findViewById(R.id.img2);
image3=(ImageView) findViewById(R.id.img3);
image4=(ImageView) findViewById(R.id.img4);
container.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(touchFlag==true)
{
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN :
topy=imageDrop.getTop();
leftX=imageDrop.getLeft();
rightX=imageDrop.getRight();
bottomY=imageDrop.getBottom();
//opRect.
break;
case MotionEvent.ACTION_MOVE:
crashX=(int) event.getX();
crashY=(int) event.getY();
int x = (int) event.getX() - offset_x;
int y = (int) event.getY()- offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth() - 50;
int h = getWindowManager().getDefaultDisplay().getHeight() - 10;
if (x > w)
x = w;
if (y > h)
y = h;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(x, y, 0, 0);
//Drop Image Here
if(crashX > leftX && crashX < rightX && crashY > topy && crashY < bottomY )
{
Drawable temp=selected_item.getBackground();
imageDrop.setBackgroundDrawable(temp);
imageDrop.bringToFront();
dropFlag=true;
selected_item.setVisibility(View.INVISIBLE);
}
//Drop Image Here
selected_item.setLayoutParams(lp);
break;
case MotionEvent.ACTION_UP:
//
touchFlag=false;
if(dropFlag==true)
{
dropFlag=false;
}
else
{
selected_item.setLayoutParams(imageParams);
}
break;
default:
break;
}
}else
{
System.err.println("Display Else Part ::->"+touchFlag);
}
return true;
}
});
image1.setOnTouchListener(this);
image2.setOnTouchListener(this);
image3.setOnTouchListener(this);
image4.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
touchFlag=true;
offset_x = (int) event.getX();
offset_y = (int) event.getY();
selected_item = v;
imageParams=v.getLayoutParams();
break;
case MotionEvent.ACTION_UP:
selected_item=null;
touchFlag=false;
break;
default:
break;
}
return false;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/ImgDrop"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#FFF123" />
<ImageView
android:id="#+id/img4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="75dp"
android:layout_marginTop="61dp"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignTop="#+id/img4"
android:layout_toRightOf="#+id/ImgDrop"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignRight="#+id/ImgDrop"
android:layout_alignTop="#+id/img3"
android:layout_marginRight="23dp"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignTop="#+id/img2"
android:layout_marginRight="40dp"
android:layout_toLeftOf="#+id/img2"
android:background="#drawable/ic_launcher" />
Drag and Drop
With the Android drag/drop framework, you can allow your users to move data from one View to another View in the current layout using a graphical drag and drop gesture. The framework includes a drag event class, drag listeners, and helper methods and classes.
Although the framework is primarily designed for data movement, you can use it for other UI actions. For example, you could create an app that mixes colors when the user drags a color icon over another icon. The rest of this topic, however, describes the framework in terms of data movement.
link see
i am working on application in which my ondraw method is not working properly ......
in my applicationi am using framelayout ...
so i am used three class...
1)acticvity
2) image choosing
3) drawing
my image selection is working fine but my drawing class is not working fine ..it is not displaying any thing ....
my drawing class is as follow ...
public class Draw extends View {
String info = "";
float x = 0;
float y = 0;
int color = Color.GREEN;
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Draw(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setColor(color);
paint.setStrokeWidth(15);
paint.setTextSize(30);
canvas.drawLine(x-10, y, x+10, y, paint);
canvas.drawLine(x, y-10, x, y+10, paint);
canvas.drawText(info, x, y, paint);
}
public void updateInfo(String t_info, float t_x, float t_y){
info = t_info;
x = t_x;
y = t_y;
invalidate();
}
public void clearInfo(){
info = "";
x = 0;
y = 0;
invalidate();
}
}
I am calling this from my mainactivity class.but i first call it from image selecting class as follow
public class FirstImage extends ImageView implements OnTouchListener {
MotionEvent event;
int a;
Bitmap image;
String huma ="human";
String info = "human";
float x = 0; //init value
float y = 0; //init value
Animation animationFadeIn;
public FirstImage(Context context) {
super(context);
}
public FirstImage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void changeImage(int id){
this.setImageResource(id);
a=id;
final Animation animationFadeout=AnimationUtils.loadAnimation(getContext(), R.anim.zoomin);
this.startAnimation(animationFadeout);
this.setOnTouchListener(this);
}
#Override
public boolean onTouch(View arg0, MotionEvent me) {
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
case MotionEvent.ACTION_MOVE:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
case MotionEvent.ACTION_UP:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
case MotionEvent.ACTION_OUTSIDE:
x=me.getX();
y= me.getY();
pageinfo(x,y);
break;
default: return true;
}
return false;
}
public void pageinfo(float x, float y) {
// TODO Auto-generated method stub
((AshActivity)getContext()).updateMsg(info, x,y);
}
}
it is called from the main activitry class
public class AshActivity extends Activity {
ImageView i;
Draw j;
TextView t,k;
ImageButton back;
ImageButton next;
OnClickListener l2 = null;
OnClickListener l = null;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
i = (FirstImage)findViewById(R.id.image);
j=(Draw)findViewById(R.id.info);
t=(TextView)findViewById(R.id.textView);
k=(TextView)findViewById(R.id.textView1);
back=(ImageButton)findViewById(R.id.button1);
next=(ImageButton)findViewById(R.id.button2);
if (count==0){
((FirstImage) i).changeImage(R.drawable.human);
}
//addListenerOnButton();
}
public void updateMsg(String info, float x, float y) {
// TODO Auto-generated method stub
j.updateInfo(info, x, y);
}
}
my layout xml file is as folow
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:name="http://schemas.android.com/apk/res/com.example.nam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- Screen Design for VIDEOS -->
<TableLayout
android:id="#+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:text="pic on click which will tell where is the dna located in human body or cell "
android:textSize="18dip" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.nam.FirstImage
android:id="#+id/image"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<com.example.nam.Draw
android:id="#+id/info"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</FrameLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="#drawable/monotone_arrow_next_left"/>
<ImageButton
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="#drawable/monotone_arrow_next_lrightcopy"/>
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:text="please don't mind "
android:textSize="18dip" />
</TableRow>
</TableLayout>
</ScrollView>
what is the problem i am not getting that ....
The problem is that your Draw instance's layout_height is 0.
This is due to the LayoutParams you've registered to it (wrap_content);
If you change the FrameLayout's attributes, the desired text will be displayed:
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ro.rekaszeru.sample.FirstImage android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ro.rekaszeru.sample.Draw android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
Please also note, that the text size of 30 is very large...