How can i drag and drop multiple imageviews over other imageviews android - android

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

Related

I want to change the GifImageView location on Android

package com.bariskarapelit.stajprojesi_1;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;
import com.twilio.video.CameraCapturer;
import com.twilio.video.LocalVideoTrack;
import com.twilio.video.VideoTextureView;
import pl.droidsonroids.gif.GifImageView;
import android.view.MotionEvent.*;
import static androidx.core.view.ViewCompat.getX;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity
{
ImageButton button,button1,button2;
GridLayout gridLayout;
ImageView circle,dislike,like;
VideoView videoView;
String videoPath;
VideoTextureView videoTextureView;
Uri uri;
GifImageView gifImageView;
MotionEvent event;
private LayoutParams layoutParams;
int windowwidth;
int windowheight;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= findViewById(R.id.circle);
button1=findViewById(R.id.dislike);
button2=findViewById(R.id.like);
circle=findViewById(R.id.imageView);
dislike=findViewById(R.id.imageView2);
like=findViewById(R.id.imageView3);
gifImageView= findViewById(R.id.gift);
circle.setVisibility(View.INVISIBLE);
dislike.setVisibility(View.INVISIBLE);
like.setVisibility(View.INVISIBLE);
//videoView=findViewById(R.id.video_view_top_right);
//Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
//videoView.setVideoURI(uri);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
gifImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
LayoutParams layoutParams = (LayoutParams) gifImageView.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
gifImageView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Circle",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.daire);
}
});
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Dislike",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.dislike);
}
});
button2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Like",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.like);
}
});
}
}
I want to change the GifImageView location on Android. By taking the position of the point at which the User has woven into the screen with the OnTouchListener method. I want to use this location in GifImageView relocation. Can you help me?
gifImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
LayoutParams layoutParams = (LayoutParams) gifImageView.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
gifImageView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
I tried to change places using the above code, but this error was included:
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at com.bariskarapelit.stajprojesi_1.MainActivity$1.onTouch(MainActivity.java:79)
at android.view.View.dispatchTouchEvent(View.java:13469)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:742)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1880)
at android.app.Activity.dispatchTouchEvent(Activity.java:3494)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:700)
at android.view.View.dispatchPointerEvent(View.java:13721)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6175)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5953)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5455)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5421)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5580)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5429)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5637)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5455)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5421)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5429)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8467)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8387)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8340)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8582)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Xml code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.twilio.video.VideoTextureView
android:id="#+id/video_view_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gift"
android:layout_width="106dp"
android:layout_height="106dp"
android:layout_gravity="center"
android:layout_marginTop="492dp"
android:layout_marginEnd="168dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginStart="61dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="40dp"
app:layout_constraintEnd_toStartOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/launch1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="40dp"
app:layout_constraintEnd_toStartOf="#+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/launch2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="428dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#mipmap/launch3" />
<ImageButton
android:id="#+id/circle"
android:layout_width="51dp"
android:layout_height="51dp"
android:layout_marginStart="60dp"
android:layout_marginBottom="4dp"
android:background="#mipmap/launch1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/dislike"
app:layout_constraintHorizontal_bias="0.574"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="#+id/dislike"
android:layout_width="51dp"
android:layout_height="51dp"
android:background="#mipmap/launch2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.499"
app:layout_constraintStart_toStartOf="parent"></ImageButton>
<ImageButton
android:id="#+id/like"
android:layout_width="51dp"
android:layout_height="51dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="4dp"
android:background="#mipmap/launch3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.447"
app:layout_constraintStart_toEndOf="#+id/dislike"></ImageButton>
</androidx.constraintlayout.widget.ConstraintLayout>
selectedImage = R.drawable.ic_dislike3;

How do I drag a view after it is dragged outside of it's parent?

I would like to able to drag the black box outside of its orange container and be able to drag it again. The black imageview does not respond to touch when it is outside of the orange box. I can only re-drag the black box if it is dropped somewhere in the orange box.
Screenshot of app
My code is as follows:
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:clipChildren="false"
android:clipToPadding="false"
android:padding="20dp">
<com.example.noname.myapplication.DraggableImageView
android:id="#+id/activity_animation_icon_imageview"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="35dp"
android:background="#000000"/>
</LinearLayout>
</LinearLayout>
DraggableImageView.java
public class DraggableImageView extends ImageView {
float dX, dY;
public DraggableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = getX() - event.getRawX();
dY = getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
break;
default:
return false;
}
return true;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Thanks in advance :)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center">
<launcher.simpleapp.vr.com.draggableimageview.DraggableImageView
android:id="#+id/activity_animation_icon_imageview"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="35dp"
android:background="#000000"/>
</LinearLayout>
Draggable ImageView
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
* Created by cvija on 8/28/2016.
*/
public class DraggableImageView extends ImageView{
int dX, dY;
private int _xDelta;
private int _yDelta;
public DraggableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) getLayoutParams();
_xDelta = dX - lParams.leftMargin;
_yDelta = dY - lParams.topMargin;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = (int)(getX() - event.getRawX());
dY = (int)( getY() - event.getRawY());
break;
case MotionEvent.ACTION_MOVE:
animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)
getLayoutParams();
layoutParams.leftMargin = dX - _xDelta;
layoutParams.topMargin = dY - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
setLayoutParams(layoutParams);
invalidate();
break;
default:
return true;
}
return true;
}
}
The same is working fine for me
That sense your touch will goes in the way of the view group first then you will get the control in your view
So you kept the image view inside the linear layout so that you will have control only inside the linear layout

Partial redraw -> invalidate (Rect rect)

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

How to get onToch event absolute position and comparing it with imageView coordinates?

I'm basically trying to edit the content of an editText by swiping on multiple ImageViews.
Here is my code but nothing seems to work.
package com.example.touchdemo;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Rect;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relLay = (RelativeLayout) findViewById(R.id.RelativeLayout1);
final TextView txtv = (TextView) findViewById(R.id.textView1);
final ImageView myImageView = (ImageView) findViewById(R.id.imageView1);
final ImageView myImageView2 = (ImageView) findViewById(R.id.imageView2);
myImageView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
int x,y;
if(event.getAction() == MotionEvent.ACTION_MOVE){
Rect rect = new Rect();
rect.left = myImageView.getLeft();
rect.top = myImageView.getTop();
rect.bottom = myImageView.getBottom();
rect.right = myImageView.getRight();
Rect rect2 = new Rect();
rect2.left = myImageView2.getLeft();
rect2.top = myImageView2.getTop();
rect2.bottom = myImageView2.getBottom();
rect2.right = myImageView2.getRight();
x = (int) event.getRawX();
y = (int) event.getRawX();
if(rect.contains(x, y)) {
txtv.append("Ti ho trovato 1");
}
else if(rect2.contains(x, y)) {
txtv.append("Ti ho trovato 2");
}
}
// txtv.append("ciao");
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is my xml:
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="86dp"
android:layout_toRightOf="#+id/imageView1"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="94dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="124dp"
android:text="TextView" />
I think that the best way is to get the Event coordinates based on full screen dimension and the checking if they are contained in the rectangles representing my imageViews.
Also I'd like to figure out if in my code the onTouch event is invoked continuously.
I'd be grateful for any suggestion.
You're checking for whether the touch listener records a touch within the rect defined by your imageview. However, you're only checking that when in an ACTION_MOVE event. You probably want to catch the initial touch(es) with ACTION_DOWN, which is defined to be on the view. ACTION_MOVE is when you've already touched the view and are moving your finger

Drag And Drop textview on imageview in android

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.

Categories

Resources