Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does LinearLayout instance.getLayoutParams look to have a wrong class?

Tags:

java

android

If I declare LinearLayout linearLayout and look at linearLayout.getLayoutParams(), it gives me ViewGroup.LayoutParams, not LinearLayout.LayoutParams.

So I have to use the repeating (and thus bad) style construction of:

int lm = ((LinearLayout.LayoutParams) linearLayout.getLayoutParams()).leftMargin?

Do I really have to use it, if I want to reach margins, for example?

Is it my misunderstanding of Android or Java, or both or something else?

like image 240
Gangnus Avatar asked Jan 20 '12 13:01

Gangnus


4 Answers


Just to put it in a very straight way:

Please keep in mind that when you get the LayoutParams of a view, their class is determined by the class of the parent view of this view.

This implies one very important fact:

You have to cast the LayoutParams of a View because you can not know beforehand what is the class of the parent. So, if view's parent is a LinearLayout, it's LayoutParams will be of type LinearLayout.LayoutParams and STILL you have to cast it to this type!


I am not sure of the source code and how it goes. But it is very logical since RelativeLayout.LayoutParams, for example has properties such as ABOVE and BELOW which are not found in a LinearLayout.LayoutParams. And these params are actually dependent on the parent.

like image 81
Sherif elKhatib Avatar answered Nov 18 '22 00:11

Sherif elKhatib


I think your understanding about 'LayoutParams' is incorrect. A view's (or layout's) LayoutParams must be an instance of 'the parent view's LayoutParams'.

For example, here's an LinearLayout in RelativeLayout. That LinearLayout's LayoutParams must be RelativeLayout.LayoutParams. This thing enables us to set the attributes properly, like 'centerInParent' and etc.

When you call getLayoutParams(), that returns various instance of LayoutParams that matches to parent ViewGroup type. As a result, we need to cast the type of a view's LayoutParams repeatedly when calling getLayoutParams().

like image 23
lulumeya Avatar answered Nov 18 '22 00:11

lulumeya


You don't cast LinearLayout.LayoutParams to LinearLayout.LayoutParams. That makes no sense. Also it's not clear if linearLayout in your code is an instance variable... Either way, you can simply reference LayoutParams inside of a View that extends from some type of View to get that View's LayoutParams. For example, if you're inside of a class that extends from RelativeLayout, then

LayoutParams params = new LayoutParams(200, LayoutParams.WRAP_CONTENT);

gets you RelativeLayout.LayoutParams. Otherwise, you'll need to qualify the LayoutParams you're looking for. To get/apply LinearLayout LayoutParams, you simply need

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams();

EDIT

linearLayout.getLayoutParams() returns the layoutParams that you supplied when you called setLayoutParams, so first make sure you do that. Then once you've done that, you simply need to cast the LayoutParams that were returned to the LayoutParams you set it to. so

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams() 

Also, why do you need to get the params like this? What are you looking to accomplish ultimately? I ask because there may be an easier way to do what you're trying to do...

In effect, the answer to your question is YES, you need to cast the LayoutParams you get from getLayoutParams because that's a method derived from the View class which returns the superclass of LayoutParams which is ViewGroup.LayoutParams which, because of polymorphism, can be casted to the type you know it to be.

like image 2
LuxuryMode Avatar answered Nov 17 '22 22:11

LuxuryMode


Are you using Eclipse and just using the Eclipse 'Organise Imports' option to import Android libraries? If so, there's a few different libraries called LayourParams, Eclipse seems to be importing the framelayout one and not the linearlayout one like you need.

So just change the imports at the top of your class.

like image 1
georgiecasey Avatar answered Nov 18 '22 00:11

georgiecasey