Reputation:
I have 10X4 table with in that some cell have image, need to drag and drop image in side table in cells. how to find already cell has image or not?
My Code: for creating cells, drag and drop.
public class WhiteFragment extends Fragment {
View mView;
private TableLayout Table1;
ImageView img;
int[] imageArray = {R.drawable.a, R.drawable.a, R.drawable.a, R.drawable.a};
LinearLayout layout;
View parentView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_white, container, false);
Table1 = (TableLayout) mView.findViewById(R.id.table);
for (int i = 0; i < 4; i++) {
TableRow TR = new TableRow(getActivity());
for (int j = 0; j < 10; j++) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.table_image, null, false);
img = (ImageView) row.findViewById(R.id.ivImage);
layout=(LinearLayout) row.findViewById(R.id.layout);
if(j%2==0)
img.setImageResource(imageArray[i]);
layout.setOnTouchListener(new MyTouchListener());
layout.setOnDragListener(new MyDragListener());
TR.addView(row);
}
Table1.addView(TR);
}
return mView;
}
float x, y = 0.0f;
boolean ismoving;
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
Log.e("owner status","down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("owner status","move");
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
break;
case MotionEvent.ACTION_UP:
Log.e("owner status","up");
break;
}
return true;
}
}
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
if(owner.getChildCount()==0)
container.addView(view);
else owner.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_border">
<LinearLayout
android:id="@+id/layout"
android:layout_width="120dp"
android:layout_height="120dp">
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>
suggest your logics..thanks
Upvotes: 3
Views: 666
Reputation:
Yes, I solved this using setTag for elements and while drag checking status of that tag in Saved List.
Upvotes: 2
Reputation: 7511
i would do it like this.
int[] newLocation = new int[2];
button.getLocationOnScreen(newLocation);
int x = newLocation[0];
int y = newLocation[1];
imageView.setX(x);
imageView.setY(y);
Also you can override onTouch listener and get position . but use setX() and setY()
Upvotes: 0
Reputation: 2312
use this code
in Activity
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch(event.getActionMasked())
{
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;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch(event.getActionMasked())
{
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;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});
XML File:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="50sp" android:layout_height="50sp"
android:id="@+id/image" android:src="@drawable/image">
</ImageView>
<ImageView android:layout_y="30dip" android:layout_x="118dip"
android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
android:src="@drawable/image1">
</ImageView>
</RelativeLayout>
Upvotes: 0