change the height of the linear layout programmatically in Android

I am trying to programmatically change the height of a linear layout. When i use

ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

I get an exception:

java.lang.ClassCastException: android.view.ViewGroup $ LayoutParams

+3
source share
3 answers

I got a solution class LinearLayout.LayoutParamslike

ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
+8
source

I assume that you are importing different LayoutParams. Try the full version:

ll.setLayoutParams(new android.view.ViewGroup.LayoutParams(
    android.view.ViewGroup.LayoutParams.FILL_PARENT,
    android.view.ViewGroup.LayoutParams.WRAP_CONTENT
));
+3
source

When you first include LayoutParams in your code and press Cntrl + Shift + Enter to automatically import the necessary files, you will see a list of all packages. Make sure you import the correct code into your code.

import android.view.ViewGroup.LayoutParams;
+1
source

Source: https://habr.com/ru/post/1786347/


All Articles