please predecessors, Am creating a web browser homepage in android studio and want to be able to input URL from the same editText as that of my search.
have been able to take search and load google results, but URL still end up in searched instade of loading the URL website.
this is my codes.
EditText searchEditText = rootView.findViewById (R.id.searchEditText);
ImageButton searchBt = rootView.findViewById (R.id.searchBt);
searchBt.setOnClickListener (new View.OnClickListener ( ) {
#Override
public void onClick(View v) {
String searchQuery = searchBar.getText ().toString ();
if(URLUtil.isValidUrl (searchQuery)){
webView.loadUrl (searchQuery);
}else {
String searchURL = "https://www.google.com/search?q="+searchQuery;
webView.loadUrl (searchURL);
}
}
});
The problem is probably because your URL doesn't have https at the beginning.
Try using some other mechanism to determine if the URL is valid.
For example, use WEB_URL pattern in Patterns Class
Patterns.WEB_URL.matcher(potentialUrl).matches()
Related
I need to highlight weburl inside textview.
To achieve this I have added android:autoLink="web" attribute inside my textview xml.
If url is related to youtube video I need to play it inside youtube player activity in my app and for other types of urls i want it to open on web browser.
So How I detect which url is get click and find is it youtube link or not and perform redirection according to it.
Following are sample text which my text view holding
this is sample text you can find nice article over this link
www.example.com and there is nice video which explain here
www.youtube.com/xyzpqr furthere reading please download pdf from
here www.example.com/pdf/xyz
there are multiple links is present so need to detect that clicked link and preform action on selected link.
You Can Apply Listener on TextView and When Click is Detected use StartsWith Method in Android To Detect if url starts with you.(url of youtube ) if it matches do your youtube player stuff else perform redirection
Why don't you use setMouvementMethod() instead ?
Try this in your TextView:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"/>
Then add this line in you Java class:
TextView myLink = (TextView) findViewById(R.id.myTextView);
myLink.setMovementMethod(LinkMovementMethod.getInstance());
Please try below attribute in your textview in xml file:
android:autoLink="all"
android:linksClickable="false"
android:textColorLink="#3393FF"
You can check URL using contains function.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String URL = textView.getText().toString();
if (URL.contains("youtube") || URL.contains("youtu.be")) {
// then redirect to youtube
} else if (URL.contains("pdf")) {
// then redirect to pdf app
} else {
// then redirect to web
}
}
});
OR
you can use below Library and link : https://github.com/saket/Better-Link-Movement-Method
implementation 'me.saket:better-link-movement-method:2.2.0'
Try below code :
BetterLinkMovementMethod
.linkify(Linkify.ALL, textView)
.setOnLinkClickListener((textView, url) -> {
// Handle clicks.
if (url.contains("youtube") || url.contains("youtu.be")) {
// then redirect to youtube
} else if (url.contains("pdf")) {
// then redirect to pdf app
} else {
// then redirect to web
}
return true;
})
.setOnLinkLongClickListener((textView, url) -> {
// Handle long-clicks.
return true;
});
I have a problem that I want to use jsoup to grab news but always fail.
this is news website.
https://www3.nhk.or.jp/news/
this is my picture . which I circle is I wanted data.
https://drive.google.com/open?id=1KJAyOSdHO8APPD6_A9MjxkoFjekcQLXt
but no matter what I do. it always get wrong data or empty.
this is my program.
public class News extends AppCompatActivity {
Button ok;
private static final String url ="https://www3.nhk.or.jp/news/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
ok=(Button)findViewById(R.id.ok);
ok.setOnClickListener(okbtn);
}
private Button.OnClickListener okbtn=new Button.OnClickListener(){
public void onClick(View v){
try{
Connection.Response response = Jsoup.connect(url).execute();
String body = response.body();
Document data = Jsoup.parse(body);//visible-phone print_hide
Elements country=data.select("main");
Elements main=data.select("div[id=module module--news-main index-main]");
for(Element e1: country)
{
mytoast(e1+"");
}
}
catch(Exception ex){ex.printStackTrace() ;}
}
};
private void mytoast(String str)
{
Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
please help me
thanks
You can try to see it's HTML first.
If you can't see it, you don't use jsoup.
There's a small hint in its comment:
このページではJavaScriptを使用しています
=>This is generated by JavaScript
If it's generated, you can't find it from Jsoup.
In this case, I'll use Chrome's tool to monitor the XHR tab
Look into each XHR request, and find the most possible one,
for example, I see this
https://www3.nhk.or.jp/news/json16/syuyo.json?_=1559183885640
A part of the response:
"id":"193411",
"title":"三菱UFJ銀行 新規口座は原則デジタル通帳に",
"pubDate":"Thu, 30 May 2019 04:03:11 +0900",
"cate":"5",
...
"id":"193437",
"title":"エアレース世界選手権 今季限りで終了",
"pubDate":"Thu, 30 May 2019 09:40:37 +0900",
So this is exactly what you want. It comes from another link!
You don't need Jsoup, just HttpGet the link
https://www3.nhk.or.jp/news/json16/syuyo.json?_=1559183885640
And I think the numbers looks like UnixTime,
So I check the current time is : 1559184830782, that's it.
Just use that link as API and time as parameter.
In Android i have a webview that lead to an google authentication form. When the authentication form is loaded in the webview it will call onReceivedLoginRequest:
public void onReceivedLoginRequest(final AuthenticationWebView client, final WebView view, final String realm,
final String account, final String args) {
...
}
When this method is called i get the Accounts stored on the device and let the user choose one and use that one to retrieve an authtoken:
mAccountManager.getAuthToken(account, mAuthToken, null, mWebViewActivity, this, null);
It all works perfectly fine (i think) and the next step looks good also:
final String result = bundle.getResult().getString(AccountManager.KEY_AUTHTOKEN);
if (result != null) {
// authentication succeeded, new url given as result for redirection
mWebView.loadUrl(result);
} else {
onLoginFailed();
}
A new method is called and returns the new url for redirection which i load in the webview. The result String returns a new adress which looks good but does nothing, it doens't log in but i don't get any message error either from the webview.
Url:
https://accounts.google.com/MergeSession?args=..&uberauth=WILL_NOT_SIGN_IN&source=AndroidWebLogin
I modified the url a bit with (triple)dots, don't know how secure it is and it was not such an interesting part of the url.
Anyone know why the url will not redirect me to the website behind it? When i use this url in my browser it recognizes my account, i only have to fill in my password and it redirects me correctly.
I use a native Android webview and i inject it with javascript but this only happens on the page after the login.
The settings i added to the webview are:
mSettings = getSettings();
mSettings.setJavaScriptEnabled(true);
mSettings.setDomStorageEnabled(true);
mSettings.setAllowFileAccess(true);
mSettings.setGeolocationEnabled(true);
Movie_search is a text field got from the user and ive referenced it by using
movie_search= (EditText)findViewById(r.id.editText);
and same goes for the button too
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(NEWS,"IMDB LINK->:"+movie_search);
URL="http://www.omdbapi.com/?t="+movie_search+"";
Log.i(NEWS,"IMDB LINK->:"+URL);
new DownloadConfigurationTask().execute(URL);
}
});
the problem is the string doesnt seem to append to the url,
the log i get is
IMDB LINK->:movie_search
IMDB LINK->:http://www.omdbapi.com/?t=
instead of
IMDB LINK->:movie_search
IMDB LINK->:http://www.omdbapi.com/?t=movie_search
any ideas what might be the problem ?
use EditText.getText() for getting text entered by user and append it with URL instead of EditText instance. change it as:
URL="http://www.omdbapi.com/?t="+movie_search.getText()+"";
I'm working on making a browser as a hybrid app using worklight framework for Android. I implemented my address bar as an input element which received the user input and pass the arguments to the webview to load the page.
However, I cannot figure out how to do the reverse: whenever the user click on a link in webview, I want the address bar to change to the new location.
Are you implementing a native page that is opened? If so, take a look at ChildBrowser, that basically does the same thing. It has a TextView being used as an address bar. You may decide to use it, or get the bits and pieces you want out of it. Regardless, I would image what you want to do something like this. By overriding the onLoadResource in the WebViewClient, you should be able to grab the url and change your TextBox.
In response to the comment below: inside your environment's main js file in the wlEnvInit() function:
function wlEnvInit(){
wlCommonInit();
// Environment initialization code goes here
document.onclick=manageLinks;
}
Then in this function get the url and set the text of your input element:
function manageLinks(event) {
var link = event.target;
//go up the family tree until we find the A tag
while (link && link.tagName != 'A') {
link = link.parentNode;
}
if (link) {
var url = link.href;
console.log("url = " + url);
//You can decide if you want to separate external or
//internal links, depending on your application
var linkIsExternal = ((url.indexOf('http://') == 0) || (url.indexOf('https://') == 0));
if (linkIsExternal) {
myInput.setText(url);
return false;
}
}
return true;
}
Inside of your WebView, inside the plugin, intercept the URL like this:
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
//use this area to set your input. Depending on how you
//implemented your plugin, you may need to return this value
//back to your main activity
Toast.makeText(cordova.getActivity(), "Loading: " + url, Toast.LENGTH_LONG).show();
}
});
Have you try to get the url from the href of and assign to the input variable and do the get/post? I know that it is possible in SDK i figure it dont will be harder in a framework. You can store the hiperlinks in a array with a parser or something similar.
example pseudocode:
When_hiperlink_clicked: //could be like a listener (search about it)
url = hiperlink.getURL("myHiperlink");
myinput.setText(url);
execute_input_bar_action();
Is difficult to figure out without code or something more, sorry.