Dragging an imageview to certain position - android

I am trying to create a draggable imageView ("chatHead" in the code, kinda like the chat head of Facebook)
I followed the instruction on the android website but whereever I drag the imageview, it always ends up at the left upper corner. (with x,y cordinate being (0,0) ~ (90,90))
Later on I found out the problem is that the case: MotionEvent.ACTION_MOVE is only entered when my touch is around the imageView location.
And when I drag a bit further away from the imageView, ACTION_MOVE is no longer entered. And instead the case DragEvent.ACTION_DRAG_EXIT is executed.
Can someone tell me how can I expand the area for the ACTION_MOVE to be executed or what is the problem. I would like to locate the imageView at whichever location I want on the screen.
( you may just focus on onDragListener, the MyDragShadowBuilder is a customized extending DragShawdowBuilder and onLongClickListener is just to trigger the drag)
How to keep the imageView even the app is closed?
public class ChatHead extends Service {
private WindowManager windowManager;
private ImageView chatHead;
private WindowManager.LayoutParams params;
private final String CHAT_TAG ="CHAT";
private int cor_x = 0;
private int cor_y = 0;
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override public void onCreate() {
super.onCreate();
ImageView image = new ImageView(this);
Log.d("", "created");
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setTag(CHAT_TAG);
chatHead.setImageResource(R.drawable.chat_icon);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 300;
params.y = 300;
chatHead.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item(v.getTag().toString());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes,item);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder(chatHead);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
return false;}
});
chatHead.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
//cor_x = ((int) chatHead.getX());
//cor_y = ((int) chatHead.getY());
switch (event.getAction()){
case DragEvent.ACTION_DRAG_ENTERED : { Log.d("", "x:"+ event.getX()+"y:"+event.getY());break;}
case DragEvent.ACTION_DRAG_STARTED : { Log.d("", "x:"+ event.getX()+" y:"+event.getY());break;}
case MotionEvent.ACTION_MOVE: {
cor_x = ((int) event.getX());
cor_y = ((int) event.getX());
Log.d("", "x:"+ cor_x+" y:"+cor_y);
break;}
case DragEvent.ACTION_DRAG_EXITED:{
Log.d("", "x:"+ cor_x+" y:"+cor_y);
break;
}
case DragEvent.ACTION_DRAG_ENDED : {
if(windowManager!=null && params!=null){
params.x = cor_x;
params.y = cor_y;
Log.d("", "x:"+ cor_x+" y:"+cor_y);
windowManager.removeView(chatHead);
windowManager.addView(chatHead, params);
}
return false;
}
}
return false;
}
});
windowManager.addView(chatHead, params);
}

If I understand what you mean, your problem is that you don't get the MotionEvent.ACTION_MOVE unless your finger is above your image.
Well that is because the concept of DragListener is that the View that register that listener, accept the events. So the only view in your layout that register to those events is chatHead, and he gets only the events that occur above him.
So if you want to get all the events in layout you must register view that take the whole screen space. So the quickest solution is to wrap the view with container that take the whole screen, and register it instead the chatHead to the DragListener. Somethong like:
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
RelativeLayout container = new RelativeLayout(this);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
chatHead = new ImageView(this);
chatHead.setTag(CHAT_TAG);
chatHead.setImageResource(R.drawable.chat_icon);
RelativeLayout.LayoutParams chatHeadParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
chatHeadParams.addRule(Gravity.TOP | Gravity.LEFT) ;
chatHeadParams.leftMargin = 300;
chatHeadParams.topMargin = 300;
container.addView(chatHead, chatHeadParams);
windowManager.addView(container, params);
And changing the line:
chatHead.setOnLongClickListener(new View.OnLongClickListener() {
to:
container.setOnLongClickListener(new View.OnLongClickListener() {
You may also adjust additional things because the DragEvent event.getX() and event.getX() will return different values according to the View itself and the DragEvent.Action type.

Related

Android Service desstroyed and stop automatically without calling stopService() or stopSelf

I have spend like 2 hours,
I'm unable to figure out what is the issue in service. I'm not calling stopService() or stopSelf from anywhere else. Below is the code ,
public class FloatingViewService extends Service {
private WindowManager mWindowManager;
private View mFloatingView;
WindowManager.LayoutParams params,landscapeParams,nonTouchableParams;
public FloatingViewService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
//Add the view to the window.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
//Add the view to the window.
landscapeParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
landscapeParams.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
landscapeParams.x = 0;
landscapeParams.y = 100;
//Add the view to the window.
nonTouchableParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
nonTouchableParams.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
nonTouchableParams.x = 0;
nonTouchableParams.y = 100;
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
//The root element of the collapsed view layout
final View collapsedView = mFloatingView.findViewById(R.id.collapse_view);
//The root element of the expanded view layout
final View expandedView = mFloatingView.findViewById(R.id.expanded_container);
//Set the close button
ImageView closeButtonCollapsed = (ImageView) mFloatingView.findViewById(R.id.close_btn);
closeButtonCollapsed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
stopSelf();
}
});
//Set the close button
ImageView closeButton = (ImageView) mFloatingView.findViewById(R.id.close_button);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
collapsedView.setVisibility(View.VISIBLE);
expandedView.setVisibility(View.GONE);
}
});
//Set the close button
ImageView lock = (ImageView) mFloatingView.findViewById(R.id.lock_button);
lock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWindowManager.updateViewLayout(mFloatingView, nonTouchableParams);
}
});
//Set the close button
ImageView expand = (ImageView) mFloatingView.findViewById(R.id.expand);
expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWindowManager.updateViewLayout(mFloatingView, landscapeParams);
}
});
//Drag and move floating view using user's touch action.
mFloatingView.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
//So that is click event.
if (Xdiff < 10 && Ydiff < 10) {
if (isViewCollapsed()) {
//When user clicks on the image view of the collapsed layout,
//visibility of the collapsed layout will be changed to "View.GONE"
//and expanded view will become visible.
collapsedView.setVisibility(View.GONE);
expandedView.setVisibility(View.VISIBLE);
}
}
return true;
case MotionEvent.ACTION_MOVE:
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
mWindowManager.updateViewLayout(mFloatingView, params);
return true;
}
return false;
}
});
}
/**
* Detect if the floating view is collapsed or expanded.
*
* #return true if the floating view is collapsed.
*/
private boolean isViewCollapsed() {
return mFloatingView == null || mFloatingView.findViewById(R.id.collapse_view).getVisibility() == View.VISIBLE;
}
#Override
public void onDestroy() {
super.onDestroy();
if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
}
}
I am not able to figure out anomalous behaviour help me out.
Start your service as forground.
https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
Since you want your service to run continuously you will have to override the onStartCommand method and return START_STICKY
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
https://developer.android.com/reference/android/app/Service.html

Scaling ImageView crops the image (added dynamically to windowmanager)

i created a floating button, not connected to any activity or viewgroup
(added it directly to the window manager), which i want to grow and shrink on touch.
However, scaling it more than 1 crops the image that i want to display.
How can i grow the imageview to match the rescaled image inside?
this is some relevant code:
Creating the AnimatedButton, which extends ImageView in the service class OnCreate
(AnimatedButton extends ImageView and just adds some extra drawing on top of the image when OnDraw is created,
don't think its code is relevant here):
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mFloatingButton = new AnimatedButton(this);
mFloatingButton.setImageResource(R.drawable.ax);
...
...
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
//this code is for dragging the chat head
mFloatingButton.setOnTouchListener(new View.OnTouchListener() {
...
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
...
case MotionEvent.ACTION_UP:
if (isAClick(initialTouchX, event.getRawX(), initialTouchY, event.getRawY())) {
...
ObjectAnimator anim = ObjectAnimator.ofFloat(mFloatingButton, "Scale", 1.0f, 3.0f);
anim.setDuration(700);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(1);
...
}
...
}
...
}
...
});
windowManager.addView(mFloatingButton, params);
...
}
and this is setScale of AnimatedButton:
public void setScale(float scale)
{
mScale = scale;
this.setScaleX(mScale);
this.setScaleY(mScale);
invalidate();
}
tnx

How to create a button which will stay on top of all window and can take click to open another similar overlay activity in android?

I am new at android programming. I have created a button using system overlay and a service. Now what I want is to click that button and get another overlay window of more 5 buttons. But the problem is that button doesn't take any click and don't allow me to open another overlay window. Here is the xml code I have used to draw the button--
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:id="#+id/l1"
android:layout_height="fill_parent"
tools:context=".OverlayActivity" >
<Button
android:id="#+id/mainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:clickable="true"
android:visibility="visible"
android:focusable="true">
</Button>
and here is the code I have used to show the overlay button
#Override
public void onCreate() {
super.onCreate();
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.activity_overlay, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
0 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
Button button = (Button)oView.findViewById(R.id.mainButton);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(OverlayService.this, "GotCha!!! ", Toast.LENGTH_LONG).show();
}
});
ViewGroup vg = (ViewGroup)oView.findViewById(R.id.l1);
final Display metrics = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
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 = metrics.getWidth()-100;
int h = metrics.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;
}
});
button.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;
}
});
}
Any ideas guys? I am running out of time..Please
Better you use fragment for this
Create views using fragments, overlay a button above the fragment.

Android: Overlay button onClick is never getting called

I have created an application that runs a service. The service basically has an Overlay button and the button on click does something. However, everytime, I click on the button, the application in the background of the button is responding. Please can someone help?
I have referred to link.
Here is my code:
Service code:
public class HUD extends Service implements OnTouchListener{
Button mButton;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
String s;
s = "Hari";
return null;
}
#Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setText("Overlay button");
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
if(mButton != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show();
return false;
}
}
The manifest has the following permission as well:
I tried changing the types to
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
Now, wherever I click, the touch event is getting called. Please can someone help me?
The problem is you are using WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY. I think since some Android 4.x it is not usable anymore. This is the way i realized an overlay Button:
public class OverlayButton extends Service implements OnTouchListener {
Button mButton;
WindowManager wm;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mButton = new Button(this);
mButton.setText("Overlay button");
mButton.setTextColor(Color.BLACK);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(150,
150, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.CENTER;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(), "onDestroy", Toast.LENGTH_LONG).show();
if (mButton != null) {
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("OverlayButton onTouch", "touched the button");
stopSelf();
}
return true;
}
}
What i did is, i also gave him the ability to disappear again, if i touch him.
This is the way i let him appear:
getActivity().startService(new Intent(getActivity().getBaseContext(), OverlayButton.class));
but, if you start it from your Activity, just use:
startService(new Intent(this, OverlayButton.class));
And don't forget to set these in your manifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
And inside the tags also don't forget:
<service android:name="OverlayButton" ></service>
When you use this WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH flag, it is telling the app to watch for all touches outside of your view.
Try using WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL instead. This will intercept touches only on your view.
To confirm, you need to be using WindowManager.LayoutParams.TYPE_SYSTEM_ALERT to make the view clickable.

Slider always on top - cannot move

I want to make my slider be always on top. - Already done.
But I cant make him move. What changes should I do?
public class SliderOnTop extends Service {
private SeekBar seekBar;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
seekBar = new SeekBar(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 45);
lp.bottomMargin = 0;
lp.leftMargin = 0;
layout.addView(seekBar, lp);
wm.addView(layout, params);
}
}
You could probably listen to onTouch events on your View, and then change the margins accordingly.

Categories

Resources