Im making a Web app.
I want block it's selecting texts.
When I use Facebook application, I can't select text likes the picture's.
Because facebook app, it's a hybrid app.
How can I control...
Is there a any method for blocking text selection??...
When I touch some text which hyper linked,
Webview showing up orange color border, where around the texts,
Can I change it color??? or not showing up...??
This is my very simple webviewClient source..
public WebView webViewDefault;
private Context context = null;
private String strListsURL = URLOperation.strListsURL;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.context = this;
webViewDefault = (WebView) findViewById(R.id.mainWeb);
webViewDefault.setVerticalScrollBarEnabled(false);
webViewDefault.setWebViewClient(new MyClient());
webViewDefault.loadUrl(strListsURL);
private class MyClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView mWebView, String url) {
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView mWebView, int errorCode, String description, String fallingUrl) {
mWebView.loadData("<html>Load Error</html>", "text/html", "utf-8");
}
}
In android webview, it enters the text selecting mode by long press the text. you can override the onPerformLongClick() to take over the control and return true to prevent from entering the webtextview mode.
Related
I have internet permission in my manifest, I am trying to search for "facebook" on the searchbar of my webview, but the problem is, for the 50% searches, It does not load the search request! this is so weird and frustrating. I implemented everything correctly and checked everything 3 times, but no luck. every time I search for the same on other browsers, It always works. but when I search on my webview, It sometimes loads the page, and most of the times, progress bar shows loading, and disappears as if the page has actually loaded, but it is still white. Why is this happening???
public class MainActivity extends AppCompatActivity {
private EditText searchEditText;
private ProgressBar progressBar;
private WebView webView;
private String SearchString = "https://www.google.co.in/search?q=";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("about:blank");
// use keyboard go button to search
searchEditText = (EditText) findViewById(R.id.searchfield);
searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
CustomSearch();
return false;
}
});
}
private class WebViewClientDemo extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getBaseContext(), "page error. please try again!", Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
public void CustomSearch() {
searchEditText = (EditText) findViewById(R.id.searchfield);
String url = searchEditText.getText().toString();
url = SearchString + url.replace(" ", "+");// replace_spaces_with_a_"+"_sign(prefix)
webView.loadUrl(url);
}
Did you try encoding your search text, as per my understanding you need to encode the search string.
I have loaded a web page in WebView via WebView.loadData().
Now I want to ad custom action to a button of that webpage from the WebView (Like, when I click the button, it will behave differently from the original webpage).
Is it possible from WebView? If possible, how can I do that?
Yes, it is possible. You can try something like this
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)
{
if(url.equalsIgnoreCase("your url"))
{
// your code
return true;
}
return false;
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
}
}
Don't forget to add myWebClient to your webView
No, it's no possible. You can only webView.setWebViewClient(new CustomWebClient());
private class CustomWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url...) // your logic here
return true;
}
I am experimenting with the loopj package. I am trying to make a HTTP request to a website and display the website in the webview.
I am successfully getting a result back, however the web view does not display the page as desired, instead chrome opens up and displays the page.
Am I missing something or is there a way I can override this unwanted behaviour?
Below is my oncreate method where I am making the request:
public class MainActivity extends Activity {
Button connectBtn;
TextView status;
WebView display;
String url = "http://www.google.com";
AsyncHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusbox);
connectBtn = (Button)findViewById(R.id.connectBtn);
display = (WebView)findViewById(R.id.webView1);
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
display.loadUrl(url);
}
});
}
});
}
setWebViewClient to your WebView and override shouldOverrideUrlLoading() now write view.loadUrl(url); in that method.
Just add this code,
display.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
You need to set a WebViewClient and override the shouldOverrideUrlLoading method. Something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
view.loadUrl(url);
}
});
That makes sure that clicks on links in the WebView are handled by the WebView itself.
Edit: Actually, I misread the question. You aren't dealing with a click in the WebView itself, so this isn't relevant. Sorry!
just use this code under you webviewclient
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final EditText editText = (EditText) findViewById(R.id.urlfield);
editText.setText(url);
}
}
Is it possible to restrict WebViews so that when you have let's say one webview for website but you just want the view to stick to that page and only that page?
Great example is I have a twitter WebView that is following a tag. I just want the user to be able to follow that tag not be able to go deeper into twitter stuff.
It's already a pre-set link,
Button--> opens web view with set link to twitter.
So has to be some kind of check for on press or something like I think.
something like this should do it.
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somedomain.com")) {
view.loadUrl(url);
}
return true;
}
});
In-fact the above is a good solution but if you want an elaborate one. Try this one. I don't know whether this is proper solution for your query. play around with this.
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!url.equals("your_domain_name.com/")){
setResult(RESULT_OK);
finish();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(!url.equals("your_domain_name.com/")){
finish();
}
}
#Override
public void onTooManyRedirects(WebView view, Message cancelMsg,
Message continueMsg) {
super.onTooManyRedirects(view, cancelMsg, continueMsg);
cancelMsg.sendToTarget();
}
}
I have made two attempts in trying to access a website with authentication, and I am not sure what is wrong with my attempts. I will list each one. Has anyone tried this and gotten it to work?
This should not be difficult for Java Android programmers to understand, nor should it matter that I am using monodroid EDIT (It DOES matter, because I have a Java implementation below that works just fine)END EDIT.
I am trying to gain access to an SSRS RDL through the WebView and I need to plug in some generic credentials.
ACTIVITY:
public static string username = "...";
public static string password = "...";
public static string website = "http://10.0.0.5/Reports";
private WebView webView;
private void setupWebView(int attempt)
{
webView.Settings.JavaScriptEnabled = true;
webView.Settings.BlockNetworkLoads = false;
switch (attempt)
{
case 1:
{
webView.SetHttpAuthUsernamePassword(website, "", username, password);
}break;
case 2:
{
webView.SetWebViewClient(new AuthenticationClient(this));
}break;
}
webView.LoadUrl(website);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
webView = new WebView(this);
setupWebView(1);//1 or 2 depending on what I am testing
// Set our view from the "main" layout resource
SetContentView(webView);
}
}
AuthenticationClient:
//DECLARED IN ANOTHER FILE
[Android.Runtime.Register("android/webkit/WebViewClient", DoNotGenerateAcw = true)]
public class AuthenticationClient:WebViewClient
{
private Context _context;
public AuthenticationClient(Context context)
{
_context = context;
}
public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl)
{
Toast.MakeText(_context, "OnReceivedError: " + errorCode, ToastLength.Long);
//base.OnReceivedError(view, errorCode, description, failingUrl);
}
public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
{
Toast.MakeText(_context, "OnPageStarted: " + url, ToastLength.Long);
//base.OnPageStarted(view, url, favicon);
}
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
Toast.MakeText(_context, "ShouldOverrideUrlLoading: " + url, ToastLength.Long);
view.LoadUrl(url);
return true;
}
public override void OnReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, string host, string realm)
{
Toast.MakeText(_context, "OnReceivedHttpAuthRequest: " + host, ToastLength.Long);
handler.Proceed(Activity1.username, Activity1.password);
}
}
//END DECLARATION
ATTEMPT 1: It does not even try to load the page. I keep getting an error:
chromium(18710): external/chromium/net/http/http_auth_gssapi_posix.cc:463: [0821/095922:WARNING:http_auth_gssapi_posix.cc(463)] Unable to find a compatible GSSAPI library
ATTEMPT 2: Does not show a single toast message.
I have already looked at:
http://developer.android.com/reference/android/webkit/WebView.html
Using WebView setHttpAuthUsernamePassword?
WebView.setHttpAuthUsernamePassword() not working?
Passing username and password to SSRS 2005 reports from web application
OTHER STUFF
I have now done it in Java and it works fine. I still need a Mono for Android solution:
public class Android_reporttestActivity extends Activity {
public static String username = "...";
public static String password = "...";
public static String website = "http://10.0.0.5/Reports";
private WebView setupWebView()
{
WebView webView = new WebView(this);
webView.setWebViewClient(
new WebViewClient(){
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)
{
handler.proceed(username,password);
}
}
);
return webView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = setupWebView();
// Set our view from the "main" layout resource
setContentView(webview);
webview.loadUrl(website);
}
Running the following for me allows everything to work as expected for me:
[Activity (Label = "WebViewExample", MainLauncher = true)]
public class Activity1 : Activity
{
string url;
WebView mWebView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
url = "http://awebsite.com";
mWebView = (WebView)FindViewById<WebView> (Resource.Id.webview);
mWebView.SetWebViewClient (new ViewClient (this));
mWebView.Settings.JavaScriptEnabled = (true);
mWebView.Settings.PluginsEnabled = (true);
mWebView.LoadUrl (url);
}
class ViewClient : WebViewClient
{
Activity1 _activity;
public ViewClient(Activity1 activity)
{
_activity = activity;
}
public override void OnPageStarted (WebView view, string url, Android.Graphics.Bitmap favicon)
{
Console.WriteLine ("On Page Started");
}
public override void OnReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, string host, string realm)
{
Console.WriteLine ("On received Http auth request");
handler.Proceed("username", "password");
}
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
Console.WriteLine ("Should Override Url Loading...");
return base.ShouldOverrideUrlLoading (view, url);
}
}
}
The reason why your toasts aren't showing is probably because you're not calling ".Show()" on them so they are never displayed.
If you still can't get it up and running, then providing a URL we can use to hit against would be really useful.
I hope this helps,
ChrisNTR