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);
Related
Please help me. How to disable webview open new page ? I want to disable this behaviour, so if I click on a link, don't load it. I've tried this solution and edited a bit for myselft, but not worked. My webviewclient code:
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://example.com");
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
public 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;
}
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
In your WebViewClient, you can load only specific url that you want as below,
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equals("my url")) {
view.loadUrl(url);
}
return true;
}
Firstly you use web view you create web activity like this:
xml layout:-
<?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"
/>
WebView Activity:-
public class WebViewActivity extends AppCompatActivity {
#BindView(R.id.webView1)
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
// i am using intent getting the value from like this
Intent intent2 = getIntent();
Bundle bundle = intent2.getExtras();
String link = bundle.getString("Agreement_URL");
Log.e("link---",""+link);
String file_type=bundle.getString("file_type");
if(file_type.equals("PDF"))
{
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+link);
setContentView(webView);
}
else
{
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(link);
}
}
/** Method on BackPressed Button click*/
public void onBackPressed(){
super.onBackPressed();
/** Activity finish*/
finish();
}
pass the value like this from previous activity
Intent intent =new Intent(context, WebViewActivity.class);
intent.putExtra("Agreement_URL","http://54.183.245.32/uploads/"+ finalUploadDoc);
intent.putExtra("file_type","PDF");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("ggg",""+ finalUploadDoc);
context.startActivity(intent);
try this it helps you
How to refresh current Activity 1 time after load? My code refresh the activity, but in Loop.
This site have some examples, but most is after click button. Isn't my case.
My code is:
finish();startActivity(getIntent());
just do this way.
startActivity(getIntent());
finish();
public class MainActivity extends Activity {
String url;
//initializing WebView
private WebView mwebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Div1().execute();
String tag = (String) getIntent().getSerializableExtra("tag");
Intent intent = getIntent();
url = "https://xxxxxxxx.com/profile/"+tag+"";
//WebView
mwebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webView performance
mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
//mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mwebView.getSettings().setAppCacheEnabled(false);
mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
mwebView.loadUrl("https://xxxxxxxx.com/profile/"+tag+"");
//force links open in webview only
mwebView.setWebViewClient(new MyWebviewClient());
}
private class MyWebviewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Uri.parse(url).getHost().equals("xxxxxxxx.com/profile/\"+tag+\"")){
return false;
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}}
// Div1 AsyncTask
private class Div1 extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
return null;
}
#Override
protected void onPostExecute(String result) {
mwebView.loadUrl("javascript:(function(){"+
"l=document.getElementsByTagName('BUTTON')[1];"+
"e=document.createEvent('MouseEvent');"+
"e.initEvent('click',true, true);"+
"l.dispatchEvent(e);"+
"})()");
}
}
}
Button ok = (Button) findViewById(R.id.btnOK2);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(getIntent());
finish();
}
});
This is sample code I wrote:
public class MainActivity extends AppCompatActivity {
static boolean isInit = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isInit) {
isInit = false;
startActivity(new Intent(this, MainActivity.class));
finish();
Log.d("Restart", "asdasda");
}
}
}
One thing I need to say is that if you start this activity from another activity, in every call you need set the isInit variable true like this:
MainActivity.isInit = true;
startActivity(new Intent(this, MainActivity.class));
Using "getIntent()" instead of "new Intent(this, MainActivity.class)" cause closing the application after logging "Restart" text.
I don't know whether it is what you want (I think this can not be any solution of any problem). I still think better is reload only views.
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;
}
}
});
});
I am new to webView ,here I had t onclick event when I click text one and open in webview and when I click text 2 open with in same webview any one please how to place two onclick with in same webview ,every textview string come from server
I tried this way but no use any ne please help me I search I google but their is no use
Here below my code
Activity.java
//webview onclick and get bundle
webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl();
webviewurl2=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl2();
news_site_link_one=(TextView)findViewById(R.id.news_SourceLink_text_one_t_webview);
news_site_like_two=(TextView)findViewById(R.id.news_SourceLink_text_two_t_webview);
news_site_link_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
webviewintent.putExtra("webviewurl", webviewurl);
startActivity(webviewintent);
}
});
news_site_like_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
webviewintent.putExtra("webviewurl2", webviewurl2);
startActivity(webviewintent);
}
});
here my webview code
String SourceURL;
WebView webview;
final Activity activity = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news__web_view);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_news__web_view);
Intent intent =this.getIntent();
if(intent!=null)
SourceURL = intent.getStringExtra("webviewurl");
if(SourceURL.equals("webviewurl")) {
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
}
if(SourceURL.equals("webviewurl2")) {
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
}
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(SourceURL);
}
Use containsKey
Bundle data =getIntent().getExtras();
if(data.containsKey("webviewurl"))
{
SourceURL =data.getString("webviewurl");
}
else if(data.containsKey("webviewurl2"))
{
SourceURL =data.getString("webviewurl2");
}
Here is code snippet I tried for multiple urls
Myactivity.java
public void openWeb() {
impressum = (TextView) findViewById(R.id.web_link_1);
konkact = (TextView) findViewById(R.id.web_link_2);
uber = (TextView) findViewById(R.id.web_link_3);
impressum.setOnClickListener(this);
konkact.setOnClickListener(this);
uber.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String disclaimer = "http://lucidspace.de/imprint.php";
String contact = "http://lucidspace.de/contact.php";
String about = "http://lucidspace.de/about-us.php";
switch(v.getId()) {
case R.id.web_link_1:
Intent imprintIntent = new Intent(MainActivity.this, WebViewOpen.class);
imprintIntent.putExtra("webivewImprint",disclaimer);
this.startActivity(imprintIntent);
break;
case R.id.web_link_2:
Intent contactIntent = new Intent(MainActivity.this, WebViewOpen.class);
contactIntent.putExtra("webivewContact",contact);
this.startActivity(contactIntent);
break;
case R.id.web_link_3:
Intent aboutIntent = new Intent(MainActivity.this, WebViewOpen.class);
aboutIntent.putExtra("webivewAbout",about);
this.startActivity(aboutIntent);
break;
}
WebViewAcitivty
public class WebViewOpen extends AppCompatActivity {
String url;
WebView webView;
final Activity webViewActivity = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
/*webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView wView, int progress) {
webViewActivity.setTitle("Loading...");
webViewActivity.requestWindowFeature(progress * 100);
if(progress == 100) {
webViewActivity.setTitle(R.string.app_name);
}
}
});*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_webview);
Intent intent =this.getIntent();
if(intent!= null) {
Bundle data = getIntent().getExtras();
if(data.containsKey("webivewImprint")) {
url = data.getString("webivewImprint");
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
}
if(data.containsKey("webivewContact")) {
url = data.getString("webivewContact");
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
}
if(data.containsKey("webivewAbout")) {
url = data.getString("webivewAbout");
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
}
webView.loadUrl(url);
WebView.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/web_link"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This has worked for me
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;
}
}