Issue
I have an autocompletetextview used for opening urls in webview. I want to automatically add ".com" as postfix to urls which does not have any extensions like .com, .tv, .co.in, etc. But problem is how to check if there is already an extension present in the url entered. I searched for it and found this post, but it only checked for particular known extensions. I want to check if any extension is there or not. If extension present then opens the url if not then adds ".com" as postfix to url.
public class MainActivity extends AppCompatActivity implements AdvancedWebView.Listener {
private AdvancedWebView mWebView;
private AutoCompleteTextView urlEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlEditText = (AutoCompleteTextView) findViewById(R.id.urlField);
mWebView = (AdvancedWebView) findViewById(R.id.webView);
mWebView.setListener(this, this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setGeolocationEnabled(false);
mWebView.setMixedContentAllowed(true);
mWebView.setCookiesEnabled(true);
mWebView.setThirdPartyCookiesEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url){
v.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
//Toast.makeText(MainActivity.this, "Finished loading", Toast.LENGTH_SHORT).show();
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
}
});
mWebView.addHttpHeader("X-Requested-With", "");
Button openUrl = (Button) findViewById(R.id.goButton);
openUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
String url = urlEditText.getText().toString();
if(!url.contains("http://www."))
url = "http://www." + url;
// Here should be the code to check for extensions
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
// ...
}
@SuppressLint("NewApi")
@Override
protected void onPause() {
mWebView.onPause();
// ...
super.onPause();
}
@Override
protected void onDestroy() {
mWebView.onDestroy();
// ...
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}
@Override
public void onBackPressed() {
if (!mWebView.onBackPressed()) { return; }
// ...
super.onBackPressed();
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
mWebView.setVisibility(View.INVISIBLE);
}
@Override
public void onPageFinished(String url) {
mWebView.setVisibility(View.VISIBLE);
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "onPageError(errorCode = "+errorCode+", description = "+description+", failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadRequested(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Toast.makeText(MainActivity.this, "onDownloadRequested(url = "+url+", userAgent = "+userAgent+", contentDisposition = "+contentDisposition+", mimetype = "+mimetype+", contentLength = "+contentLength+")", Toast.LENGTH_LONG).show();
/*final String filename = UUID.randomUUID().toString();
if (AdvancedWebView.handleDownload(this, url, filename)) {
// download successfully handled
}
else {
// download couldn't be handled because user has disabled download manager app on the device
}*/
}
@Override
public void onExternalPageRequest(String url) {
Toast.makeText(MainActivity.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
}
}
Solution
I used a simple method to validate the url and return true/false value. If url is valid then open url as it is. If url is invalid add ".com" to url and then open.
In onCreate use methos like this:
if(isURL(url)){
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}else{
url = url + ".com";
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}
Method to check if url is valid:
public boolean isURL(String url)
{
if(Patterns.WEB_URL.matcher(url).matches()){
return true;
}else{
return false;
}
}
Answered By - Sammy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.