unable to access Clipboard Content from foreground service in Android 10+ - android

I know that we can't access clipboard content from background services in 10+ but there is an application that shows copied text on a floating widget in Android 10+ devices using foreground service here's that app. I want to achieve that functionality somehow! please help me.
My app is working fine in android P and lower devices!
FloatingViewService.java
public class FloatingViewService extends Service implements View.OnClickListener {
private WindowManager wm;
private View floatv;
View collapsed;
View expanded;
CharSequence copiedText;
EditText editText;
public FloatingViewService(){
}
#Override
public void onCreate() {
super.onCreate();
// getting the widget layout from xml using layout inflater
floatv = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget,null);
// setting window manager layout parameters
// set foucus able ,, wrap content, set on lock screen etc...
final WindowManager.LayoutParams wrules = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
// getting window service and adding floating view to it
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(floatv,wrules);
//adding click listener to close button and expanded view
collapsed = floatv.findViewById(R.id.layoutCollapsed);
expanded = floatv.findViewById(R.id.layoutExpanded);
editText = floatv.findViewById(R.id.pastehere);
// adding an touch listener to make drag movement of the floating view
floatv.findViewById(R.id.relativeLayoutParent).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float touchX;
private float touchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
initialX = wrules.x;
initialY = wrules.y;
touchX = event.getRawX();
touchY = event.getRawY();
return true;
/*as we implemented OnTouchListener() to the root view, OnClickListner() won’t work. So, to detect clicks we will detect clicks in MotionEvent.ACTION_MOVE in the OnTouchListener() using below code:*/
case MotionEvent.ACTION_UP:
int diffX = (int) (event.getRawX() - touchX);
int diffY = (int) (event.getRawY() - touchY);
if(diffX < 10 && diffY < 10){
if (isViewCollapsed()){
collapsed.setVisibility(View.GONE);
expanded.setVisibility(View.VISIBLE);
wrules.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
wm.updateViewLayout(floatv,wrules);
}else {
collapsed.setVisibility(View.VISIBLE);
expanded.setVisibility(View.GONE);
wrules.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wm.updateViewLayout(floatv,wrules);
}
}
return true;
case MotionEvent.ACTION_MOVE:
// moving the widget across the screen
wrules.x = initialX + (int) (event.getRawX() - touchX);
wrules.y = initialY + (int) (event.getRawY() - touchY);
wm.updateViewLayout(floatv,wrules);
return true;
}
return false;
}
});
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged() {
copiedText = clipboardManager.getPrimaryClip().getItemAt(0).getText();
if(copiedText != null){
editText.setText(String.valueOf(copiedText));
wm.updateViewLayout(floatv,wrules);
}
Toast.makeText(getBaseContext(), copiedText, Toast.LENGTH_SHORT).show();
}
});
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
if(floatv != null) wm.removeView(floatv);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.layoutExpanded){
collapsed.setVisibility(View.VISIBLE);
expanded.setVisibility(View.GONE);
}else if(v.getId() == R.id.buttonClose){
stopSelf();
}
}
private boolean isViewCollapsed() {
return floatv == null || floatv.findViewById(R.id.layoutCollapsed).getVisibility() == View.VISIBLE;
}
}
layout_floating_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relativeLayoutParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- this is the collapsed layout -->
<RelativeLayout
android:id="#+id/layoutCollapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:id="#+id/collapsed_iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_baseline_menu_book_24" />
<ImageView
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="50dp"
android:src="#drawable/ic_baseline_close_24"
android:background="#color/teal_200"/>
</RelativeLayout>
<!-- this is the expanded layout -->
<LinearLayout
android:id="#+id/layoutExpanded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#a0c3d7"
android:orientation="horizontal"
android:padding="8dp"
android:visibility="gone">
<ImageView
android:id="#+id/buttonSimplifiedCodingExpanded"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_baseline_markunread_24"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/buttonSimplifiedCoding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="Dictionary"
android:textAlignment="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/pastehere"
android:textAlignment="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
see these images
text should appear in editText, working in android 9

Related

ANDROID Drag views anywhere on the layout

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.

Swipe in listView makes the link shrink

I followed a guide to add swipe to my listView.
Basically there is a gesture listener on each row, when swiping the visibility of the back layout is set to visible and the buttons appear, while the front layout disappears
Unfortunately, whenever I swipe, the dimension of the row shrinks back to that of the buttons I want to show after swiping.
after swiping the row shrinks
for better comprehension, with visibility of both backgrounds
(sorry can't post images yet)
how do I keep the same height when showing the buttons?
(mind that rows' height might vary)
list_item_swipeable.xml (for each row)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layout_front"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/list_item_link"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/list_item_origin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:id="#+id/list_item_created_at"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#color/colorAccent" />
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_back"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="right"
android:visibility="gone"
>
<ImageButton
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/delete"
android:background="#color/btn_delete_bg"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="#+id/btnEdit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/edit"
android:background="#color/btn_edit_bg"
android:layout_toLeftOf="#id/btnDelete"
/>
</RelativeLayout>
</RelativeLayout>
MyGestureListener
public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int MIN_DISTANCE = 50;
private static final String TAG = "MyGestureListener";
private RelativeLayout backLayout;
private LinearLayout frontLayout;
private Animation inFromRight,outToRight,outToLeft,inFromLeft;
private ViewGroup.LayoutParams frontParams;
private ViewGroup.LayoutParams backParams;
public MyGestureListener(Context ctx, View convertView) {
backLayout = (RelativeLayout) convertView.findViewById(R.id.layout_back);
frontLayout = (LinearLayout) convertView.findViewById(R.id.layout_front);
inFromRight = AnimationUtils.loadAnimation(ctx, R.anim.in_from_right);
outToRight = AnimationUtils.loadAnimation(ctx, R.anim.out_to_right);
outToLeft = AnimationUtils.loadAnimation(ctx, R.anim.out_to_left);
inFromLeft = AnimationUtils.loadAnimation(ctx, R.anim.in_from_left);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float diffX = e2.getX() - e1.getX();
float diffY = e2.getY() - e1.getY();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > MIN_DISTANCE) {
if(diffX<0){
Log.v(TAG, "Swipe Right to Left");
if(backLayout.getVisibility()==View.GONE){
frontParams = frontLayout.getLayoutParams();
backParams = backLayout.getLayoutParams();
Log.d("HEIGHT FRONT ", ": "+frontParams.height ); // why -1?
Log.d("HEIGHT BACK ", ": "+backParams.height ); // why -1?
frontLayout.startAnimation(outToLeft);
backLayout.setVisibility(View.VISIBLE);
backLayout.startAnimation(inFromRight);
frontLayout.setVisibility(View.GONE);
}
}else{
Log.v(TAG, "Swipe Left to Right");
if(backLayout.getVisibility()!=View.GONE){
backLayout.startAnimation(outToRight);
backLayout.setVisibility(View.GONE);
frontLayout.setVisibility(View.VISIBLE);
frontLayout.startAnimation(inFromLeft);
}
}
}
}
return true;
}
}
getView method in my custom listAdapter
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = inflator.inflate(R.layout.list_item_swipeable, null);
holder = new ViewHolder();
// holder.container = (LinearLayout) convertView.findViewById(R.id.container);
holder.container = (RelativeLayout) convertView.findViewById(R.id.container);
holder.created_at = (TextView) convertView.findViewById(R.id.list_item_created_at);
holder.link = (TextView) convertView.findViewById(R.id.list_item_link);
holder.origin = (TextView) convertView.findViewById(R.id.list_item_origin);
holder.mDetector = new GestureDetectorCompat(mContext, new MyGestureListener (mContext, convertView));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.created_at.setText( mData.get(position).getCreated_at());
holder.link.setText( mData.get(position).getLink());
holder.origin.setText( mData.get(position).getOrigin());
holder.container.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
holder.mDetector.onTouchEvent(event);
return true;
}
});
return convertView;
}

custom drag and drop

i am a begginer android develpoer and i am tring to build a small soiltare game.
for dragging the cards i implemented a custom viewgroup which is a "DragContainer" from one of the questions here.
my problem is that when i drag a linear layout. my linear layout holds the cards
with a - margin (to overlap the cards) but when i start the drag the dragged "shadow" is my layout without the margin.
here is an example
this is the start of the activity, the left is a linear layout with two children and also the right
when i start the drag this is what i see
as you can see the dragged "shadow" is bigger(without the - margin)
this is the code for the custom drag container(only the stuff that matters):
public boolean startDragChild(View child, ClipData data,
Object myLocalState, int flags) {
setDragTarget(child);
return child.startDrag(data, new EmptyDragShadowBuilder(child),
myLocalState, flags);
}
private void setDragTarget(View v) {
target = v;
onSetDragTarget(v);
}
/**
* this is similar to the constructor of DragShadowBuilder
*
* #param v
*/
protected void onSetDragTarget(View v) {
}
#Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (mOnDrag && target != null) {
canvas.save();
drawDragShadow(canvas);
canvas.restore();
}
}
protected void drawDragShadow(Canvas canvas) {
int h = target.getHeight();
int w = target.getWidth();
canvas.translate(mDragX - w / 2, mDragY - h / 2);
target.draw(canvas);
}
I guess akon does not require the explanation but I would like to tell to the audience of the question.
The margins inside the LinearLayout should be intact as you would be moving ViewGroup itself, so it's children won't have any issue with it's design,
Code is also available on the Github
public class DragTestTwoActivity extends AppCompatActivity {
private LinearLayout source_linearLayout;
private LinearLayout destination_linearLayout;
private static final String TAG = "DragTestTwoActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag_test_two);
initializeUI();
}
private void initializeUI() {
source_linearLayout = (LinearLayout) findViewById(R.id.DragTestTwoActivity_Source_LinearLayout);
destination_linearLayout = (LinearLayout) findViewById(R.id.DragTestActivityActivity_Destination_LinearLayout);
// source_linearLayout.setOnDragListener(new MyDragListener());
destination_linearLayout.setOnDragListener(new MyDragListener());
source_linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(source_linearLayout);
source_linearLayout.startDrag(data, shadowBuilder, source_linearLayout, 0);
return true;
}
});
}
private class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG, "Drag has started");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "Drag has ended");
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "Drag has entered");
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "Drag location");
break;
case DragEvent.ACTION_DROP:
Log.d(TAG, "Drag has dropped");
View source_linear_Layout = (LinearLayout) event.getLocalState();
LinearLayout view = (LinearLayout) source_linear_Layout.getParent();
view.removeView(source_linear_Layout); // This will remove the imageView where it was
LinearLayout linearLayout = (LinearLayout) v;
if (v.getId() == R.id.DragTestActivityActivity_Source_LinearLayout) {
Log.d(TAG, "This is a source location");
} else if (v.getId() == R.id.DragTestActivityActivity_Destination_LinearLayout) {
Log.d(TAG, "This is a destination");
}
linearLayout.addView(source_linear_Layout); // this will add the ImageView to the new location where it was dropped.
source_linear_Layout.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "Drag has exited");
break;
}
return true;
}
}
}
activity_drag_test_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="activities.list.first.DragTestTwoActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4sp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
android:background="#ABABAB"
android:orientation="vertical"
android:padding="4dp">
<LinearLayout
android:id="#+id/DragTestTwoActivity_Source_LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/DragTestTwoActivity_imageView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_margin="4dp"
android:scaleType="centerInside"
android:src="#drawable/gohan" />
<ImageView
android:id="#+id/DragTestTwoActivity_imageView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_margin="4dp"
android:scaleType="centerInside"
android:src="#drawable/goku" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/DragTestActivityActivity_Destination_LinearLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
android:background="#CACACB"
android:orientation="vertical"
android:padding="4dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
Output
this is how it would look like...
Of course, you can use standart views for this purpose but it is irrational. It will better to use SurfaceView (or if you know OpenGL GLSurfaceView).

How to toggle touch event between nested Relative layouts?

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>

drag and drop auto scroll with multiple layout in android

"auto scroll not working very well when i drag and drop the image with multiple layout"
I searched all over, but could not find a solution.
i got a solution from this link:
Make a scrollView autoscroll with drag and drop in Android
#thehayro thanks for such a nice example.
but it is working for only one layout and auto scroll is also worked. but i have more than 4-5 child layout in one linear layout and this layout is in the scroll view my layout file is like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.dragvdropdemo.MyScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scroll_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/lldrag">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll1"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/dr_logo" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fb" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll2"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/twitter" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll3"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/ImageView1_l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/twitter" />
<ImageView
android:id="#+id/ImageView2_l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView3_l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll4"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/ImageView1_l4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/twitter" />
<ImageView
android:id="#+id/ImageView2_l4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView3_l4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
</com.example.dragvdropdemo.MyScrollView>
i want to drag and drop image from one to another layout.i don't know how to solve it. i have tried but i could do it.i'm don't know veryand my code is:
public class MyActivity extends Activity {
ImageView img1,img2,img3,img01;
ImageView img02,img03,img1_l3,img2_l3,img3_l3,img1_l4,img2_l4,img3_l4;
LinearLayout ll1,ll2,ll3,ll4,lldrag;
MyScrollView myScrollView;
int mScrollDistance;
private static int oldvalue;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
//SCROLLVIEW
myScrollView = (MyScrollView) findViewById(R.id.scroll_view);
myScrollView.setOnScrollViewListener(new MyScrollView.OnScrollViewListener() {
#Override
public void onScrollChanged1(OnScrollViewListener listener) {
// TODO Auto-generated method stub
mScrollDistance = myScrollView.getScrollY();
}
});
img1 = (ImageView)findViewById(R.id.imageView1);
img2 = (ImageView)findViewById(R.id.imageView2);
img3 = (ImageView)findViewById(R.id.imageView3);
img01 = (ImageView)findViewById(R.id.ImageView01);
img02 = (ImageView)findViewById(R.id.ImageView02);
img03 = (ImageView)findViewById(R.id.ImageView03);
img1_l3 = (ImageView)findViewById(R.id.ImageView1_l3);
img2_l3 = (ImageView)findViewById(R.id.ImageView2_l3);
img3_l3 = (ImageView)findViewById(R.id.ImageView3_l3);
img1_l4 = (ImageView)findViewById(R.id.ImageView1_l4);
img2_l4 = (ImageView)findViewById(R.id.ImageView2_l4);
img3_l4 = (ImageView)findViewById(R.id.ImageView3_l4);
ll1 = (LinearLayout)findViewById(R.id.ll1);
ll2 = (LinearLayout)findViewById(R.id.ll2);
ll3 = (LinearLayout)findViewById(R.id.ll3);
ll4 = (LinearLayout)findViewById(R.id.ll4);
lldrag = (LinearLayout)findViewById(R.id.lldrag);
img1.setOnTouchListener(new ChoiceTouchListener());
img2.setOnTouchListener(new ChoiceTouchListener());
img3.setOnTouchListener(new ChoiceTouchListener());
img01.setOnTouchListener(new ChoiceTouchListener());
img02.setOnTouchListener(new ChoiceTouchListener());
img03.setOnTouchListener(new ChoiceTouchListener());
img1_l3.setOnTouchListener(new ChoiceTouchListener());
img2_l3.setOnTouchListener(new ChoiceTouchListener());
img3_l3.setOnTouchListener(new ChoiceTouchListener());
img1_l4.setOnTouchListener(new ChoiceTouchListener());
img2_l4.setOnTouchListener(new ChoiceTouchListener());
img3_l4.setOnTouchListener(new ChoiceTouchListener());
ll1.setOnDragListener(new ChoiceDragListener());
ll2.setOnDragListener(new ChoiceDragListener());
ll3.setOnDragListener(new ChoiceDragListener());
ll4.setOnDragListener(new ChoiceDragListener());
lldrag.setOnDragListener(new ChoiceDragListener());
}
private final class ChoiceTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
ClipData data=ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched''
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
}
/**
* DragListener will handle dragged views being dropped on the drop area
* - only the drop action will have processing added to it as we are not
* - amending the default behavior for other parts of the drag process
*
*/
class ChoiceDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//no action necessary
Log.i("", "DRAGSTARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
Log.i("","DRAGENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
Log.i("","DRAGEXITED");
break;
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view
Log.i("","DROP");
View view = (View) event.getLocalState();
LinearLayout ll=(LinearLayout) view.getParent();
ll.removeView(view);
LinearLayout dropTarget = (LinearLayout) v;
ImageView dropped = (ImageView) view;
dropTarget.addView(dropped,0);
Object tag = dropTarget.getTag();
if(tag!=null)
{
int existingID = (Integer)tag;
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view being dropped on - to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
break;
case DragEvent.ACTION_DRAG_LOCATION:
//no action necessary
int y = Math.round(event.getY());
int translatedY = y - mScrollDistance;
Log.i("translated",""+translatedY+" "+ mScrollDistance+" "+y);
int threshold =50 ;
// make a scrolling up due the y has passed the threshold
if (translatedY < threshold) {
// make a scroll up by 30 px
myScrollView.scrollBy(0, -30);
}
// make a autoscrolling down due y has passed the 500 px border
if (translatedY + threshold > 200) {
// make a scroll down by 30 px
myScrollView.scrollBy(0, 30);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
//no action necessary
break;
default:
break;
}
return true;
}
}
}
MySrollView
public class MyScrollView extends ScrollView {
public OnScrollViewListener mListener;
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
if (mListener != null) {
mListener.onScrollChanged1(mListener);
}
}
public void setOnScrollViewListener(OnScrollViewListener listener) {
mListener = listener;
}
public static interface OnScrollViewListener {
public void onScrollChanged1(OnScrollViewListener listener);
}
}
i'm android beginner so don't know it very well so tell me how to solve it.
sorry for my bad english and grammer mistake.
thanks in advances
I solved this problem by myself. Here is the solution:
DragEvent.ACTION_DRAG_LOCATION:
//no action necessary
int y = Math.round(v.getY())+Math.round(event.getY());
int translatedY = y - mScrollDistance;
Log.i("translated",""+translatedY+" "+ mScrollDistance+" "+y);
int threshold =50 ;
// make a scrolling up due the y has passed the threshold
if (translatedY < 200) {
// make a scroll up by 30 px
myScrollView.smoothScrollBy(0, -15);
}
// make a autoscrolling down due y has passed the 500 px border
if (translatedY + threshold > 500) {
// make a scroll down by 30 px
myScrollView.smoothScrollBy(0, 15);
}
break;

Categories

Resources