Loading HTML5 contents on gallery view seems slow - android

I am developing an online magazine application. It downloads 50 to 100 html5 files from server and store it on sd card and shows this html5 files as pages of magazine. I have used gallery widget to load html pages, It works as my requirement.
But Some issues like loading html on gallery seems slow and page moves so slowly when swiping the pages still exist. My code is given bellow
public View getView(int position, View convertview, ViewGroup parent) {
try{
vi = convertview;
final int loctn = position;
if (convertview == null)
vi = inflater.inflate(R.layout.contentsadapter, null);
WebView webview=(WebView)vi.findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(pages.get(loctn));
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
&& view.canGoBack()) {
view.goBack();
return true;
}
return super.shouldOverrideKeyEvent(view, event);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
/*Intent intent = new Intent(Femi9Activity.this,
SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Femi9Activity.this.startActivity(intent);*/
}
});
webview.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webview.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
gallery.onTouchEvent(event);
return false;
}
});
}catch(Exception e){
}finally{
}
return vi;
}
}
so the question is
How to improve speed of HTML loading.
can I load previous and next page html on current page
Is there a better way to do this without using gallery

You can use ViewPager instead of Gallery.

Related

How to get URL from long click links in a webview?

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

How to intercept the click action of WebView?

My app has a WebView to load HTML web page, I want to implement a feature that user can click any link inside the web, then I could know which link was clicked, but the web page couldn't jump out and still keep the unclicked state. I tried the below way, but it doesn't work, the WebView still would jump to the url I clicked.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "shouldOverrideUrlLoading() - url = " + url);
return true;
}
});
You can use setOnTouchListener, Using this you can get clicked url
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView) v).getHitTestResult();
Log.d(TAG, "clicked - url = " + hr.getExtra());
return false;
}
});

goBack() function of webview doesnt work when html string is loaded

I am setting a HTML string to a webview. The html string contains hyperlinks(clickable links). when the user clicks on the link, it is loaded in the same webView. When the user presses back, the goBack() function works but doesnt load back the string. I also tried loading the html string again when the user presses back. But this doesnt work. Posting my code below.
mDescriptionWebVIew.getSettings().setJavaScriptEnabled(true);
mDescriptionWebVIew.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((i == KeyEvent.KEYCODE_BACK) && mDescriptionWebVIew.canGoBack()) {
mDescriptionWebVIew.goBack();
if(urlList.size()!=0) {
urlList.remove(urlList.size() - 1);
}
if(urlList.size()==0){
mDescriptionWebVIew.loadDataWithBaseURL("", htmlFinal, "text/html", "UTF-8", "");
}
return true;
}
return false;
}
});
mDescriptionWebVIew.getSettings().setPluginState(WebSettings.PluginState.ON);
mDescriptionWebVIew.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
urlList.add(url);
return false;
}
});
If you're not using the baseUrl parameter (first parameter of the loadDataWithBaseURL method), then maybe you just need loadData instead:
mDescriptionWebVIew.loadData(htmlFinal, "text/html", "UTF-8");

Android WebView error for high density screens

I have an Android application which uses a WebView to render web page. In css I have set to show everything twice as big for high density screens (like Nexus 7 2013). If I open the web page from the browser, Everything shows as it should. But when I run the app, everything is very small.
Is there any way to determine why the application WebView shows content for lower density screens but browser (Chrome) shows as it should!
Device: Nexus 7 2013
// I used this class and my code is working fime at my side please try this may be it will help you
public class WebViewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
private TextView header_maintext;
private TextView headeroptiontext;
private RelativeLayout back;
private String url_string="http://www.google.com";
private String header_maintext_string="Your text";
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled") #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_layout);
webview = (WebView)findViewById(R.id.webview01);
header_maintext= (TextView)findViewById(R.id.header_maintext);
header_maintext.setText(header_maintext_string);
headeroptiontext = (TextView)findViewById(R.id.headeroptiontext);
headeroptiontext.setVisibility(View.GONE);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
back = (RelativeLayout) findViewById(R.id.back_layout);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(webview.canGoBack() == true)
{
webview.goBack();
}
else
{
finish();
}
}
});
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewActivity.this, "My application", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(url_string);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
I think this article should help explain what you need to do: https://developers.google.com/chrome/mobile/docs/webview/pixelperfect
I suspect that the difference between Chrome and WebView boils down to the WebSettings that are being applied to the WebView.

how to get click event of edittext inside webview

I am displaying webpages in a webview in my app. I want to enable screen orientation only when user clicks/touch on an edittext inside webview.I have tried below code.
mWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
if(hr!=null){
if(hr.getType()==HitTestResult.EDIT_TEXT_TYPE)
((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
else
((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
return false;
}
});
But it is not working in OS versions below 2.3. How can i do this in these versions also.please help me .i am stuck on this..
below code working for edittext click inside webview :
WebView web_view = (WebView) subview
.findViewById(R.id.webview_mytagdetail_text);
web_view.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent in = new Intent(getActivity(), URL_SCREEN.class);
in.putExtra("URL", url);
startActivity(in);
return true;
}
});
web_view.getSettings().setJavaScriptEnabled(true);
web_view.getSettings().setLoadWithOverviewMode(true);
web_view.getSettings().setBuiltInZoomControls(true);
web_view.getSettings().setDisplayZoomControls(false);
web_view.loadDataWithBaseURL("", campaignClass.getCampaignDesc(),
"text/html", "UTF-8", "");

Categories

Resources