Ponmalar
Ponmalar

Reputation: 7031

Load url in Webview after few seconds

I'm creating one application in Android. It has one layout named main.xml

main.xml:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splashscreen" />

mainActivity.java:

public class MainActivity extends WebViewActivity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int wwidth = displaymetrics.widthPixels;
        setContentView(R.layout.main);
        final ImageView imgview=(ImageView)findViewById(R.id.imageView1);
        imgview.setLayoutParams(new LinearLayout.LayoutParams(wwidth, 
                height, 0.0F));
        int DELAY = 6000;

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {            
            public void run()
            { 
                WebViewActivity.loadurl("file:///android_asset/www/index.htm");                
            }
        }, DELAY);

    }
}

Initially want to load main.xml, after 6 seconds I want to load url in webview. WebViewActivity has codes for loading...

I have followed the above coding...

Result: Initially main.xml is loading perfectly... but after 6 seconds loadurl method is called but main.xml is not removed. How to do this?

Note: WebViewActivity has layout with webview.

Upvotes: 3

Views: 5259

Answers (4)

Ponmalar
Ponmalar

Reputation: 7031

I have followed the below coding... I did not used main.xml page... directly loaded in webview... Working good :-)

public class MainActivity extends WebViewActivity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebViewActivity.loadurl("file:///android_asset/www/splashscreen.png");
        int DELAY = 6000;

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {            
            public void run()
            { 
                 WebViewActivity.loadurl("file:///android_asset/www/index.htm");
            }
        }, DELAY);

    }
}

Upvotes: 3

Avi Kumar
Avi Kumar

Reputation: 4433

You can do it like this 

// first add a progress dialog

ProgressDialog dialog ; 

//Initialize in OnCreate

dialog = new ProgressDialog(this);

//and call the following function 

        display();

private void display() {
// TODO Auto-generated method stub
new CountDownTimer(6000, 1000) {

public void onTick(long millisUntilFinished) {
// mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
dialog.show();
}

public void onFinish() {

dialog.dismiss();
}
}.start();

}

Upvotes: 0

DroidBender
DroidBender

Reputation: 7902

Your mainActivity uses setContentView(layout) to define what XML related to this Activity. Your main.xml only includes an Image which is shown correctly. Loading an URL in your WebViewActivity and leave it that way, will not do much because it is not shown

Solutions:

1) Start a new WebViewActivity:

handler.postDelayed(new Runnable() {            

        public void run()
        { 
            Intent myIntent = new Intent(this, WebViewActivity.class);
            startActivity(myIntent);
            finish();
        }
    }, DELAY); 

Create an XML for your WebViewActivity including a WebView In the OnCreate:

 myWebView = (WebView) findViewById(R.id.myWebView);
 myWebView.loadurl("file:///android_asset/www/index.htm"); 

Note: mainActivity does not have to extends from your WebViewActivity in this case

2) setVisibility()

main.xml

<ImageView />
<WebView 
       id:myWebView
       visibility:invisible />

In the OnCreate you can already load your URL into your WebView. The Handler will simply make the View visible when you want.

handler.postDelayed(new Runnable() {            
        public void run()
        { 
            myWebView.setVisibility(View.VISIBLE);
        }
    }, DELAY);

Upvotes: 0

StealthOne
StealthOne

Reputation: 195

you should have a webview view in your main.xml...

or you make anoher layout xml and use setContentView(R.layout.anotherlayoutxml);

Upvotes: 0

Related Questions