WebView scroll up automatically when scrolling it down little - java

In web view when I scroll the page little bit down then it scroll up automatically on some pages not all pages but on some pages.Why this happen and what is it solution
Here is xml code
<android.support.design.widget.CoordinatorLayout android:id="#+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<WebView
android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
Here is my code you can check this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
bindLayout();
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
CookieManager.getInstance().setCookie(url, cookie);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
ProgressDialogHelper.showProgress(WebViewActivity.this);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
webView.loadUrl(url);
}
Here is my java code I set the cookie also to web view and I set also layer type. I search that everyone says to set layer type then this problem solved but it does not solved.

try this
final WebSettings settings = mWebView.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.setVerticalScrollBarEnabled(false);

Just remove
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
all others are OK

try this in AndroidManifest.xml
set android:hardwareAccelerated="true" in the application and activity tag.

Related

Android - WebView refreshing when scrolling down

This is probably a stupid question at this stage but I've been looking through the threads online and none of the solutions have worked for me so far.
I have an Activity that has a Webview nested inside a SwipeRefreshLayout to enable the Pull-To-Refresh functionality to refresh the page. The problem arises where when I try to scroll to the top the web page, the OnRefresh callback is being called. Below is some snippets of the relevant code I have tried related to fixing the problem. I tried to implementing the ViewTreeObserver.OnScrollChangedListener interface to enable the refresh function when the scroll is at the top. I am wondering if I am missing something.
Browser.java
public class Browser extends AppCompatActivity implements Common, ViewTreeObserver.OnScrollChangedListener {
private final String TAG = getClass().getSimpleName();
private ProgressBar loadingPageBar;
private Toolbar toolbar;
private WebView browser;
private WebSettings webSettings;
private SwipeRefreshLayout refreshLayout;
private String urlLink;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
Log.i(TAG, "onCreate: Called");
declareVariables();
getIntentData();
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setNavigationOnClickListener(view -> onBackPressed());
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
toolbar.setSelected(true);
webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
refreshLayout.setRefreshing(false);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
});
browser.setWebChromeClient(new ChromeClient());
enableHtml5Features();
browser.clearCache(true);
browser.loadUrl(urlLink);
refreshLayout.setRefreshing(true);
refreshLayout.getViewTreeObserver().addOnScrollChangedListener(this);
refreshLayout.setOnRefreshListener(() -> browser.reload());
}
#Override
public void onScrollChanged() {
System.out.println(browser.getScrollY());
if (browser.getScrollY() == 0)
refreshLayout.setEnabled(true);
else
refreshLayout.setEnabled(false);
}
}
activity_browser.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Browser">
<androidx.appcompat.widget.Toolbar
android:id="#+id/webview_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/white"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ColoredBackArrow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/view3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/greyDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/webview_toolbar" />
<ProgressBar
android:id="#+id/loadingBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="3dp"
app:layout_constraintBottom_toBottomOf="#id/webview_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="parent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/view3">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fitsSystemWindows="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view3" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Any help would be appreciated. Thanks.
Add SwipeRefreshLayoutListener i.e. setOnRefreshListener like below:
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refreshLayout.setRefreshing(true);
loadWebView();
}
});
Whole Class with SwipeRefreshLayout Layout :
public class WebViewActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private WebSettings webSettings;
private View view3;
private ProgressBar loadingBar;
private SwipeRefreshLayout refreshLayout;
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
view3 = findViewById(R.id.view3);
loadingBar = findViewById(R.id.loadingBar);
refreshLayout = findViewById(R.id.swipeContainer);
webview = findViewById(R.id.webview);
webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
loadWebView();
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refreshLayout.setRefreshing(true);
loadWebView();
}
});
}
private void loadWebView(){
String urlLink="https://stackoverflow.com/questions/57478675/android-webview-refreshing-when-scrolling-down";
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
refreshLayout.setRefreshing(false);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
});
webview.setWebChromeClient(new WebChromeClient());
webview.clearCache(true);
webview.loadUrl(urlLink);
}
}
I hope its work for you.

Show toast error and prevent display of webview when no connection is available

When there is no connection available it displays the deafult browser error page, What is want is simply not to display any browser error page instead a toast message and a blank screen.
My code:
public class EarnFragment extends Fragment {
WebView mWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_earn, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}
}
if you want to display black screen instead of webview, first you need to change your layout file.
then you check internet connection before load the url.
check if internet connection not available taht time hide your webview and display your Toast message and relative layout.
Othervise hide Relativelayout noConnection.
Step 1 : create layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webViewData"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="#+id/noConnection"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
Step 2 : Check your internet connection using this function.
public static boolean checkInternetConnection(Context context) {
if (context != null) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isConnectedOrConnecting();
} else {
return false;
}
} else {
return false;
}
}
At the end do this in your code, before load you url,
public class EarnFragment extends Fragment {
WebView mWebView;
RelativeLayout noConnection;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_earn, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
noConnection = (RelativeLayout)v.findViewById(R.id.noConnection);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
if(checkInternetConnection(getActivity())){
noConnection.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
mWebView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");
}else{
noConnection.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.GONE);
}
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}
Step 1: Make include_error_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ebebeb"
android:gravity="center"
android:orientation="vertical">
<!--android:background="#ebebeb"-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/labelError"
android:layout_centerHorizontal="true"
android:src="#drawable/img_icon_error_list" />
<TextView
android:id="#+id/labelError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="10dp"
android:text="#string/err_error_list"
android:textColor="#color/color_app_font_primary" />
<ProgressBar
android:id="#+id/errorProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/labelError"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
Step 2: Make fragment layout fragment_abc.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/errorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<include layout="#layout/include_error_list_view" />
</RelativeLayout>
</RelativeLayout>
Step 3: Bind Layout with JAVA.
public class AbcFragment extends Fragment {
private final String TAG = "AbcFragment";
private WebView webView;
private ProgressDialog progress;
private RelativeLayout errorView;
private boolean isShowErrorWiew = false;
private ProgressDialog progress;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_abc, container, false);
webView = (WebView) view.findViewById(R.id.webview);
errorView = (RelativeLayout) view.findViewById(R.id.errorView);
TextView labelError = (TextView) view.findViewById(R.id.labelError);
labelError.setText("Application unable to connect with internet.");
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
showProgressBarWithoutHide();
errorView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isShowErrorWiew = false;
showProgressBarWithoutHide();
webView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");
}
});
try {
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (!isShowErrorWiew) {
errorView.setVisibility(View.GONE);
}
hideProgressBar();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
isShowErrorWiew = true;
errorView.setVisibility(View.VISIBLE);
// Snackbar.make(webView, getString(R.string.err_process_webView), Snackbar.LENGTH_LONG).show();
}
});
webView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");
} catch (Throwable th) {
th.printStackTrace();
}
}
#Override
public void onResume() {
super.onResume();
webView.onResume();
}
#Override
public void onPause() {
super.onPause();
/*Todo Always Pause WebView Because Background video play is violating google policy*/
webView.onPause();
}
public void hideProgressBar() {
if (progress != null) {
progress.dismiss();
}
}
public void showProgressBarWithoutHide() {
if (progress == null) {
progress = new ProgressDialog(getActivity());
progress.setMessage(getString(R.string.please_wait));
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.show();
} else if (!progress.isShowing()) {
progress.show();
}
}
}

android setVisibility(LinearLayout.VISIBLE) not work in WebView #JavascriptInterface Function

activity_main.xml content
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="#+id/ny_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ny_sms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBlack"
android:isScrollContainer="false"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:visibility="gone"></LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java content
package com......;
import android.Manifest;
import android.blablabla.and.other.imports;
...
...
public class MainActivity extends AppCompatActivity {
static WebView mWebView;
static LinearLayout ny_sms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
ny_sms =(LinearLayout)findViewById(R.id.ny_sms);
mWebView = (WebView)findViewById(R.id.ny_web_view);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("file:///android_asset/main.html");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new WebAppInterface(this),"Android");
}
public static void showLayoutMA() {
mWebView.setVisibility(WebView.INVISIBLE);
ny_sms.setVisibility(LinearLayout.VISIBLE);
}
}
class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showLayout() {
MainActivity.showLayoutMA();
}
}
main.html content
<html>
<body>
<center>
Visiblity Test
</center>
</body>
</html>
all permissions ok. al settings ok, app version 28 (android 9 pie)
im click "Visiblity Test" link not change any screen activity,
and after scroll to bottom nofication top bar click toggle auto rotate screen button
auto refresh and show LinearLayout front of the webview control.
how to fix this problem.
not showing LinearLayout and webView not invisible with "Visiblity Test" clicked. after need a refreshing action.
Sorry my English is bad :(
im found this problem,
returned error "Only the original thread that created a view hierarchy can touch its views"
So then you need me: runOnUiThread()
extend this code...
public static void showLayoutMA() {
CurrentActivitiy.runOnUiThread(new Runnable() {
#Override
public void run() {
//mWebView.setVisibility(WebView.INVISIBLE);
ny_sms.setVisibility(LinearLayout.VISIBLE);
}
});
why not use direct runOnUiThread(new Runnable() {} function, because my function is static, im add "CurrentActivitiy" declaration of MainActivity.
static Activity CurrentActivitiy;
CurrentActivitiy = (Activity)MainActivity.this;
and after few hours happy ending, problem is resolved..

WebView not working in Android when used with OnClickListener

I am working on a simple Android App which uses WebView.
The code is:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editUrl"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/url_hint"/>
<Button
android:id="#+id/buttonGo"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/url_go"/>
</LinearLayout>
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1"
tools:context=".MainActivity" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
WebView mWebView;
EditText editText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.buttonGo);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = (EditText)findViewById(R.id.editUrl);
String url = editText.getText().toString();
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
});
}
}
However, this code doesn't seem to work. I don't know why. It was working perfectly when I did not add any EditText or Button, and the code in the MainActivity.java was:
public class MainActivity extends ActionBarActivity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://google.com";
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
});
}
}
I have no idea why this is happening because the app seemed to work fine before.
When I changed the layout, the app doesn't work.
Please help me in detail as I am new to Android.
Thanks.
PLEASE FEEL FREE TO SUGGEST ME A BETTER METHOD TO IMPLEMENT THIS ALSO
Create java object of all other view objects like web view before it and then try.
I just tried your code as is, and it worked. One thing to note though, you have to type in the full url, for example: http://www.example.com.
One improvement would be to make it so you just have to type in example.com by doing something like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = (EditText)findViewById(R.id.editUrl);
String url = editText.getText().toString();
if (!url.contains("www")){
url = "www." + url;
}
if (!url.contains("http")){
url = "http://" + url;
}
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
});

Error Gmail login OAuth android using webview

I tried to log in my app using Gmail log in Authentication using web view, when i run using emulator always shows the web page is not available.Please find the attached screen shoot.I do not understand how this comes, please any body suggest me how to get gmail log in authentication and user token.
Thanks in advance
Here is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String OAUTH_URL ="https://accounts.google.com/o/oauth2/auth";
String REDIRECT_URI="http://localhost";
String ACCESS_TYPE = "offline";
String RESPONSE_TYPE = "code";
String OAUTH_SCOPE="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/userinfo.email https://www.google.com/m8/feeds";
String CLIENT_ID = "";//confidential
String REQUEST = OAUTH_URL+"?redirect_uri="+REDIRECT_URI+"&response_type="+RESPONSE_TYPE+"&client_id="+CLIENT_ID+"&scope="+OAUTH_SCOPE+"&access_type="+ACCESS_TYPE;
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url,Bitmap bitmap) {
System.out.println("onPageStarted url->"+url);
}
#Override
public void onPageFinished(final WebView view, final String url) {
System.out.println("onPage finished url->"+url);
/*if (url.startsWith(Constants.OAUTH2PARAMS.getRederictUri())) {
webview.setVisibility(View.INVISIBLE);
if (!handled) {
new ProcessToken(url,oAuth2Helper).execute();
}
} else {
webview.setVisibility(View.VISIBLE);
}*/
}
});
webview.loadUrl(REQUEST);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Here iam attaching the screen shot![web page is not avilable]
[Here][1] is a perfect example.
[1]: http://tush.wordpress.com/2014/07/15/android-google-contact-api-3-0-example/
Use the authentication process from this example.
And also in your code clientID is blank it can't be empty for debug mode it should
be empty for only release mode of your application

Categories

Resources