0

I'm trying to do something like this in android:

// If the parent is a viewpager
if (parentIsViewPager)
{
    // Retrieve the view's layout informations specific to viewpagers
    ViewPager.MarginLayoutParams marginLayoutParams = (ViewPager.MarginLayoutParams) getLayoutParams();
}
// If the parent is not a viewpager (mainly a viewgroup)
else
{
    // Retrieve the view's layout informations for a viewgroup
    ViewGroup.MarginLayoutParams marginLayoutParams = getLayoutParams();
}

Log.i("Test","marginLayoutParams.leftMargin = " + marginLayoutParams.leftMargin);

Unfortunately, the IDE (Android Studio 1.5.1) tells me it cannot find declaration for layoutParams in the last line above... But I declared it in the if statements!

I guess there is something about scope in here but as the following code in my project is really big, I cannot duplicate it in each if statement.

So how can I achieve this or something similar?

EDIT:
As I guessed, and this was confirmed in the comments, this is a matter of scope. Ok, got it.

Thanks for your help guys.

3 Answers 3

1

A variable declaration is limited to it's scope. In this case the if-else statement.

You could do it like this:

// Declare in correct scope and define later...
ViewGroup.LayoutParams layoutParams;

// If the parent is a viewpager
if (parentIsViewPager)
{
    // Retrieve the view's layout informations specific to viewpagers
    layoutParams = (ViewGroup.LayoutParams) getLayoutParams();
}
// If the parent is not a viewpager (mainly a viewgroup)
else
{
    // Retrieve the view's layout informations for a viewgroup
    layoutParams = (ViewGroup.LayoutParams) getLayoutParams();
}

Log.i("Test","layoutParams.width = " + layoutParams.width);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that's what I guessed, but I cannot do it like that as the layoutParams is not from the same classes in my if statement. What you wrote gave me a runtime error as ViewPager.LayoutParams is different from ViewGroup.LayoutParams despite the fact the ViewPager is derived form ViewGroup (thanks Google!).
If you only need the methods declared in ViewGroup.LayoutParams class then use ViewGroup.LayoutParams. I fixed the code above.
Hehe! Nope! This rises an incompatible type conversion! This is why I needed the if statement... and why I thanked Google above lol. I though it will be that easy too as they are derived from each other, but nope... Too easy! We need challenges in our lives, right?!
What kind of incompatible type conversion and where? It should be possible to cast a derived class to it's super class...
Oups my bad I confused LayoutParams with MarginLayoutParams! I'm so sorry, but the problem is real, simply replace LayoutParams by MarginLayoutParams in my question... I will edit my OP.
1

This compilation error has nothing to do with android or the IDE, is just the following:

if (parentIsViewPager)
{
    // Retrieve the view's layout informations specific to viewpagers
    ViewPager.LayoutParams layoutParams = (ViewPager.LayoutParams) getLayoutParams();
}

in this condition, the "Variable" layoutParams has a Scope: only inside the if condition, the second part:

else
{
    // Retrieve the view's layout informations for a viewgroup
    ViewGroup.LayoutParams layoutParams = getLayoutParams();
}

the same criteria applies..

the error is because you are trying to use an object out of its scope... layoutParams is not avaliable anymore outside that if condition

5 Comments

Yep it's what I guessed... Thank you for the explanation. Do you have a solution to avoid this problem?
Yes, move the declaration over the if condition so the object can be "visible" inside that method, OR place it as a class variable....
I cannot move the declaration above the if statement as the variable is one class or another depending on the if statement... What do you mean by "place it as a class variable"? Do you mean a global variable inside the class? Unfortunately, the problem will be exactly the same...
A viewPager is an inherited class from the ViewGroup... so a there you have it
Well, see my other answers in comments, unfortunately, the 2 classes aren't compatible despite the fact they inherit from each other... I don't understand that neither...
1

Is simply duplicating the Log statement on both sides of the if-else not an option or is layoutParams referenced else where?

// If the parent is a viewpager
if (parentIsViewPager)
{
    // Retrieve the view's layout informations specific to viewpagers
    ViewPager.LayoutParams layoutParams = (ViewPager.LayoutParams) getLayoutParams();
    Log.i("Test","layoutParams.width = " + layoutParams.width);
}
// If the parent is not a viewpager (mainly a viewgroup)
else
{
    // Retrieve the view's layout informations for a viewgroup
    ViewGroup.LayoutParams layoutParams = getLayoutParams();
    Log.i("Test","layoutParams.width = " + layoutParams.width);
}

1 Comment

Thanks, this is a solution. But as I wrote, my real code is really big after the if statement... So I would avoid it... Anyway, I edited my OP so take a look now and I will post the link in there in a few.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.