Unable to use variable defined inside if statement
<p>I'm trying to do something like this in android:</p>

<pre><code>// 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);
</code></pre>

<p>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!</p>

<p>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.</p>

<p>So how can I achieve this or something similar?</p>

<p><strong>EDIT:</strong><br>
As I guessed, and this was confirmed in the comments, this is a matter of scope. Ok, got it.</p>

<p>Thanks for your help guys.</p>