Android: Force updating layout

Angrybambr picture Angrybambr · Nov 18, 2011 · Viewed 7.7k times · Source

I'm trying to change background color of ImageView, but it updates only after I click on it. I tried to do layout.invalidate(), layout.postInvalidate() where layout is my root element in layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color_select_page_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/color_select_page_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="10px"
        android:paddingBottom="10px"
        android:text="Установки цвета:"
        android:textAppearance="?android:attr/textAppearanceMedium" />



    <LinearLayout
        android:id="@+id/color_select_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >

        <TextView
            android:id="@+id/color_select_simple_text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Цвет фона:"
            android:paddingRight="20px"
            android:layout_gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/color_select_background_color"
            android:layout_width="170sp"
            android:layout_height="30sp"
            android:background="#80111111"
             />
    </LinearLayout>


            <TextView
                android:id="@+id/color_select_titles_text_color"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10px"
                android:layout_marginTop="10px"
                android:gravity="center"
                android:text="Цвет списка юзеров"
                android:textSize="30dp" />


            <Button
                android:id="@+id/color_select_buttons_background_color"
                android:paddingTop="20px"
                android:paddingBottom="20px"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Цвет фона кнопок" />

            <Button
                android:id="@+id/color_select_buttons_text_color"
                android:layout_marginTop="10px"
                android:paddingTop="20px"
                android:paddingBottom="20px"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Цвет текста кнопок" />

             <TextView
                android:id="@+id/color_select_pages_titles_text_color"
                android:layout_marginTop="10px"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Цвет заголовков страниц"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/color_select_podzagolovki_text_color"
                android:layout_width="fill_parent"
                android:gravity="center"
                android:layout_marginTop="10px"
                android:layout_height="wrap_content"
                android:text="Цвет подзаголовков страниц"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


            <TextView
                android:id="@+id/color_select_phone_numbers_text_color"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10px"
                android:gravity="center"
                android:text="Цвет телефонных номеров" />

            <TextView
                android:id="@+id/color_select_all_other_text_color"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10px"
                android:gravity="center"
                android:text="Цвет остального мелкого текста" />

            <EditText
                android:id="@+id/color_select_edit_field_text_color"
                android:layout_marginTop="10px"
                android:text="Цвет текста полей ввода"
                android:layout_width="fill_parent"
                android:editable="false"
                android:layout_height="wrap_content" >
        </EditText>

            <EditText
                android:id="@+id/color_select_edit_field_text_color"
                android:layout_marginTop="10px"
                android:text="Цвет фона полей ввода"
                android:editable="false"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
        </EditText>

            <Button
                android:id="@+id/color_select_save_changes"
                android:layout_marginTop="30px"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Сохранить изменения" />
           <Button
                android:id="@+id/color_select_cancel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Отмена" />    

            <Button
                android:id="@+id/color_select_restore_defaults"
                android:layout_marginTop="20px"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Восстановить умолчания" />


</LinearLayout>
</ScrollView>    

So.. I don't know what to do. Could anyone help me with advice? Thanks in advance.

Answer

js- picture js- · Nov 18, 2011

Try setting the visibility of your ImageView to invisible and then to visible again. This is not a nicve solution but it worked for me in times where invalidate(), requestLayout() and such do not work anymore.