0

enter image description here

I want to make the gradient area swipe-able (down to show, swipe up to hide).

this is my code :

val scopeLayout = inflaterView.findViewById<ConstraintLayout>(R.id.scope_layout)

    scopeLayout.setOnTouchListener({ v, event ->
        when (event.action) {
            MotionEvent.ACTION_DOWN ->
                Toast.makeText(context, "you just touch the screen :-)", Toast.LENGTH_SHORT).show()
                scopeLayout.height = 215 // error val cannot be reassigned
        }
        true
    })

and I got the error val cannot be reassigned. and how to set the height with dp value?

Kakashi
  • 3,329
  • 4
  • 26
  • 37

2 Answers2

0

try this

public override bool OnTouchEvent (MotionEvent e)
{
    var action = e.Action;

    switch (action) {
    case MotionEventActions.Pointer3Down:
        Toast.MakeText (this, "hey", ToastLength.Long);

        break;

    case MotionEventActions.PointerUp:

        Toast.MakeText (this, "yo", ToastLength.Long);


        break;
    default:
        break;
    }




    if (e.Action == MotionEventActions.Move) {
        return true;
    }

    return base.OnTouchEvent (e);
}

I think it helps you

Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

use var instead of val as val is immutable.

For how to calculate dp to px you can

val Int.dp: Int 
   get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.px: Int
   get() = (this * Resources.getSystem().displayMetrics.density).toInt()

And then use it this way 88.dp

EDIT:

scopeLayout.layoutParams = scopeLayout.layoutParams.apply {
    width = ...
    height = ...
}
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • i changed to this `var scopeLayout = inflaterView.findViewById(R.id.scope_layout)` and still error `val cannot be reassigned` – Kakashi Mar 21 '18 at 09:28
  • Have try calling for layout params? added suggestion in my answer's edit. It seems to work it many cases – Rainmaker Mar 21 '18 at 09:34
  • it works! i set on swipe up height=70 and on swipe down height = 415. but everytime I swipe down, the height back to 70 again. it seem because it's `ACTION_DOWN` not `ACTION_SWIPE_UP` or something like that. do you know what event i should listen for swipe? – Kakashi Mar 21 '18 at 09:51
  • yeah, action_down doesn't mean a gesture like swipe down it means that user touched the screen. In android there is such interface as SimpleOnGestureListener. and GestureDetector . There you'll need to write motions that you want to catch with the coordinates of them. You can see this link https://stackoverflow.com/questions/13095494/how-to-detect-swipe-direction-between-left-right-and-up-down , but it's java I believe – Rainmaker Mar 21 '18 at 10:25