I've created Normal Activity layout with original relative layout.My xml Button redirect to a webpage. And I want to make full screen of webview when tap, so I set setContentView(mWebView); (which is dynamically called) ...
My problem is - to appear banner image (which is originally created with xml -) upon mWebView!
My code is -
(imgbanner is above this code and findviewbyid)
btnlogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mWebView.loadUrl("http://www.facebook.com");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
setContentView(mWebView);
}
});
Related
I am using a WebView to show my website on my mobile Android app. I have disabled text selection in the WebView as I want to disable Copy.
I actually wish to allow the user to Paste a text but not Copy.
How can I do that please ?
public class MainActivity extends AppCompatActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
webview = findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
String domain = "www.google.fr";
if (host.equals(domain)) {
// This is my website, so do not override; let my WebView load the page
return false;
} else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
});
webview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.loadUrl("https://www.google.fr");
}
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}
}
}
As you know to copy a text you need to long press it to select.
So please add a longClickListener to the webview which will show anything else on long click instead of copying the text.
mWebView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
mWebView.setLongClickable(false);
But this will also disable using PASTE so to paste content from clipboard. I will suggest add a small button beside it and add an onClickListener to it which will paste the copied content to wherever you want.
ClipboardManager mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String pasteData = "";
if (!(mClipboard.hasPrimaryClip())) {
} else if (!(mClipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {
} else {
ClipData.Item item = mClipboard.getPrimaryClip().getItemAt(0);
copiedText = item.getText().toString();
}
I have a simple webview app that loads my website. Everything is ok, but when the user clicks on the Instagram icon in bottom of the webpage, I want to open the Instagram app instead of loading the Instagram webpage.
I'm trying to achieve this with shouldOverrideUrlLoading function and webview.geturl("https://www.instagram.com/").
The problem is when the user clicks on the Instagram icon, the URL doesn't change and stays default "http://archclub.ir/".
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://archclub.ir/login");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = webView.getUrl();
Toast.makeText(MainActivity.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if (url.contains("https://www.instagram.com/")) {
Toast.makeText(MainActivity.this, "salam", Toast.LENGTH_SHORT).show();
webView.loadUrl("http://archclub.ir/");
}
return false;
}
});
}}
My problem is: When the user clicks on the Instagram icon on the website, the Toast.makeText(MainActivity.this, "salam", Toast.LENGTH_SHORT).show(); doesn't run.
I've checked this. I've debugged it. The problem is when the Instagram icon is clicked, the webview.geturl is equal to archclub.ir/login and doesn't change to instagram.com but the webview shows the Instagram page.
you were replacing the url over and over.
please replace this block of code instead of shouldOverrideUrlLoading method :
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url = webView.getUrl(); // just omit this line
Toast.makeText(ali.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if (url.contains("https://www.instagram.com/")) {
Toast.makeText(ali.this, "salam", Toast.LENGTH_SHORT).show();
webView.loadUrl("http://archclub.ir/");
return true;
} else return false;
}
});
i have 3 diferents screens: main(contains one image), dialog(contains another image) and browser(contains a webView) and one activity source, ok, in the activity source I call the dialog on click image in main layout, then the dialog shows another image and I want when click in the dialog image, the app change main layout for browser layout and then the browser load an specific URI and then close the dialog.
My code dont work becouse i dont know how i need use the webView in separate layout, for example, to call in dialog i need use dialog.findViewById(R.id.webView1) or in main only findViewById(R.id.webView1), but this not work now and the app crash...
My code:
ImageView imgMain = (ImageView)findViewById(R.id.imgMain1);
imgMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(mainActivity.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Android");
dialog.setCancelable(true);
dialog.show();
//Boton de cerrar del dialog Android
Button closeDialog = (Button)dialog.findViewById(R.id.closeDialogBT);
closeDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
//click en imagen juegos del dialog android
ImageView imgDialog = (ImageView)dialog.findViewById(R.id.imgDialog1);
imgDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final WebView mWebView = (WebView)findViewById(R.id.webView1);
mWebView.loadUrl("http://www.example.com");
mWebView.setWebViewClient(new WebViewClient());
setContentView(R.layout.browser);
dialog.cancel();
}
});
}
});
Thank You!!!!
Why do you want to start WebView of one Activity from other activity.
It will be better to start activity containing WebView from your "imgDialog" with the intent.
and send URL string in that intent's extras.
in next step load webview with the URL string fetched from intent.
Try this. Hope this will help you
public class Example extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.translate);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I want to have a button press display a web page, I use the standard approach with a WebViewClient and WebChromClient like so:
final Button revsButton = (Button) this.findViewById(R.id.buttonReviews);
revsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
wb.setWebViewClient(new ShareWebViewClient());
wb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading Reviews...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl(revString );
setContentView(wb);
}
});
private class ShareWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
This is all working great, the thing is when I press the Back button it exits my Activity entirely and returns to the previous page. What I really want the back button to do is to close the WebViewClient and return to the dispaly with the button on it. This is what I tried to do so far which half worked -- it didn't leave the activity, but the activity page was just a blank screen instead of showing the controls that should be on there.
#Override
public void onBackPressed() {
if (wb.getVisibility() == View.VISIBLE) {
wb.setVisibility(View.INVISIBLE);
} else {
finish();
}
}
I suppose this happens because you're using WebViewClient rather than launching browser using standard:
Uri uri=Uri.parse(urlString);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);
In this case BACK button would close browser activity and you'll get back your primary activity.
In your case I would propose in onBackPressed() - restore your original content selecting
setContentView(my_original_layout);
i have one doubt. I have an iteration of TextViews, and what i want is when i click in one TextView , i want stop the iteration and open a web, who can i know what TextView as been click on? i have this code:
Iterator<TextView> it = text.iterator();
while(it.hasNext()){
test = it.next();
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
}
});
// condition to stop the iteration when i click on TextView
}
And what i want is the condition to stop the iteration when i click on the TextView that i want see, i try using some methods that are in the TextView and don't work. Anyone can help me? I have the iteration of TextViews because i want to do this dynamic, and i don't know the TextView who as click, and i want to know what TextView was click to stop the iteration, because if i don't stop this, for example if i have 3 TextView they will open the same web.
Thanks and forgive my English
I'm not sure I understand you entirely but from what I understand you want to stop the Iteration when you click on a TextView.
So one way you can do it is. Declare a boolean property for this class to use to control the loop, in this case I called it isStop and initial value with false.
while(it.hasNext() && !isStop){
...
}
then
public void onClick(View v) {
isStop = true;
//mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
So when you click on a TextView the flag will turn off the loop.
The solution to what i wanted:
On the Activity i create this.
OnClickListener s = new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
text = (TextView) findViewById(v.getId());
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
}
};
Iterator<TextView> it = text.iterator();
while(it.hasNext())
{
text = it.next();
text.setOnClickListener(s);
}