Question

i want the onitemclicklistener to open HTML pages stored in assets folder and each activity should open in a WebView , i don't want to have so many activities and so many web views

so here is my onitemclicklistener

list1.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
           if(position==0)
           {
           Intent myIntent = new Intent(MainActivity.this,Activity1.class);
           startActivity(myIntent);
           } 
          if(position==1)
           {
           Intent myIntent = new Intent(MainActivity.this,Activity2.class);
           startActivity(myIntent);
           } 
          if(position==2)
           {
           Intent myIntent = new Intent(MainActivity.this,Activity3.class);
           startActivity(myIntent);
           } 


}
});
}
}
Was it helpful?

Solution

In List item click.

Intent myIntent = new Intent(MainActivity.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);

In WebviewActivtiy

private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web); // webview in activity_web.xml
int pos = getIntent().getIntExtra("key", 0);
webView = (WebView) findViewById(R.id.wv); // initialize once
webView.setJavaScriptEnabled(true);
if(pos==0)
{ 
webView.loadUrl("file:///android_asset/2.html");
}

OTHER TIPS

Do you try with this.

private WebView YourWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_web);

    YourWebView = (WebView) findViewById(R.id.YourWebView);


    WebSettings wset = webviewMsg.getSettings();
    wset.setJavaScriptEnabled(true);

    YourWebView.loadUrl(YourURL);

     YourWebView.setWebViewClient(new WebViewClient() {
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
    }

        @Override
        public void onPageFinished(WebView view, String url) {

        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        }
    });
}

I hope you help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top