user1487104
user1487104

Reputation: 1

Android: setVisibility not setting View to visible

I'm using minSDK = 8 and targetSDK = 17

At activity start, my 3 screens (3 different Views) are set to invisible in the XML layout. In onResume, I call setScreen(1), which starts an animation that sets View 'wait' to visible. However, at line 34 where 'wait' is set to visible, nothing happens. If I remove the comment // from line 43 where 'main' is set to invisible, 'wait' becomes visible.

Why does 'wait' not appear when line 43 is commented out? Why does it take setting the already invisible 'main' View to invisible for 'wait' to appear?

public class TestActivity extends Activity {
static final String TAG = "test";
public Context context;
Activity myActivity;
UserFunctions userFunctions;
Button but_begin;
int status;
View main;
View wait;
View noconnect;
Animation fadeIntoWait;
Animation fadeIntoConnect;
Animation fadeIntoMain;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.test);
    context = getApplicationContext();
    myActivity = this;

    main = findViewById(R.id.screen_main);
    wait = findViewById(R.id.screen_wait);
    noconnect = findViewById(R.id.screen_connect);

    fadeIntoWait = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    fadeIntoConnect = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    fadeIntoMain = AnimationUtils.loadAnimation(this, R.anim.fade_in);

    fadeIntoWait.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            wait.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //main.setVisibility(View.INVISIBLE);
            //noconnect.setVisibility(View.INVISIBLE);

        }
    });
    fadeIntoConnect.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

            noconnect.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            wait.setVisibility(View.INVISIBLE);
        }
    });
    fadeIntoMain.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

            main.setVisibility(View.VISIBLE);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            wait.setVisibility(View.INVISIBLE);
        }
    });

}

@Override
public void onStart() {

    super.onStart();

}

@Override
public void onResume() {
    super.onResume();

    setScreen(1);

}

public void setScreen(int screen) {

    switch (screen) {
    case 1: // into wait
        wait.startAnimation(fadeIntoWait);

        break;
    case 2: // to noconnect
        noconnect.startAnimation(fadeIntoConnect);

        break;

    case 3: // to main
        main.startAnimation(fadeIntoMain);

        break;
    }
}

public void gotoActivity(Class<?> s) {
    Intent g = new Intent(getApplicationContext(), s);

    // Close all views before launching Dashboard
    g.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(g);
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}

public void retryConnect(View view) {

}

public void begin(View view) {
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 1, 0, "Exit App");
    menu.add(0, 2, 0, "Close Menu");
    menu.add(0, 3, 0, "Dashboard");

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case 1: // log out
        closeOptionsMenu();
        return true;
    case 2: // Exit App
        finish();
        return true;
    case 3: // Exit App
        gotoActivity(DashboardActivity.class);
        return true;

    }
    return super.onOptionsItemSelected(item);

}

public void menu(View v) {

    openOptionsMenu();

}

}

Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:layout_alignParentTop="true"
    android:background="@drawable/line" >
</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/header" >

    <RelativeLayout
        android:id="@+id/screen_wait"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/img_wait"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:contentDescription="@string/wait"
            android:src="@drawable/wait"
             />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/screen_connect"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/img_connect"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:contentDescription="@string/connect"
            android:onClick="retryConnect"
            android:src="@drawable/connect"
            />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/screen_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:visibility="gone" >

        <ScrollView
            android:id="@+id/middle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/footer"
            android:background="#2F2430" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#2F2430" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="@string/idea"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#21dbd4" />

                <Button
                    android:id="@+id/but_begin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/TextView01"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="67dp"
                    android:onClick="begin"
                    android:text="Begin" />
            </RelativeLayout>
        </ScrollView>

        <RelativeLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/line"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Pennies:" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="22dp"
                android:layout_toRightOf="@+id/textView2" />

            <Button
                android:id="@+id/btnMenu"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:onClick="menu"
                android:text="Menu" />
            </RelativeLayout>
        </RelativeLayout>
    </FrameLayout>

</RelativeLayout>

Upvotes: 0

Views: 1299

Answers (1)

Simon
Simon

Reputation: 14472

Your UI is not laid out until after onCreate(). Try moving the animations to onStart() or better, use a ViewTreeObserver to start them once layout is complete

Upvotes: 2

Related Questions