Reputation: 13
How to make floating bubble,like in copy bubble app? That floats on top of every android application, and when cliicked shows another view. Like that orange bubble
Upvotes: 0
Views: 4219
Reputation: 83
You can also use this Touch Listener to make any view draggable.
class ABDragViewTouchListener(val mContext: Context): View(mContext), View.OnTouchListener {
private var downRawX = 0f
private var downRawY = 0f
private var dX = 0f
private var dY = 0f
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
val layoutParams = view.layoutParams as ViewGroup.MarginLayoutParams
return when(motionEvent.action){
MotionEvent.ACTION_DOWN -> {
downRawX = motionEvent.rawX
downRawY = motionEvent.rawY
dX = view.x - downRawX
dY = view.y - downRawY
true // Consumed
}
MotionEvent.ACTION_MOVE -> {
val viewWidth = view.width
val viewHeight = view.height
val viewParent = view.parent as View
val parentWidth = viewParent.width
val parentHeight = viewParent.height
var newX = motionEvent.rawX + dX
newX = max(
layoutParams.leftMargin.toFloat(),
newX
) // Don't allow the FAB past the left hand side of the parent
newX = min(
(parentWidth - viewWidth - layoutParams.rightMargin).toFloat(),
newX
) // Don't allow the FAB past the right hand side of the parent
var newY = motionEvent.rawY + dY
newY = max(
layoutParams.topMargin.toFloat(),
newY
) // Don't allow the FAB past the top of the parent
newY = min(
(parentHeight - viewHeight - layoutParams.bottomMargin).toFloat(),
newY
) // Don't allow the FAB past the bottom of the parent
view.animate()
.x(newX)
.y(newY)
.setDuration(0)
.start()
true // Consumed
}
MotionEvent.ACTION_UP -> {
val upRawX = motionEvent.rawX
val upRawY = motionEvent.rawY
val upDX = upRawX - downRawX
val upDY = upRawY - downRawY
if (abs(upDX) < CLICK_DRAG_TOLERANCE && abs(
upDY
) < CLICK_DRAG_TOLERANCE
) { // A click
performClick()
} else { // A drag
true // Consumed
}
}
else -> {
super.onTouchEvent(motionEvent)
}
}
}
companion object {
private const val CLICK_DRAG_TOLERANCE =
10f // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.
}
}
Use it this way:
yourView.setOnTouchListener(ABDragViewTouchListener(mContext))
Upvotes: 0
Reputation: 1321
here is a simple example for floting bubble example: Floating Bubble customize this as your requirement.
Upvotes: 2
Reputation: 2577
You can achieve this by using WindowManager In Android.
By Using Window Manager you can add the views. On top of any android application. The Following link may useful to you.
http://www.programcreek.com/java-api-examples/index.php?api=android.view.WindowManager
Upvotes: -1