Sunday , November 28 2021

Android Floating Widget Tutorial – Display Widget Over Apps – Android Coding by DMTechnolab

The package The trap.Simplify.androidfloatingwidget;

Import Android.App.Service;

Import Android.Content.Intent;

Import Android.The graphics.PixelFormat;

Import Android.The.IBinder;

Import Android.Opinion.Layout;

Import Android.Opinion.Motion;

Import Android.Opinion.Opinion;

Import Android.Opinion.WindowManager;

public class FloatingViewService Is spread Service come into force Opinion.OnClickListener {{

Private WindowManager mWindowManager;

Private Opinion mFloatingView;

Private Opinion Collapsed scene;

Private Opinion Extended view;

public FloatingViewService()) {{

}

@ Override

public IBinder Unscrewed()Intent is intended) {{

The return Invalid;

}

@ Override

public Zero Go ahead()) {{

Super.Go ahead());

// get the widget layout from xml using layout inflotter

mFloatingView = Layout.From()this).To inflate()R.Layout.Layout_flotting_gate, Invalid);

// Setting Layout Parameter

The last WindowManager.Layoutparts The ultimate = New WindowManager.Layoutparts()

WindowManager.Layout love.wrap content,

WindowManager.Layout love.wrap content,

WindowManager.Layoutparts.TYPE_PHONE,

WindowManager.Layoutparts.FLAG_NOT_FOCUSABLE,

PixelFormat.Translucent);

// Getting Windows services and adding floating views to it

mWindowManager = ()WindowManager) getSystemService()WINDOW_SERVICE);

mWindowManager.Addview()mFloatingView, Para);

// Getting a collapsed and extended view from a floating view

Collapsed scene = mFloatingView.findViewById()R.Id.Layoutcapsapped);

Extended view = mFloatingView.findViewById()R.Id.Layout);

// add click listener to close button and expanded view

mFloatingView.findViewById()R.Id.Click the button).setOnClickListener()this);

Extended view.setOnClickListener()this);

// adding a touchlist to create a drag movement of the floating widget

mFloatingView.findViewById()R.Id.Relative bootout).setOnTouchListener()New Opinion.OnTouchListener()) {{

Private int Initials;

Private int Initial;

Private swimming initialTouchX;

Private swimming Initial;

@ Override

public Boolean on touch()Opinion v, Motion The competition) {{

to switch ()The competition.Gotation())) {{

matter Motion.ACTION_DOWN:

Initials = Para.X;

Initial = The ultimate.This;

initialTouchX = The competition.getRawX());

Initial = The competition.getRawY());

The return True;

matter Motion.ACTION_UP:

// The position of the widget changes when the drag is finished

Collapsed scene.Instability()Opinion.Gone);

Extended view.Instability()Opinion.View);

The return True;

matter Motion.ACTION_MOVE:

// This code is helping the widget move across the screen with fingers

Para.X = Initials + ()int) ()The competition.getRawX()) initialTouchX);

Para.This = Initial + ()int) ()The competition.getRawY()) Initial);

mWindowManager.updateViewLayout()mFloatingView, The ultimate);

The return True;

}

The return false;

}

});

}

@ Override

public Zero onDestroy()) {{

Super.onDestroy());

If ()mFloatingView ! = Invalid) mWindowManager.Delete scene()mFloatingView);

}

@ Override

public Zero on click()Opinion v) {{

to switch ()v.GetId())) {{

matter R.Id.Layout:

// switching idea

Collapsed scene.Instability()Opinion.View);

Extended view.Instability()Opinion.Gone);

break;

matter R.Id.Click the button:

// close the widget

Stop yourself());

break;

}

}

}


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

Your email address will not be published. Required fields are marked *