I want to change the GifImageView location on Android - 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;

Related

Custom view is being displayed as nested views in Component Tree

I have created custom view which extends FrameLayout. After adding it into RelativeLayout it's being displayed as two nested views:
Is it normal? It sometimes messes up with wrap_content flags but I couldn't figure out why. When I use View as a base class everything looks normal.
Here is my code:
MainActivity.java
package com.example.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import com.codersmill.tset.R;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.app.RateBar
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="#+id/rateBar"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
RateBar.java
package com.example.app;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.codersmill.tset.R;
public class RateBar extends FrameLayout {
TextView star1, star2, star3, star4, star5;
View dot;
private boolean isAnimating = false;
int radius = 36;
private int step = 100;
private int leftMargin = 50;
private int topMargin = 0;
private int currentPosition = 0;
public RateBar(Context context) {
this(context, null);
}
public RateBar(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.view_rate_bar, this);
star1 = (TextView) this.findViewById(R.id.star1);
star2 = (TextView) this.findViewById(R.id.star2);
star3 = (TextView) this.findViewById(R.id.star3);
star4 = (TextView) this.findViewById(R.id.star4);
star5 = (TextView) this.findViewById(R.id.star5);
star1.setOnClickListener(onClickListener);
star2.setOnClickListener(onClickListener);
star3.setOnClickListener(onClickListener);
star4.setOnClickListener(onClickListener);
star5.setOnClickListener(onClickListener);
dot = this.findViewById(R.id.selector);
}
public RateBar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override public void onClick(View v) {
if(isAnimating) return;
switch (v.getId()) {
case R.id.star1:
animateToPosition(0);
break;
case R.id.star2:
animateToPosition(1);
break;
case R.id.star3:
animateToPosition(2);
break;
case R.id.star4:
animateToPosition(3);
break;
case R.id.star5:
animateToPosition(4);
break;
}
}
};
#Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
step = (int) (getMeasuredWidth() / 5.0f);
leftMargin = (int) (step / 2.0f - radius);
topMargin = (int) (getMeasuredHeight() / 2.0f - radius);
if(!isAnimating) {
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.leftMargin = leftMargin + currentPosition * step;
params.topMargin = topMargin;
dot.setLayoutParams(params);
}
}
private void animateToPosition(final int position) {
final int from = currentPosition*step + leftMargin;
final int to = position*step + leftMargin;
ValueAnimator animation = ValueAnimator.ofInt(from, to);
animation.setDuration(250);
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override public void onAnimationUpdate(ValueAnimator animation) {
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.leftMargin = (int) animation.getAnimatedValue();
params.topMargin = topMargin;
dot.setLayoutParams(params);
}
});
animation.addListener(new AnimatorListenerAdapter() {
#Override public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
isAnimating = true;
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.topMargin = topMargin;
params.leftMargin = currentPosition * step + leftMargin;
dot.setLayoutParams(params);
}
#Override public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
isAnimating = false;
currentPosition = position;
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.leftMargin = currentPosition * step + leftMargin;
params.topMargin = topMargin;
dot.setLayoutParams(params);
}
});
animation.start();
}
private void updateDotPosition() {
}
}
view_rate_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<View
android:id="#+id/selector"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="#drawable/oval"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
android:id="#+id/star1"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#2200ff00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2"
android:id="#+id/star2"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#22ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="3"
android:id="#+id/star3"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#2200ff00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="4"
android:id="#+id/star4"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#22ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="5"
android:id="#+id/star5"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#2200ff00" />
</LinearLayout>
</merge>
even i was facing the same issue, it appears that new version of android studio comes up with two files content_main.xml and activity_mail.xml , when we select Activity_main.xml>Design view everything appears 'Custom View' , instead when we highlight content_main.xml>Design, everything is bac to normal. I dont know why it happens but that;s how i fix mine ( android nooob here )
More can be found here : https://teamtreehouse.com/community/i-cant-drag-widgets-onto-the-phone-mockup-component-tree-shows-customview-instead-of-the-relative-view-help

How can I implement a View in my Activity?

I have the Feldlayout class, the feldlayout.xml which is the layout of the Feldlayout class, and I have a View class, which can be used to make lines on the screen. I want now to make these lines on the feldlayout.xml layout, but I don't know how to connect my classes. So how can I do this?
Feldlayout.class (Main Class)
package com.example.volleyballapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class feldlayout extends Activity{
Button b7;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feldlayout);
b7 = (Button) findViewById(R.id.button7);
b7.setOnClickListener(handler);
}
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
if(v==b7){
Intent i = new Intent(feldlayout.this, Main_Layout.class);
startActivity(i);
}
}};}
Feldlayout.xml (Main Layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/farbe"
android:orientation="vertical" >
<ImageView
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="160dp"
android:background="#drawable/whiterectangle" />
<ImageView
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="74dp"
android:layout_marginLeft="74dp"
android:layout_marginRight="74dp"
android:layout_marginTop="164dp"
android:background="#drawable/volleyballfeld" />
<ImageView
android:id="#+id/view3meterlinie"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignTop="#+id/view1"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="100dp"
android:background="#drawable/dreimeterlinie" />
<ImageView
android:id="#+id/netz"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignTop="#+id/view1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="310dp"
android:background="#drawable/netz" />
<ImageView
android:id="#+id/view3meterliniedef"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignTop="#+id/view1"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="520dp"
android:background="#drawable/dreimeterlinie" />
<Button
android:id="#+id/button8"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/view1"
android:layout_marginTop="1dp"
android:background="#drawable/bnt_black"
android:text="Speichern" />
<Button
android:id="#+id/button7"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignLeft="#+id/view2"
android:layout_alignParentTop="true"
android:layout_marginTop="1dp"
android:background="#drawable/bnt_black"
android:onClick="home"
android:text="Zurück" />
<Button
android:id="#+id/button9"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignTop="#+id/button7"
android:layout_marginLeft="35dp"
android:layout_toRightOf="#+id/button7"
android:background="#drawable/bnt_black"
android:text="Linien Anzeigen" />
<Button
android:id="#+id/Statistik"
style="#style/HomeText"
android:layout_width="140dp"
android:layout_height="85dp"
android:layout_alignBaseline="#+id/button8"
android:layout_alignBottom="#+id/button8"
android:layout_marginRight="31dp"
android:layout_toLeftOf="#+id/button8"
android:background="#drawable/bnt_black"
android:text="Statistik" />
</RelativeLayout>
ViewFeld.class (View class, which makes lines)
package com.example.volleyballapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class ViewFeld extends View{
private Paint paint = new Paint();
private Path path = new Path();
int count = 1;
public ViewFeld(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint.setAntiAlias(true);
paint.setStrokeWidth(6f); //Breite des Strichs
paint.setColor(Color.BLACK); //Farbe des Strichs
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
protected void onDraw(Canvas canvas) {
if ((count%2 != 0)){
canvas.drawPath(path, paint);}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
count++;
if(count%2 != 1){
path.moveTo(eventX, eventY);
return true;
}
if(count%2 != 0){
path.lineTo(eventX, eventY);
break;
}
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
}
You can use your custom view in your layout xml:
<com.example.volleyballapp.ViewFeld
android:layout_width="....."
android:layout_height="....."
etc..... />

How can i drag and drop multiple imageviews over other imageviews 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

Moving image with touch events

I need a sample code for when we move(touch) a image from left to right and right to left. Please help me.
Regards,
chakri
try this code...
this is your main activity class
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
int x = (int) event.getRawX();
int y = (int) event.getRawY();
mParams.leftMargin = x-50;
mParams.topMargin = y-50;
imageView.setLayoutParams(mParams);
break;
default:
break;
}
return true;
}
});
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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="126dp"
android:layout_marginTop="168dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>

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