Android: TextView.setLayoutParams sometimes work, sometimes doesn't
<p>I have an android app that let's the user modify some layout parameters. One of my functions let's the user decide if a TextView will be aligned against the top or the bottom of a picture.</p>

<p>This is the function:</p>

<pre><code>private void setAlign(String align) {

    /* Get Preferences */
    SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
    SharedPreferences.Editor editor = prefs.edit();

    editor.putString("align", align);
    editor.commit();

    Log.d("ALIGN", align);

    paramAlign = align;

    FrameLayout.LayoutParams floLP = new FrameLayout.LayoutParams(
                                            FrameLayout.LayoutParams.WRAP_CONTENT,
                                            FrameLayout.LayoutParams.WRAP_CONTENT,
                                            (align == "TOP") ? Gravity.TOP : Gravity.BOTTOM);

    txtGoal.setLayoutParams(floLP);

    int res = paramAlign == "TOP" ? R.drawable.btn_toolbar_align_top_up : R.drawable.btn_toolbar_align_bottom_up ;
    btnAlign.setImageResource(res);

}
</code></pre>

<p>Now once the activity is started, this function works fine. However, when I initialize the activity, I call the setAlign() function in the onGlobalLayout method after retrieving the alignment preference.</p>

<p>This is the relevant code:</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personalize);

    /* Get Preferences */
    SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);

    paramAlign = prefs.getString("align", "BOTTOM");

    Log.d("ALIGN", paramAlign);

    // Get screen dimensions and initialize preview
    ViewTreeObserver vto = rootView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

        setAlign(paramAlign);

        ViewTreeObserver obs = rootView.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
        }

    });

}
</code></pre>

<p>Now if you notice the logging functions, they both return "TOP" when I start the activity. And the setAlign() function is obviously getting called. Yet, the TextView is aligned at the bottom. This is the XML for the TextView:</p>

<pre><code>&lt;TextView
                android:id="@+id/txtGoal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:textColor="@color/color_white"
                android:textSize="8sp"
                android:shadowColor="@color/color_black"
                android:shadowDx="1.2"
                android:shadowDy="1.2"
                android:shadowRadius="1.2"
                /&gt;
</code></pre>

<p>Any idea why the setLayoutParams is not happening when the activity is created? The function is getting fired when the layout is done being drawn so it shouldn't be the issue here. And the XML has no gravity specified to start with.</p>

<p>What am I missing here?</p>
