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", "");
Related
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;
}
});
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");
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.
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.
i have one doubt. I have an iteration of TextViews, and what i want is when i click in one TextView , i want stop the iteration and open a web, who can i know what TextView as been click on? i have this code:
Iterator<TextView> it = text.iterator();
while(it.hasNext()){
test = it.next();
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
}
});
// condition to stop the iteration when i click on TextView
}
And what i want is the condition to stop the iteration when i click on the TextView that i want see, i try using some methods that are in the TextView and don't work. Anyone can help me? I have the iteration of TextViews because i want to do this dynamic, and i don't know the TextView who as click, and i want to know what TextView was click to stop the iteration, because if i don't stop this, for example if i have 3 TextView they will open the same web.
Thanks and forgive my English
I'm not sure I understand you entirely but from what I understand you want to stop the Iteration when you click on a TextView.
So one way you can do it is. Declare a boolean property for this class to use to control the loop, in this case I called it isStop and initial value with false.
while(it.hasNext() && !isStop){
...
}
then
public void onClick(View v) {
isStop = true;
//mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
So when you click on a TextView the flag will turn off the loop.
The solution to what i wanted:
On the Activity i create this.
OnClickListener s = new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
text = (TextView) findViewById(v.getId());
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
}
};
Iterator<TextView> it = text.iterator();
while(it.hasNext())
{
text = it.next();
text.setOnClickListener(s);
}