novafluff
novafluff

Reputation: 911

Android setting width to ConstraintLayout.LayoutParams.MATCH_PARENT gives -1

I have this code when I'm creating my popup

val width = ConstraintLayout.LayoutParams.MATCH_PARENT

The popup is showing up and have a full width as wanted. But for some reasons the width variable gets assigned -1 and I use that variable to populate other things in the popup so they get an negative width and don't show up. How come this gives back -1 when the popup gets full width?

Upvotes: 2

Views: 425

Answers (2)

Faz
Faz

Reputation: 354

public static final int MATCH_PARENT Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.

Constant Value: -1 (0xffffffff) (+)

To get the width of specific view, you can do as below :

int width = specificView.getWidth();

Upvotes: 3

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8715

The width of your view (guess ConstraintLayout) is not static, it depends on the device on which the view is rendered to. In kotlin you have a convenient method to calculate view dimensions at runtime:

view.doOnLayout {
    it.measuredWidth
    it.measuredHeight
}

Upvotes: 2

Related Questions