how to put a progress bar in an application using tabs - android

I have a main java file that sets up a series of tabs where each one calls a seperate activity in this case each activity is linked to a webpage. I am trying to implemente a progress bar in the tab activity that will show a progress bar for when a link is clicked on regardless of what tab is currently selected. I have a progress bar that i can use for a single activity but i cant get it to work for the tabs
i have the progress bar defined as
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.main );
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setProgress(progress * 100);
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
Which goes after oncreate()
Here is the main Java File
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TabHost;
import android.widget.TextView;
public class UniversityofColorado extends TabActivity{
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost host=getTabHost();
Resources res = getResources();
host.addTab(host.newTabSpec("one")
.setIndicator("",res.getDrawable(R.drawable.ic_tab_google))
.setContent(new Intent(this, Hello.class)));
host.addTab(host.newTabSpec("two")
.setIndicator("Colorado Main Site")
.setContent(new Intent(this, ColoradoMainSiteBrowser.class)));
host.addTab(host.newTabSpec("three")
.setIndicator("CULearn")
.setContent(new Intent(this, CULearnBrowser.class)));
host.addTab(host.newTabSpec("four")
.setIndicator("CULink")
.setContent(new Intent(this, CULinkBrowser.class)));
host.addTab(host.newTabSpec("five")
.setIndicator("MyCUInfo")
.setContent(new Intent(this, MyCUInfoBrowser.class)));
host.addTab(host.newTabSpec("six")
.setIndicator("", res.getDrawable(R.drawable.ic_tab_map))
.setContent(new Intent(this, CampusBrowser.class)));
host.addTab(host.newTabSpec("Seven")
.setIndicator("",res.getDrawable(R.drawable.ic_tab_notes))
.setContent(new Intent(this, NotesList.class)));
host.addTab(host.newTabSpec("eight")
.setIndicator("", res.getDrawable(R.drawable.ic_tab_help))
.setContent(new Intent(this, CampusBrowser.class)));
}
Here is one of the activities that the main java file calls
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class CampusBrowser extends Activity {
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://amath.colorado.edu/department/Maps/bldgs.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
If you could help me figure this out it would be appreciated

Hey this is exactly what I do in my webviews
In the onCreate method of the webview[in your case it may be your tabActivity] do this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.webview_simple);
}
then in the webview client do this
private class MyWebViewClient extends WebViewClient {
public synchronized boolean shouldOverrideUrlLoading(WebView view, String url){
}// and all ur implementation
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
setProgressBarIndeterminateVisibility(false);
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setProgressBarIndeterminateVisibility(true);
super.onPageStarted(view, url, favicon);
}
}
Hope it helps. I am overriding the onPageFinished and onPageStarted in the webview client. Its difficult to do it from onProgressChanged. I tried it but I found this to be simple.

Related

Android WebView when refresh it always goes to home page

I was try to create a android webview with the refresh option.
Whenever I pull down for refresh it always goes to the home page.
I have try using the swiperefeshlayout. My code are as follows
activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/destiny"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/progressBar" />
</RelativeLayout>
MainActivity.java file
package com.my.project;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
WebAction();
}
});
WebAction();
}
public void WebAction(){
myWebView = (WebView)findViewById(R.id.destiny);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
myWebView.loadUrl("http://myurl");
swipe.setRefreshing(true);
myWebView.setWebViewClient(new WebViewClient());
AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this); //pass the activity in constructure
appUpdateChecker.checkForUpdate(false); //mannual check false here
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onReceivedError(WebView view, int errorCode, String description, String fallingUrl) {
myWebView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
progressBar.setVisibility(View.VISIBLE);
setTitle("Loading.....");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url){
progressBar.setVisibility(View.GONE);
setTitle(view.getTitle());
super.onPageFinished(view, url);
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
With this code the refresh always goes to the home page
but I want to refresh the webview on the same page itself.
You are setting all variables and actions again onRefresh() you only need to load current url of WebView again. So you need to change your OnRefreshListener like below;
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myWebView.loadUrl(myWebView.getUrl());
}
});

Webview in Fragment causes crash

Ok so I've been working on an app in Android Studio for school. I have a webview inside of a fragment. But in the app, when ever I open the fragment on my phone, the app crashes and my phone says "Unfortunately, LARKer App has stopped."
Here's the Fragment class:
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Andrew on 12/29/2014.
*/
public class menu4_Fragment extends Fragment {
private WebView mWebView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu4_layout,container,false);
mWebView = (WebView) getView().findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("foo");
mWebView.setWebViewClient(new MyWebViewClient());
return rootview;
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().contentEquals("foo")) {
// This is my website, so do not override; ler my WebView load the page
return false;
}
//Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
And here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LARK: Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:textSize="50dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_below="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here's an image of what I got on my phone:
Change this code
mWebView = (WebView) getView().findViewById(R.id.webView);
Like this, it will work
mWebView = (WebView) rootview.findViewById(R.id.webView);
You have to get the view's from your View Group.otherwise it will return error.
change this on your oncreateview() methed of your fragmanet
mWebView = (WebView) rootview.findViewById(R.id.webView);
Try this code,
WebView webViewInfo = (WebView) findViewById(R.id.webview);
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("LOAD_YOUR_URL", "text/html", "UTF-8");
And WebViewClient class implement on webview method.
private class MYWEBCLIENT extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On keyboard back handle,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewInfo.canGoBack()) {
webViewInfo.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I hope this code help for you...!!

Spinner Appearing But not Disappearing

I am trying to implement a progress bar, that appears when loading the page and disappears when the page is finished loading. I have added on finish parameter but it seems that it is not being recognized.
Code
package com.test;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.PushService;
public class MainActivity extends Activity implements OnClickListener {
private Button push;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "onReceive invoked!", Toast.LENGTH_LONG).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setContentView(R.layout.activity_main);
PushService.setDefaultPushCallback(this, MainActivity.class);
WebView webView = (WebView) findViewById(R.id.webView1);;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.teqez.com/xx/ ");
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setBuiltInZoomControls(false);
push = (Button)findViewById(R.id.senPushB);
push.setOnClickListener(this);
}
private class HelloWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url){
webview.loadUrl(url);
setProgressBarIndeterminateVisibility(true);
return true;
}
#Override
public void onPageFinished(WebView webview, String url){
super.onPageFinished(webview, url);
setProgressBarIndeterminateVisibility(false);
}
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (true);
}
}
#Override
public void onBackPressed()
{
WebView webView = (WebView) findViewById(R.id.webView1);
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
}
#Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(MyCustomReceiver.intentAction));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
JSONObject obj;
try {
obj = new JSONObject();
obj.put("alert", "hello!");
obj.put("action", MyCustomReceiver.intentAction);
obj.put("customdata","My message");
ParsePush push = new ParsePush();
ParseQuery query = ParseInstallation.getQuery();
// Push the notification to Android users
query.whereEqualTo("deviceType", "android");
push.setQuery(query);
push.setData(obj);
push.sendInBackground();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Layout
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/senPushB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"
android:text="Check For Updates" />
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:focusable="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
You do not invoke your custom private class
Change
webView.setWebViewClient(new WebViewClient());
to
webView.setWebViewClient(new HelloWebViewClient());

WebView inside application?

So I have a WebView inside of a LinearLayout with an EditText Box and a Button to make the application search for the web. When I click go on my button it takes me to the stock android browser. how do I make It so that it stays within my WebView when I click go?
here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="#+id/web_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"/>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/go"
android:gravity="right"/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
here is my source:
package straightapp.com.Browser;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class Browser extends Activity {
WebView mWebView;
Button button;
EditText text;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.straightapp.com");
text=(EditText)findViewById(R.id.web_address);
button=(Button) findViewById(R.id.go);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
openBrowser();
}
});
text.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_ENTER){
openBrowser();
return true;
}
return false;
}
});
}
public void openBrowser(){
Uri uri=Uri.parse(text.getText().toString());
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks
When I click go on my button it takes me to the stock android browser.
That's because that is what you told Android to do. startActivity() starts an activity.
how do I make It so that it stays within my WebView when I click go?
Call loadUrl() on your WebView instead of calling startActivity().
To be precise, you can try following change:
public void openBrowser(){
//Ensure that URL entered is a valid one
mWebView.loadUrl(text.getText().toString());
}
Change your xml file as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Browser"> <!--Just add this line -->
<EditText
android:id="#+id/web_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"/>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/go"
android:gravity="right"/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
First declare the webview and progressbar
progressBar = (ProgressBar)findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
Copy and paste this code below onCreate method
public class myWebClient extends WebViewClient {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}

Android app in eclipse

i've searched for days but cant find an answer, perhaps you guys can help.
I'm creating an android app in eclipse, it all works just one thing is bugging me.
this is my main.java:
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.btn_rassen);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.button2);
secondButton.setOnClickListener(this);
}
// Process the button click events
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_rassen:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.test.Webscreen.URL,
"http://www.google.com/");
startActivity(j);
break;
case R.id.button2:
Intent k = new Intent(this, Webscreen.class);
k.putExtra(com.test.Webscreen.URL,
"http://notworkingurltotest.com");
startActivity(k);
break;
}
}
}
now when it calls the webview.java the page called shows up but not the buttons i created in the layout xml page. does anybody have any idea why this is?
your help is much appreciated!
ohw this is my webscreen.java
package com.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Webscreen extends Activity {
public static final String URL = "";
private static final String TAG = "WebscreenClass";
private WebView webview;
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webscreen);
this.getIntent().getExtras();
this.webview = (WebView) findViewById(R.string.webview);
String turl = getIntent().getStringExtra(URL);
Log.i(TAG, " URL = "+turl);
WebView webview = new WebView(this);
setContentView(webview);
final Activity activity = this;
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Bezig met laden...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Intent myIntent = new Intent();
myIntent.setClassName("com.test", "com.test.Main");
startActivity(myIntent);
Toast.makeText(activity, "Laden van onderdeel mislukt, probeer het later nog eens! ", Toast.LENGTH_LONG).show();
progressDialog.show();
}
});
webview.loadUrl(turl);
}
}
webscreen.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- <1> -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="#+id/url" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:lines="1"
android:layout_weight="1.0" android:hint="http://"
android:visibility="visible" />
<Button android:id="#+id/go_button" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="go_button" />
</LinearLayout>
<!-- <2> -->
<WebView
android:id="#string/webview"
android:layout_width="fill_parent"
android:layout_height="0dip"
/>
</LinearLayout>
Looks like you're creating a second webview and then setting that as the content view, so your R.layout.webscreen is replaced.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webscreen);
this.getIntent().getExtras();
this.webview = (WebView) findViewById(R.string.webview);
String turl = getIntent().getStringExtra(URL);
Log.i(TAG, " URL = "+turl);
**WebView webview = new WebView(this);
setContentView(webview);**
.....
Edit:
I've just noticed something in your code, should the line that reads:
this.webview = (WebView) findViewById(R.string.webview);
Acutally be:
this.webview = (WebView) findViewById(R.**id**.webview);
Edit 2:
Just noticed another thing while I was making the project. The following in webscreen.xml:
android:id="#string/webview"
Should be:
android:id="#+id/webview"
Edit 3:
And another thing noticed in webscreen.xml:
android:layout_height="0dip"
Sure you want a 0 height?? ;-)

Categories

Resources