Open Url in webView in android - java

How to open this link in webView. This is running on browser but it does not run in webView android. Please give solution.

Activity:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
And your XML:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Check this out,
it has worked for me...but i dont know whether it got any side effects or not but works fine with me
super.onCreate(savedInstanceState);
WebView theWebPage = new WebView(this);
theWebPage.getSettings().setJavaScriptEnabled(true);
theWebPage.getSettings().setPluginState(PluginState.ON);
setContentView(theWebPage);
theWebPage.loadUrl("http://www.hkmytravel.com");
Source can be found in [http://www.androidpanna.com/functionality/webview-open-url-webpage-within-the-android-app-when-launch-android-development]

Test the following:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

works for me
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com/");
XML
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
add in manifest
<uses-permission android:name="android.permission.INTERNET"/>

Related

Not able to view webpage using webview

this is the xml file for the webview
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="com.example.testing.WebViewExample">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this code is for Webview.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("https://www.google.com/");
}
}
But the problem is I am not able to view the google and it show an error as webpage not available
the webpage at https://www.google.com/ could not be loaded because:
net::ERR_CACHE_MISS
You may need to add the INTERNET permission to your manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
Another Reference: loadUrl() in Android Webview fails with net::ERR_CACHE_MISS

Kill chrome process

I created a new project in Android. We use the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://t.co/YoHe3NKrJH");
He is opening Chrome.
I need to close it automatically after 10s.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Multiple Log-Ins on Separate WebViews? (Android)

I'm fairly new to Android development, and I have a question about how WebViews handle data (in Java).
I'm assuming this would fall under the 'cookie' category. But what I have is two different webViews on different tabs of my application. I would like for one webView (call it webView1) to be logged into one account of a website, while the other (webView2) to be logged into another account of the same website. For example, I would want to log into two separate Gmail accounts within the two webViews at the same time.
The problem I am having is that once I log into an account on webView1, webView2 follows suit and logs me into that account. The same problem happens when I log into webView2, as webView1 logs into that account also naturally.
Is there any way to get around this? I want my two webViews to act completely independently from one another is what it comes down to.
Thanks!
I can definitely answer your question. I am mainly an iOS developer and I am creating an Android app with the exact opposite effect of webView cookies not sharing. I created my webviews in tabs to simulate the behavior I use in iOS, but my webViews are acting completely independant of one another. I want them to share the same login info, but as it stands I must login to each tab individually because they aren't playing nice.
Either way, my problem can definitely help you and I would hope that you share your info with me to create the cookie sharing effect I am searching for...
Here is my main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/web_engine"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/messages"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/myprofile"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/rncorner"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
</FrameLayout>
</LinearLayout>
</TabHost>
Here is my Activity:
package com.example.tabs;
import android.app.TabActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class TabsActivity extends TabActivity {
WebView webView;
final String DEFAULT_URL = "http://example.com";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home",getResources().getDrawable(R.drawable.home)).setContent(R.id.web_engine));
mTabHost.addTab(mTabHost.newTabSpec("Messages").setIndicator("Messages",getResources().getDrawable(R.drawable.messages)).setContent(R.id.messages));
mTabHost.addTab(mTabHost.newTabSpec("My Profile").setIndicator("My Profile", getResources().getDrawable(R.drawable.myprofile)).setContent(R.id.myprofile));
mTabHost.addTab(mTabHost.newTabSpec("Map").setIndicator("Map", getResources().getDrawable(R.drawable.rncorner)).setContent(R.id.rncorner));
mTabHost.setCurrentTab(0);
//home
webView = (WebView)findViewById(R.id.web_engine);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(DEFAULT_URL);
//messages
webView = (WebView)findViewById(R.id.messages);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php2");
//my profile
webView = (WebView)findViewById(R.id.myprofile);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php3");
//rncorner
webView = (WebView)findViewById(R.id.rncorner);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php4");
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
}

Android flash magazine application

Is it possible to create a magazine based on Android embedding flash in it?
As your question is very wilde and generic, I can only say "yes that's possible"!
Here is a link to embed your swf file into an Android application.
Load an SWF into a WebView
public class Test3Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="file:///android_asset/qualibus.swf";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
}
}
main.xml:
<?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 android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
Result:

ViewFlipper w/ WebView includes

I am creating a ViewFlipper that flips thru WebViews: I have no problems running the app if I place the WebViews within the main.xml. Since I will be using a multiple count of Web views, I decided to break them up into separate XML files. When I do this using the include android:id="#+id/myWebView001" layout="#layout/pg001" within the ViewFlipper of the main.xml, I get an force close when the app starts.
Please look thru the following code and if you have any suggestions for this to work correctly, it will greatly appreciate it. Thnx again!!
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewFlipper"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<include android:id="#+id/myWebView001" layout="#layout/pg001" />
</ViewFlipper>
main.java:
package com.aero.ac4313;
import android.app.Activity;
import android.os.Bundle;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your content view, this will be your layout
setContentView(R.layout.main);
}
}
pg001.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myWebView001" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Pg001.java:
package com.aero.ac4313;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Pg001 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your content view, this will be your layout
setContentView(R.layout.pg001);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.myWebView001);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/pg001.html");
}
}
The error is obviously simple. Your main activity class is null. I believe that he added Pg001.class without linking it with the main activity class. If you did add it on the manifest file then try again.

Categories

Resources