Android web application: Application does not have sufficient geolocation permissions - android

I am trying to make an Android App that just shows a map inside a WebView component.
I have started with the How to Convert a Website into an Android Application using Android Studio tutorial and then try to allow the browser to use JavaScript and the position of the device.
To allow the WebView to use the position I have applied the following guides and answers
Enable GeoLocation On Webview
android webview geolocation
Enable Geolocation in a WebView (Android)
but, when I click on the current position button, I still get the following message:
Geolocation error: Application does not have sufficient geolocation
permissions..
I don't think this is a duplicate question as this is specific about Leafet map JS library and about a specific case.
What I have tried
At the moment my app is made of the following files:
MainActivity.java
package com.vinaysomawat.careerhigh;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
public class MainActivity extends ActionBarActivity {
//private WebView mywebview;
public class GeoWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
view.loadUrl(url);
return true;
}
}
//public class GeoWebChromeClient extends WebChromeClient {
public class GeoWebChromeClient extends android.webkit.WebChromeClient {
#Override
public void onGeolocationPermissionsShowPrompt(String origin,
//GeolocationPermissions.Callback callback) {
android.webkit.GeolocationPermissions.Callback callback){
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}
WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView)findViewById(R.id.webView);
WebSettings webSettings = mywebview.getSettings();
mywebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mywebview.getSettings().setBuiltInZoomControls(true);
mywebview.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.getSettings().setGeolocationEnabled(true);
mywebview.setWebChromeClient(new GeoWebChromeClient());
mywebview.getSettings().setAppCacheEnabled(true);
mywebview.getSettings().setDatabaseEnabled(true);
mywebview.getSettings().setDomStorageEnabled(true);
mywebview.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());
mywebview.loadUrl("https://domoritz.github.io/leaflet-locatecontrol/demo/");
}
#Override
public void onBackPressed(){
if(mywebview.canGoBack()) {
mywebview.goBack();
} else
{
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vinaysomawat.careerhigh"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/faviconcircular"
android:label="#string/app_name"
android:roundIcon="#mipmap/faviconcircular"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity" android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
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"
tools:context=".MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
How can I fix the geolocation problem? Thank you in advice.

Add this to your class with your permissions and then call it in onCreate
private fun checkPermissions() {
val permission = ActivityCompat.checkSelfPermission(
this#MainActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this#MainActivity,
permissions,
requestExternalStorageCode
)
}
}
Also do not forget to override permissions callback to check if user granted you permissions:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == requestExternalStorageCode) {
if (grantResults.size > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//to whatever you need after the permission is granted
} else {
finish()
}
}
}

Related

Access camera in android WebView to stream on agora

I am developing a web app which I would now like to make an android app for. For testing, a simple WebView is enough.
Now the issue is that whenever a regular desktop web browser asks for camera and microphone permissions, the app does nothing. It doesn't crash but it also doesn't join the stream.
What do I have to do in order to fix this?
So far, I have the following Code:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.techadvice">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-feature android:name="android.hardware.camera"/>
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.techadvice;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView=(WebView) findViewById(R.id.webview);
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.getSettings().setCacheMode(webSettings.LOAD_NO_CACHE);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("%some url%");
}
public class mywebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed(){
if(myWebView.canGoBack()) {
myWebView.goBack();
}
else{
super.onBackPressed();
}
}
}
I was mostly following this tutorial: https://youtu.be/2cWbepS1NZM
Thanks in advance!
In your onCreate you can check for permission and request it if the permission hasn't been granted already. You can try something like this:
int MY_PERMISSIONS_REQUEST_CAMERA = 0;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, MY_PERMISSIONS_REQUEST_CAMERA);
}

Handling Back Button in Android Webview

I develop an app with WebView and Html5, It works, clicking a link in the webpage goes to the next page in the html file inside my app. But when I click the phone's back button, it give me this error : Unfortunately, APPNAME has stopped. I want to go back to the previous page in the website instead.
The website build with : https://github.com/01org/appframework
MainActivity.java
package com.package.name;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView WebView;
//Back Button Code
#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);
}
//--------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
webView.loadUrl("file:///android_asset/index.html");
}
}
Activity_main.xml :
<?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="com.package.name.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
</android.support.constraint.ConstraintLayout>
AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
First,
Check if WebView is null.. rename the global variable to webView (private WebView webView;) and in onCreate change WebView webView=(WebView) findViewById(R.id.webview); to webView=(WebView) findViewById(R.id.webview);
You could also try moving the back button handling code to
#Override
public void onBackPressed()
{
....
}
instead of
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
...
}
as in:
#Override
public void onBackPressed()
{
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
as mentioned in https://developer.android.com/training/implementing-navigation/temporal.html#back-webviews

File upload in web view not working

I'm trying to make it possible to upload files via a web view but it is not happening currently, I'm looking at this famous answer and here are my
MainActivity.java
package ...;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient());
wv.setWebChromeClient(new WebChromeClient() {
//The undocumented magic method override
//Eclipse will swear at you if you try to put #Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
MainActivity.this.showAttachmentDialog(uploadMsg);
}
// For Android > 3.x
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
MainActivity.this.showAttachmentDialog(uploadMsg);
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
MainActivity.this.showAttachmentDialog(uploadMsg);
}
});
wv.loadUrl("http://www.tizag.com/phpT/fileupload.php");
}
private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
this.mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="....MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
and permissions
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="...">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
If you set these 3 files and run it, I have set the webview url to a page which contains a file input, you will see it is not working. I am using API version 19+
I'm going to cry soon.
So I came across this GIT repo that I think will help - https://github.com/GoogleChrome/chromium-webview-samples
Look at the "input-file-example".
I had issues with the Build.Gradle and found the solution here-
Gradle DSL method not found: 'runProguard'
Hope this helps!

how to mopub ads add in application

i want to add ads banner in my application i have integrate mopub sdk with my project and import and add library to the my project now my question is how to add banner disply and where code i have to write in my application java code and xml code about ads so please help enyone
my java code and mainifest file code is given below
mainactivity.java
package com.example.ration;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView web;
int k;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://dcs-dof.gujarat.gov.in/live-info.htm");
// web.getProgress();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,menu.NONE,"About");
menu.add(0,2,menu.NONE,"Feedback");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id == 1)
{
Toast.makeText(MainActivity.this,"About",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity.this,about.class);
startActivity(i);
}
else {
Toast.makeText(MainActivity.this,"Feedback",Toast.LENGTH_LONG).show();
Intent i2 =new Intent(MainActivity.this,feedback.class);
startActivity(i2);
}
return super.onOptionsItemSelected(item);
}
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
super.onResume();
// .... other stuff in my onResume ....
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this,"Press Again to Exit", Toast.LENGTH_SHORT).show();
}
}
and my manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ration"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ration.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.ration.about"></activity>
<activity android:name="com.example.ration.feedback"></activity>
<activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.mopub.mobileads.MraidBrowser" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation"/>
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<activity android:name="com.millennialmedia.android.MMActivity" android:theme="#android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|keyboard" />
<activity android:name="com.millennialmedia.android.VideoPlayer" android:configChanges="keyboardHidden|orientation|keyboard" />
</application>
</manifest>
I think you should better go through the guide for Mopub Banner Ads Integration which explains you the steps of banner ads integration.
Hope this will help you.
Use view in XML file
<com.mopub.mobileads.MoPubView
android:id="#+id/mrect_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
in activity Load code
pass id of view in below function ,unit id of ur app,and keywords of cataogry of ads
public void loadMoPubView(MoPubView moPubView, String adUnitId, String keywords) {
if (moPubView == null) {
Utils.logToast(LockScreenActivity.this, "Unable to inflate MoPubView from xml.");
//Toast.makeText(this, "Unable to inflate MoPubView from xml.", Toast.LENGTH_SHORT).show();
return;
}
try {
Utils.validateAdUnitId(adUnitId);
} catch (IllegalArgumentException exception) {
Utils.logToast(LockScreenActivity.this, exception.getMessage());
return;
}
moPubView.setBannerAdListener(this);
moPubView.setAdUnitId(adUnitId);
moPubView.setKeywords(keywords);
moPubView.setAutorefreshEnabled(true);
moPubView.loadAd();
}

WebView with SherlockActionBar reloads on rotation

I know there are a lot of similar questions and believe me, I tried a lot, but nothing worked out for me. I have a Android App which uses a Webview. You can find my App in the Play Store:
https://play.google.com/store/apps/details?id=at.rk.rps&feature=search_result#?t=W251bGwsMSwxLDEsImF0LnJrLnJwcyJd
After a few month in the Play Store I decided to make an update and to use the Android Design Guidelines for my new Design, so a Actionbar was necessary. I went with ActionbarSherlock and everything is fine, except the Webview is now reloading every time I rotate the phone. In my last version I handled this problem with this answer and it worked:
My Webview Reloads When I Change From Portrait To Landscape
But now it won't work again. I have tried that solution too: (I also left a comment at the end of the site)
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
With the same disappointing result. I'm not even sure if ActionbarSherlock is the problem, because I've changed a lot, but it is impossible to recreate the old state!
This is what the App looks like (This is only in the new version implemented, you can't find it in the version which is publish on the Play Store):
Here's a part of my code, first the manifest, then the layout and afterwards the java code:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.rk.rps"
android:versionCode="8"
android:versionName="0.4" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/rps"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar" >
<activity
android:name=".RPSActivity"
android:configChanges="orientation"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".DonateActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".PayPalActivity"
android:configChanges="orientation"
android:label="#string/app_name" >
</activity>
<activity
android:name=".LoadingScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Layout:
<?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" >
<TextView
android:id="#+id/paypal_loadingtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="#string/paypal_loadingtext"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/rk_red" />
<ProgressBar
android:id="#+id/paypal_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/paypal_loadingtext"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp" />
<WebView
android:id="#+id/paypal_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
</RelativeLayout>
Java code:
package at.rk.rps;
import org.apache.http.util.EncodingUtils;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class PayPalActivity extends SherlockActivity {
private ActionBar actionbar;
private WebView webview;
private TextView loadingtxt;
private ProgressBar progressbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.paypal);
initUI();
}
private void initUI() {
actionbar = getSupportActionBar();
actionbar.setTitle(R.string.paypal_actionbar_headline);
actionbar.setHomeButtonEnabled(true);
this.actionbar.setDisplayHomeAsUpEnabled(true);
loadingtxt = (TextView) findViewById(R.id.paypal_loadingtext);
progressbar = (ProgressBar) findViewById(R.id.paypal_progressbar);
webview = (WebView) findViewById(R.id.paypal_webview);
webview.requestFocus(View.FOCUS_DOWN); // Enables the keyboard in landscape mode
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
webview.getSettings().setJavaScriptEnabled(true); // Enables JavaScript
webview.getSettings().setBuiltInZoomControls(true); // Enables zoom
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // Displays scrollbars outside the view
webview.setScrollbarFadingEnabled(false);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
loadingtxt.setVisibility(TextView.VISIBLE);
progressbar.setVisibility(ProgressBar.VISIBLE);
webview.setVisibility(WebView.GONE);
progressbar.setProgress(progress);
if(progress == 100) {
loadingtxt.setVisibility(LinearLayout.GONE);
progressbar.setVisibility(LinearLayout.GONE);
webview.setVisibility(WebView.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient());
byte[] post = EncodingUtils.getBytes("cmd=_s-xclick&hosted_button_id=VRM63MEY4J936", "BASE64");
webview.postUrl("https://www.paypal.com/cgi-bin/webscr", post);
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
#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);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater i = getSupportMenuInflater();
i.inflate(R.menu.paypal_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
finish();
return(true);
case R.id.paypal_actionbar_reload:
webview.reload();
return(true);
default:
return super.onOptionsItemSelected(item);
}
}
}
I found the answer here:
https://stackoverflow.com/a/9550231/1254514
"Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation."
So I had to do the following:
Add in your Android manifest to the activity you like:
android:configChanges="keyboardHidden|orientation|screenSize"
Then override the function in your activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Maybe if you try something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.paypal);
initUI();
if(savedInstanceState != null)
{
/** Restoring the web Ui state. */
webView.restoreState(savedInstanceState);
}
}
#Override
public void onSaveInstanceState(Bundle outState)
{
/** Saving the webview state. */
webView.saveState(outState);
}
This is untested but it seems like you are not saving the state of the webview. I am using this code in a Fragment and it seems to save the state of the webview.

Categories

Resources