I´m new to android and i´m using the first time webview, fragment, drawer...
I have the problem that with my code after pressing fullscreen in the video my app is crashing.
And i have no idea what is going wrong...
WebViewFragmentVideos
public class WebViewFragmentVideos extends Fragment {
WebView webView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.Websitesenglish);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);
webView.setWebChromeClient(new MyChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
return v;}
class MyChromeClient extends WebChromeClient {
String url = getArguments().getString("url");
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
Intent intent = new Intent(null, LandVideoAct.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("video", url);
startActivity(intent);
}
}
}
and
LandVideoAct
public class LandVideoAct extends Activity {
WebView webView, fullweb;
String url = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen);
setContentView(R.layout.landfull);
url = getIntent().getStringExtra("video") + "?fs=1";
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
webView = (WebView) findViewById(R.id.fullwebview);
if (Build.VERSION.SDK_INT < 8) {
webView.getSettings().setPluginsEnabled(true);
} else {
webView.getSettings().setPluginState(PluginState.ON);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
LandVideoAct.this.finish();
}
});
webView.loadUrl(url);
}}
I hope you dudes can help me with my problem.
Thank you!!
this piece of line of code is not allowed in android.
Intent intent = new Intent(null, LandVideoAct.class);
coming to your crash problem. Its mainly because of a above line of code. When full screen of video in webview is called , its gives call to android system ie onShowCustomView(View view, CustomViewCallback callback). Where you above line of code is failing.
instead of that use activity context or application context
Intent intent = new Intent(context, LandVideoAct.class);
Related
I want to Create a Context Menu for web view in android. My problem is that I cant get the URL from the clicked link. I write this code for log the clicked link:
webView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
WebView webView1 = (WebView) v;
WebView.HitTestResult hitTestResult = webView1.getHitTestResult();
Log.i("LinkClicked", hitTestResult.getExtra());
return false;
}
});
With this code, when long click on links in websites like stackoverflow, log like this:
https://stackoverflow.com/questions
everything is ok, but when long click on google search results, log like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAJFBMVEVHcEz/AAD/AAD/AAD/AAD/AAD/AAD/AAD/g4P/////KCj/7e0I8rGvAAAAB3RSTlMAT0g5Ggp+EilkVQAAAENJREFUGJVjYCAGsDAzggEzC5jLxo4E2BgYWNlRACsDM5jmggkwMzCBaQ5uqBATTICTk4OAAEILhqEY1mI4DNPphAAASIsES4gsgWgAAAAASUVORK5CYII=
it does not return a link. How I can fix this?
public class MainActivity extends AppCompatActivity {
WebView webView;
String URL1 = "https://stackoverflow.com/questions/60577403/how-to-get-url-from-long-click-links-in-a-webview/60577736?noredirect=1#comment107173847_60577736";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
public void onPageFinished(WebView view, String url) {
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
URL1 = url;
return super.shouldOverrideUrlLoading(view, url);
}
});
webView.loadUrl(URL1);
// Register the context menu for web view
registerForContextMenu(webView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Get the web view hit test result
final WebView.HitTestResult result = webView.getHitTestResult();
// If user long press on url
if (result.getType() == WebView.HitTestResult.ANCHOR_TYPE ||
result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
// Set the title for context menu
menu.setHeaderTitle("\t\t\t\t\t\t\t\t\t\t ◦ ◉ ⦿ Select ⦿ ◉ ◦ \t");
// Add an item to the menu
menu.add(0, 1, 0, " \t \t➤\t Show URL")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
String Pressed_url = result.getExtra();
Toast.makeText(MainActivity.this, "URL is:-" + Pressed_url,
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
}
Download Full Code from github.
Output
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;
}
});
This similar SO question didn't help.
I have implemented a simple WebView to my MainActivity which loads a particular webpage on app launch. I have a TextView that shows the loading % of current webpage. The code is as below
public class MainActivity extends AppCompatActivity {
WebView webView;
final String homeUrl = "https://example.com";
String lastAccessedUrl = homeUrl;
WebChromeClient webChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = (WebView) findViewById(R.id.browser);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setLoadsImagesAutomatically(false);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
webView.canGoBack();
webView.canGoForward();
webView.setWebViewClient(new MyBrowser());
webView.loadUrl(homeUrl);
webChromeClient = new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
TextView textView = (TextView) findViewById(R.id.loadingPercentage);
textView.setText(""+newProgress);
}
};
webView.setWebChromeClient(webChromeClient);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
lastAccessedUrl = url;
view.loadUrl(url);
return true;
}
}
}
The problem I am facing is, the TextView shows loading % only for the first time the webpage loads. Then it is set to 100 permanently. Then if I click on any link on the first page, the TextView does not start from zero.
Do justify in comments if you vote down.
Any help would be appreciated.
There is no such problem with your code. Make sure you are clicking on a url. If you click on an expand view, it will just expand to show you certain data. It wont load as a new url. Click on say, www.stackoverflow.com and check if it's showing you loading percentage.
Hi i have a MainActivity class where i have a listview populated by rss items. When i click on item list my app shows me an alertdialog where i can see in message box the item details. My problem is that after i show alertdialog when i close it and open a new item my app crashes giving me this error:
FATAL EXCEPTION: main.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Here my java class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView);
wv = new WebView(this);
try {
url = new URL("http://www.unikore.it/index.php/ingegneria-informatica-home?format=feed");
} catch (MalformedURLException e) {
e.printStackTrace();
}
new ReadRssTask().execute(url);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
OptionDialog = new AlertDialog.Builder(MainActivity.this).create();
OptionDialog.setTitle(listview.getItemAtPosition(position).toString());
s = rssItems.get(position).getDescription();
if(s.contains("href=")){
s.substring(0,s.indexOf("</div>"));
wv.loadData(s, "text/html", "UTF-8");
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
OptionDialog.setView(wv);
}else {
OptionDialog.setMessage(s.replaceAll("<[^>]*>", ""));
}
OptionDialog.show();
}
});
}
i tried using OptionDialog.dismiss() method but app crashes anyway.
Thanks
move the instantiation of the WebView just before accessing the object.
s = rssItems.get(position).getDescription();
if(s.contains("href=")){
wv = new WebView(this);
wv.loadData(s, "text/html", "UTF-8");
// other code
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);
}
}