How to open Url in Webview activity
Hi,
i want to open link in WebView activity right now my code is scan barcode & open link directly to browser but
i want to change it and open in Webview
how can i do this please help me to fix this issue
thanks
here is code of BarcodeScannerActivity
public class BarcodeScannerActivity extends AppCompatActivity {
String scanContent;
String scanFormat;
TextView textView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode_scanner);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(BarcodeScannerActivity.this);
scanIntegrator.setPrompt("Scan");
scanIntegrator.setBeepEnabled(true);
//enable the following line if you want QR code
//scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
scanIntegrator.setCaptureActivity(CaptureActivityAnyOrientation.class);
scanIntegrator.setOrientationLocked(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
if (scanningResult.getContents() != null) {
scanContent = scanningResult.getContents().toString();
scanFormat = scanningResult.getFormatName().toString();
}
Toast.makeText(this, scanContent + " type:" + scanFormat, Toast.LENGTH_SHORT).show();
textView.setText(scanContent + " type:" + scanFormat);
Intent browseintent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com/index.php?iduser="+ scanContent));
startActivity(browseintent);
} else {
Toast.makeText(this, "Nothing scanned", Toast.LENGTH_SHORT).show();
}
}
}
Webview Activity Code
public class SecondActivity extends AppCompatActivity {
Button b1;
EditText ed1;
private WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = ed1.getText().toString();
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Replace the following code
Intent browseintent=new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com/index.php?iduser="+ scanContent));
startActivity(browseintent);
with below code
Intent browseintent=new Intent(this, SecondActivity.class);
browseintent.putExtra("url","http://www.example.com/index.php?iduser="+ scanContent);
startActivity(browseintent);
This will open the secondactivity with url in intent extras. You can set it to your edittext or you can use it directly to your webview.
You can receive the url in the second activity using the following code
String url = getIntent().getExtras().getString("url");
You can use it in your button click as follows
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = getIntent().getExtras().getString("url");
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});
You try this, it should open link with webview:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://vk.com/zabroshkiborika");
Related
I want to pass URL string from main activity to second activity and load the URL in second activity.... but when I click the go button at mainactivity it goes to second activity but it shows nothing but blank.
here is my code ..
MainActivity:-
public class MainActivity extends AppCompatActivity {
EditText editText;
Button go;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.urltext);
go=findViewById(R.id.button6);
final String link=go.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Webview.class);
intent.putExtra(link,1);
startActivity(intent);
}
});
}
}
Second activity:
public class Webview extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String one = getIntent().getExtras().getString("1");
String http="https://";
String url=http+one;
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
No offence but you must have read the documentation first .
intent.putExtra(key,value);
Key should be constant in case of getting value from the same key in other component.So the answer is your OnClick shold be like below.
final String link=go.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(YourActivity.this,Webview.class);
intent.putExtra("link",link);
startActivity(intent);
}
});
Getting url from intent in other activity.
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;// Use this url to open in web view
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 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;
}
}
});
});
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