Víctor Martín
Víctor Martín

Reputation: 3450

Why setVisibility doesn't work on a Fragment/FrameLayout

I have a layout with an Indeterminate Progress Bar, and when passed 3 seconds I want to hide this progress bar and show a webview, but for some reason It doesn't work.

This is my XML:

<FrameLayout 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"
    android:id="@+id/fl_register_oauth"
    tools:context=".fragments.RegisterFragment"
    android:background="@color/f_yellow">

    <LinearLayout 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"
        tools:context=".MainActivity$PlaceholderFragment"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ll_pbar"
            android:gravity="center"
            android:visibility="gone">

        </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateDrawable="@drawable/progress" />

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView"
            android:visibility="gone"
            android:layout_gravity="center" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/FlashBarLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="72dp">
        <!-- flash bar content -->
    </FrameLayout>

</FrameLayout>

And this is my source code of the fragment.

public class RegisterFragment extends Fragment {

...

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_register, container, false);

        bar = (ProgressBar) view.findViewById(R.id.progressBar);

        webView = (WebView) view.findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bar.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        });

I have tried to do it with a Handler with postDelayed, but I've realized that it's not working, eventhough I write it out of the handler this lines.

bar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);

Thank you for your help.

Upvotes: 1

Views: 2429

Answers (1)

westito
westito

Reputation: 86

Try:

((new Handler()).postDelayed(new Runnable() {
    @Override
    public void run() {
       bar.setVisibility(View.GONE);
       webView.setVisibility(View.VISIBLE);
    }
}, 3000);

You don't need to use runOnUiThread. The problem is: your code executed immediately and the activity starts without progress bar showing.

Upvotes: 5

Related Questions