i have a problem with loading a webview from a string url.
here is the code of an activity with only WebView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Intent intent=getIntent();
String url=intent.getStringExtra("url");
//EditText edit=(EditText)findViewById(R.id.editText1);
//String url=edit.getText().toString();
WebView webview=(WebView)findViewById(R.id.webView1);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient());
}`
and code of the calling activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGo(View v)
{
EditText edit=(EditText)findViewById(R.id.editText1);
Intent intent=new Intent(MainActivity.this,WebActivity.class);
intent.putExtra("url", edit.getText().toString());
startActivity(intent);
}
i have even added the android.permissions.INTERNET in the manifest
but still i cannot view the page with the click of the button in MainActivity.
From the comment
yes the real device has the same error with "www.google.com"
Use
http://www.google.com
Also make sure you have internet permission mentioned in manifest file
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Related
I am able to load the website properly in the app but it has a function which redirects users to whatsapp. The api works fine on mobile browser and on PC/laptop. But in the android app it loads for a second and then says webpage unavailable. What am I missing?
Image 1 stays only for a second.
After 1 second loading time :
Main Java Code:
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.loadUrl("https://zzzz/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}
Override this shouldOverrideUrlLoading and do this in it
Code
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
Your code should be like this
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.loadUrl("https://naturesexpress.in/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
}
I have two ImageButton with two different url. When I click the image, it returns an white screen.I don't know what is the problem. Any edit or suggestions are welcome.
Thanks!
main activity
public class main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageButton amazon = (ImageButton)findViewById(R.id.imagebutton1);
ImageButton flipkart = (ImageButton)findViewById(R.id.imagebutton2);
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.amazon.com"));
WebView webview = new WebView(main.this);
setContentView(webview);
startActivity(intent);
}
});
flipkart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Flipkart",Toast.LENGTH_LONG).show();// display the toast on you tube button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.setData(Uri.parse("http://www.flipkart.in"));
startActivity(intent);
}
});
}
my webview activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView htmlWebView = (WebView) findViewById(R.id.webView);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("");
}
class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Do you want to load Url in your app or want to open via External Browser. anaway I'm given the snipped code for the open the url in webview as given below in your application as:
WebView mynews;
ProgressBar pb;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsreadscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pb= (ProgressBar) findViewById(R.id.progress_bar);
mynews= (WebView) findViewById(R.id.mynews);
mynews.getSettings().setJavaScriptEnabled(true);
mynews.getSettings().setDefaultFontSize(17);
mynews.getSettings().setDisplayZoomControls(true);
mynews.getSettings().setDomStorageEnabled(true);
mynews.getSettings().setLoadsImagesAutomatically(true);
mynews.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
pb.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
pb.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pb.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
});
mynews.loadUrl("https://www.google.co.in");
this is the code I have Edited
amazon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click
Intent intent = new Intent(main.this, MainActivity.class);
intent.putExtra("url","http://www.amazon.com");
startActivity(intent);
}
});
get the value on the MainActivity.Class onCreateMethod like below
String url=getIntent().getStringExtra("url");
mynews.loadUrl(url);
I tried my best but it doesn't work for me. I want to open url other than http://google.com in default browser. What code should add inside, I seen android documentation and added the code but it doesn't work. Any suggestion is appreciated.
public class MainActivity extends Activity {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
this.webview = (WebView) findViewById(R.id.activity_main_webview);
webview.loadUrl("http://google.com");
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
}
You can try to this hope this can help you..
public class MainActivity extends AppCompatActivity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("http://gmail.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
try this snippet and make sure you have Internet permission in manifest file
public class MainActivity extends Activity {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.activity_main_webview);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webview.loadUrl("http://google.com/");
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String myAlternativeURL = "http://yahoo.com";
if (!url.equals(myAlternativeURL)) {
view.loadUrl(myAlternativeURL);
return true;
}
}
});
});
Hey guys I want to surf internet from webview in android and i am getting the page for which i have provided url in edittext.But when click on a link inside webview then it is unable to find that page.I have provided internet permission in manifest file and taken web view in layout.
This is my main activity which needs to be corrected somewhere.Thanks in advance and help done will be greatly appreciated.
public class MainActivity extends Activity {
EditText et1;
Button btn;
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.et1);
btn=(Button) findViewById(R.id.btn);
wv=(WebView) findViewById(R.id.wv);
wv.setWebViewClient(new mybrowser());
btn.setOnClickListener(new OnClickListener() {
#SuppressLint("SetJavaScriptEnabled") #Override
public void onClick(View v)
{
String url = et1.getText().toString();
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl("https://"+url);
wv.requestFocus();
}
});
}
class mybrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url1=et1.getText().toString();
view.loadUrl(url1);
return true;
}
}
}
Try to do like
class mybrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
and remove String url1=et1.getText().toString();
I have a WebView that is opening a Url for an article. However, a couple of seconds after opening the link in the WebView, the browser opens up and loads the same article.
This is my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_headline_web_view);
Intent intent = getIntent();
Uri headlineUri = intent.getData();
WebView webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl(headlineUri.toString());
}
Thanks in advance!
What happens if you add:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_headline_web_view);
Intent intent = getIntent();
Uri headlineUri = intent.getData();
WebView webView = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new MyWebViewClient());
webView.loadUrl(headlineUri.toString());
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}