Posts

Showing posts from October, 2018

Simple example of DraggableTextView in Android | Kotlin

Image
Hello All, By using simple onTouch event we make an DraggableTextView widget.
Here is the code sample for you.
override fun onTouch(v: View?, event: MotionEvent?): Boolean { val x = event?.rawX?.toInt()!! val y = event.rawY.toInt() when (event.action) { MotionEvent.ACTION_DOWN -> { val layoutParams = layoutParams as ConstraintLayout.LayoutParams xDelta = x - layoutParams.leftMargin yDelta = y - layoutParams.topMargin rect.set(0, 0, width, height) isDragging = true } MotionEvent.ACTION_MOVE -> { val layoutParams = layoutParams as ConstraintLayout.LayoutParams layoutParams.leftMargin = x - xDelta layoutParams.topMargin = y - yDelta setLayoutParams(layoutParams) } MotionEvent.ACTION_UP -> { isDragging = false } } invalidate() return true }
Here is the full video tutorial. Interesting rig…

Using shuffle from Collections in Java | Android

Image
From Doc shuffle is used to Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.

Here is the simple example of shuffle from collections. List numbers = new ArrayList<>(); for (int i = 0; i <= 10; i++) { numbers.add(i); } Log.v(TAG, "Original List: " + numbers.toString()); Collections.shuffle(numbers); Log.v(TAG, "Shuffled List: " + numbers.toString()); Output:
Original List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled List: [2, 10, 0, 3, 7, 5, 1, 9, 8, 6, 4] There is also public static void shuffle(List list, Random rnd) which will Randomly permute the specified list using the specified source of randomness. Interesting right? If you are really interested in this example, then please share this post with your friends, also share your feedback as comment here.

Thank You