im working on my first android app, be patience. In my android emulator, the responsive design works fine, but when i create the apk and run in my device the design is normal, not responsive. Any idea?
This is my code:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView myWebView;
#SuppressWarnings("deprecation")
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.loadUrl("http://apelarse.com.ar");
myWebView.setWebViewClient(new myWebViewClient());
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(myWebView.canGoBack()){
myWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
please try from this and i think if web responsive layout not working then its not android side probe its web side probe first you try from my code 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="tittle";
/** 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);
url_string = getIntent().getStringExtra("url");
header_maintext_string = getIntent().getStringExtra("header_text");
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, "tittle", "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);
}
}
Related
When I tried to login it shows a popup window and asking for email and password
when I entered my correct email and password and click on login it doesn't redirect to my account it only shows the same page
this is my MainActivity
you can also download my application for better understanding my problems https://play.google.com/store/apps/details?id=in.bidforx.bidforx
use email: anup.gorai.9835#gmail.com
password:78907890
private ProgressBar progressBar;
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppRate.with(this)
.setInstallDays(0)
.setLaunchTimes(3)
.setRemindInterval(0)
.monitor();
AppRate.showRateDialogIfMeetsConditions(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo(
));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("https://bidforx.com");
if (Build.VERSION.SDK_INT>=21){
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
}else{ CookieManager.getInstance().setAcceptCookie(true);}
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent=new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
String shareBody="Add World ";
String shareSub="Download the BidForx App and Buy everthing in 1% Download the App now https://play.google.com/store/apps/details?id=in.bidforx.bidforx";
myIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
myIntent.putExtra(Intent.EXTRA_TEXT,shareSub);
startActivity(Intent.createChooser(myIntent,"Share using"));
}
});
}
private class WebViewClientDemo extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
// on back pressed
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
finish();
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
When I try to login, I am getting an error
"Uncaught TypeError: Cannot read property 'username' of null", source: https://bidforx.com/js/login.js (2)
There must be some problem in login.js line 2. In line 2 you try to call localStorage, and somehow you can login with browser but not with app.
I haven't tried it yet but you should allow WebView to use HTML5 local storage feature first and see if you can login with app.
webView.getSettings().setDomStorageEnabled(true);
and I think you should do the item checking like this:
if (localStorage.hasOwnProperty("username")) {
//
}
in order to prevent TypeError
I want to open glauniversity.in in my webview. I have implemented following code and searched a lot, but everything seems futile. Any Help will be Appreciated. The code goes as follows:
public class WebViews extends Activity {
String link;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
link="glauniversity.in";
webview = findViewById(R.id.wv);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
webview.setWebChromeClient(new WebChromeClient() { #Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } });
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(link);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webview.canGoBack()) {
webview.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
glauniversity.in, This is not a valid url
replace this to
http://glauniversity.in/
Bonus::
Check valid url by
Patterns.WEB_URL.matcher(url).matches()
or
URLUtil.isValidUrl(url)
I am using webview to display the html data.It is working fine if the data is less(less than 1 mb)..I am using following code
mDecryptDataWv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
DebugLog.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
//showProgressDialog();
DebugLog.i(TAG, "************onPageStarted **************");
}
#Override
public void onPageFinished(WebView view, String url) {
DebugLog.i(TAG, "************onPageFinished **************");
if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) {
DebugLog.i(TAG, "mProgressDialog ::::::stopping");
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
If the data is more then 1MB it is taking more time to complete the loading.so my intention is to load the data page by page upon user scrolling with progress dialog..Can anyone has idea?
// 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 have the Problem that i load a website with the loadDataWithBaseURL method. There i load a prepared internetsite in a webview. if i go back in my previous activity and start the webview activity it loads the same prepared website with loadDataWithBaseURL() method but i get a webview with twice the content. For example Hello World and the second Hello World.
My Code is the followed:
package de.http.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mywebview);
Bundle bundle= this.getIntent().getExtras();
String param1 = bundle.getString("Weburl");
getDetailWebsite(param1);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
loadWebview();
}
public void loadWebview()
{
webview.clearView();
webview.loadDataWithBaseURL("http://branchenportal-schlitz.de", HttpHelperClass.htmlDetailWebsiteCode, "text/html", "UTF-8", HttpHelperClass.htmlDetailWebsiteCode);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
WebBackForwardList mWebBackForwardList = webview.copyBackForwardList();
String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()).getUrl();
if(historyUrl.contains("<html>"))
{
loadWebview();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void getDetailWebsite(String url)
{
try{
HttpHelperClass.executeHttpGetDetailWebsite(url,this);
}catch (Exception ex)
{
ex.printStackTrace();
}
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
if(url.contains("branchenportal-schlitz.de"))
{
view.clearHistory();
}
super.onPageFinished(view, url);
}
}
#Override
protected void onStop() {
webview.clearCache(true);
webview.clearView();
webview.clearHistory();
webview.destroy();
super.onStop();
}
}
Is there anybody who knows what iam doing wrong?
Thanks in advance.
With regards
OK well to trouble shoot this I would start by getting rid of (commenting out) all of your code below public boolean onKeyDown(int keyCode, KeyEvent event). Then you can see if its in your onCreate or not. But that's up to.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
final String urlToLoad = "http://www.yourWebsite.com";
wb.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wb.loadUrl(urlToLoad);
}
start here then start adding your extra code back in.
I have a webview in my app. I can see the title of the website on the title bar (I have custom title bar). However, i dont see anything in the view - the website is not viewable :(.. any suggestions ? Here's the code:
public class WebViewer extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
webView = (WebView) findViewById(R.id.webview);
String url = "http://www.google.com";
final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
title.setText("Loading...");
WebViewer.this.setProgress(progress * 100);
if(progress == 100)
title.setText(webView.getTitle());
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
This isn't needed. shouldOverrideUrlLoading is called before the url is loaded to give you a chance to handle loading yourself. What you're doing is loading the url over and over.
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29