1

I want to display a box with suggested entries next to an EditText. I figured I'd have to set LayoutParams (like suggested in this question.

However, no matter what I try, the box always ends up in the top left corner.

What I tried: - get LayoutParams as MarginLayoutParams (which they are), and set them. Set them back in the view for good measure. - All the answers in the link mentioned above

  • Change the position with TranslationX and -Y (which works, but runs into other problems like status bar might not always be 24dp and seems a hack)
                        val layoutParams = box?.layoutParams
                        //val layoutParams = ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply{
                        //    setMargins(left, top, 0,0)
                        //}
                        if (layoutParams !is ViewGroup.MarginLayoutParams) return@onTextChanged

                        //layoutParams.leftMargin = left
                        layoutParams.marginStart = left
                        layoutParams.topMargin = top
                        box?.layoutParams = layoutParams // Don't think this is necessary 
                        Log.d(TAG, "$layoutParams")

                        // works, but don't like it
                        // box?.translationX = left.toFloat()
                        // box?.translationY = top.toFloat()

                        //doesn't work
                        //box?.setMargins(left, top, right, 1000)

the function View.Setmargins is as follows:

fun View.setMargins(left: Int, top: Int, right: Int, bottom: Int) {
    if (layoutParams is MarginLayoutParams) {
        val p = layoutParams as MarginLayoutParams
        p.setMargins(left, top, right, bottom)
        requestLayout()
    }
}

Complete source of the thing I want to do here (snippet above is at line 170, edited it a bit to show things I've tried)

Other things I have tried: add box to different layouts, which in all cases results in the box being drawn in the top-left corner of that layout

Joozd
  • 501
  • 2
  • 14
  • 1
    What's the parent `ViewGroup`? Are both the `EditText` and the box direct children of it? Where exactly are you trying to place the box in relation to the `EditText`? – Mike M. May 12 '20 at 07:26
  • parent `ViewGroup` is the first ConstrainView it finds going up from `EditText`, editText in this case is in a `TextInputLayout`. However, I get the correct x and y coordinates (which is why the `TranslationX` stil works. I want to put the box just below the `EditText`. No matter which layout I add it to it will end up on the top left corner of that layout with 0 margins – Joozd May 12 '20 at 08:54
  • 1
    Is the box in the `ConstraintLayout`? Are you adding that programatically? If so, are you adding it before trying to modify the `LayoutParams`? `View`s don't have any `LayoutParams` by default, so if you're running that first block before any have been set, that's going to return at the `if`, and when you do eventually add the box, it will get the default `LayoutParams`, which would put it in the top-left corner. – Mike M. May 12 '20 at 09:28
  • all answers are yes, I add it programatically to `ConstraintLayout` before updating `LayoutParams`, I also tried creating new `LayoutParams` and adding those which has the same (no) effect. – Joozd May 12 '20 at 11:44
  • 1
    OK, I don't use `ConstraintLayout` much, but I would guess that the margins are being ignored because there are no constraints on the box. Try getting the `LayoutParams` as `ConstraintLayout.LayoutParams`, and also set its `topToTop` and `startToStart` fields to `ConstraintLayout.LayoutParams.PARENT_ID`. – Mike M. May 12 '20 at 11:56
  • 1
    Also, I forgot to mention earlier, but have you considered using a `PopupWindow` for this? It might be a little easier, and it's how things like this are often done, even in the platform; e.g., for `Spinner` dropdowns, context menus, etc. – Mike M. May 12 '20 at 11:57
  • 1
    yaaaaay that worked! Also, I shall look into `PopupWindow` which I didn't use because I didn't know about it :). Also: I am not quite sure why I didn't just use an `AutoCompleteTextView`. Thanks a lot for the help! – Joozd May 12 '20 at 12:46

1 Answers1

0

For anybody having the same problem as I had: Mike M. 's suggestion to get the LayoutParams as ConstraintLayout.LayoutParams, and also set its topToTop and startToStart fields to ConstraintLayout.LayoutParams.PARENT_ID did the trick, ie.

(box?.layoutParams as ConstraintLayout.LayoutParams).apply{
    topToTop = ConstraintLayout.LayoutParams.PARENT_ID
    startToStart = ConstraintLayout.LayoutParams.PARENT_ID
    topMargin = top
    marginStart = left
}
Joozd
  • 501
  • 2
  • 14