I have an Activity named MyActivity. MyActivity uses WebView to show web pages.
I also have an options activity like below. O also have /res/xml/options.xml
When user changes something in the options, I run MyUtility.setOptionsChanged() method.
My problem is when user changes something in preferences XML. When user presses back button I need to reload the current webview URL with post data ?
Can you check the code below and where should I put that logic ? Should I add to MyOptionsActivity's onKeyDown method or ?
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
mContext = getApplicationContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.myweb);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new myWebViewClient());
String postData = MyUtility.getOptionsDataForPOSTURL(mContext);
mWebView.postUrl("http://example.com", EncodingUtils.getBytes(postData, "BASE64"));
}
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String postData = MyUtility.getOptionsDataForPOSTURL(mContext);
view.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
public class MyOptionsActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
Context mContext;
#Override
public void onCreate(Bundle savedInstanceState) {
mContext = getApplicationContext();
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName("myoptions");
addPreferencesFromResource(R.xml.options);
}
#Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
MyUtility.setOptionsChanged(mContext,true);
}
}
public class MyUtility {
public static void setOptionsChanged(Context pContext, Boolean pOptionsChanged) {
SharedPreferences prefs = pContext.getSharedPreferences("general", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("optionsChanged", pOptionsChanged);
editor.commit();
}
}
Regarding to #Hiren Dabhi's comment, best place is Activity's onRestart method. It worked good.
Related
Need some advice. I want to be able to redirect the user to a specific link in WebView. Example: we go to the first link and if it directs us to "google.com" then we will have to direct the user to "http://test.com". I made an implementation, put a redirect in shouldOverrideUrlLoading, however, after SplashScreen-loading image done its work, a white background appears and that's it. At the same time, if you remove the code for redirection, then after performing its work, the SplashScreen - loading image, the first link opens and the page from the Internet is displayed correctly. How do I redirect to other pages after opening the first one?
public class MainActivity extends AppCompatActivity {
private WebView webView;
private final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("http://test/start");
new TaskForRedirecting().execute();
}
public class TaskForRedirecting extends AsyncTask<Boolean, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Boolean doInBackground(Boolean... booleans) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Log.d(TAG, "Start loading here");
WebViewClient webViewClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("google.com")) {
view.loadUrl("http://test1.com");
return false;
} else if (url.contains("yahoo.com")) {
view.loadUrl("http://test2.com");
return false;
} else {
}
return true;
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString() == "google.com") {
view.loadUrl("http://test1.com");
view.loadUrl(request.getUrl().toString());
return false;
} else if (request.getUrl().toString() == "yahoo.com") {
view.loadUrl("http://test2.com");
return false;
}
return true;
}
};
webView.setWebViewClient(webViewClient);
Log.d(TAG, "Do in background: APP is Working!!!");
}
});
return false;
}
#Override
protected void onPostExecute(Boolean success) {
success = false;
if (success) {
Toast toast = Toast.makeText(MainActivity.this, "App is under maintenance!", Toast.LENGTH_SHORT);
toast.show();
} else {
Toast toast = Toast.makeText(MainActivity.this, "Let's start to work!", Toast.LENGTH_SHORT);
toast.show();
}
Log.d(TAG, "upload - final! ");
}
}
}
public class SplashScreenActivity extends AppCompatActivity {
long Delay = 6000;
private final String TAG = "SplashScreenActivity";
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
new SplashScreen().execute();
}
public class SplashScreen extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void...voids) {
Timer RunSplash = new Timer();
TimerTask ShowSplash = new TimerTask() {
#Override
public void run() {
finish();
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
}
};
RunSplash.schedule(ShowSplash, Delay);
Log.d(TAG, "ShowSplash - final! ");
return null;
}
}
}
Your implementation needs to change a bit, we don't need to set the webview client inside an AsyncTask, instead, we can set it directly but it should be set before we call loadurl.
I have removed the asynctask, and set the client in onCreate method before calling loadUrl.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClient webViewClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("google.com")) {
view.loadUrl("http://test1.com");
return true;
} else if (url.contains("yahoo.com")) {
view.loadUrl("http://test2.com");
return true;
}
return false;
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString() == "google.com") {
view.loadUrl("http://test1.com");
return true;
} else if (request.getUrl().toString() == "yahoo.com") {
view.loadUrl("http://test2.com");
return true;
}
return false;
}
};
webView.setWebViewClient(webViewClient);
webView.loadUrl("http://test/start");
}
UPDATE
Please check out the shouldOverrideUrlLoading Doc, You should return true when you want to cancel the current load, so when you want to load a different URL, then return true else return false to continue the current load. Returning false means the URL which is being loaded to the webview will continue, your custom URL will not be loaded.
I have an activity (AuthenticationActivity) on Android with an interface. Another class implements this interface. I need to parse an interface object for AuthenticationActivity so when one event happens, another class performs an action.
public class MyWebViewActivity extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new OAuthWebViewClient());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("https://www....");
}
private class OAuthWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(App.callbackUrl)) {
String urls[] = url.split("=");
// listener.onComplete(urls[1]); // HERE I NEED FOR LISTNER
return true;
}
return false;
}
}
public interface OAuthDialogListener {
public abstract void onComplete(String accessToken);
public abstract void onError(String error);
}
}
I have in other class:
OAuthDialogListener listener = new OAuthDialogListener() {
#Override
public void onComplete(String code) {
getAccessToken(code);
}
#Override
public void onError(String error) {
mListener.onFail("Authorization failed");
}
};
ann this class call:
Intent intent = new Intent(this, MyWebViewActivity.class);
startActivity(intent);
how the MyWebViewActivity clas uses listener?
inside OAuthWebViewClient class initialize the OAuthDialogListener and them implement it with MyWebViewActivity
try
public class MyWebViewActivity extends Activity implements OAuthWebViewClient {
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment_view frag=new Fragment_view();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.fragment,frag,"hey");
transaction.commit();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (inCustomView()) {
hideCustomView();
return true;
}
if ((mCustomView == null) && webView.canGoBack()) {
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Now here variables like inCustomView and hidecustomView are defined in another fragment. So how to add this back button support on activity while using variables from a fragment. So I basically want to implement back button support in Android activity while using the defined variables from a fragment.
public class Fragment_view extends Fragment{
private WebView webView;
private FrameLayout customViewContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private View mCustomView;
private myWebChromeClient mWebChromeClient;
private myWebViewClient mWebViewClient;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.fragment_view, container,false);
customViewContainer = (FrameLayout)view.findViewById(R.id.customViewContainer);
webView = (WebView)view. findViewById(R.id.webView);
mWebViewClient = new myWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
String htmlFile = "file:///" + Environment.getExternalStorageDirectory()
.getPath()
+ "/video_nsta/index.html";
webView.loadUrl(htmlFile);
return view;
}
public boolean inCustomView() {
return (mCustomView != null);
}
public void hideCustomView() {
mWebChromeClient.onHideCustomView();
}
#Override
public void onPause() {
super.onPause();
webView.onPause();
}
#Override
public void onResume() {
super.onResume();
webView.onResume();
}
#Override
public void onStop() {
super.onStop();
if (inCustomView()) {
hideCustomView();
}
}
class myWebChromeClient extends WebChromeClient {
#Override
public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
onShowCustomView(view, callback);
}
#Override
public void onShowCustomView(View view,CustomViewCallback callback) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
webView.setVisibility(View.GONE);
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.addView(view);
customViewCallback = callback;
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
if (mCustomView == null)
return;
webView.setVisibility(View.VISIBLE);
customViewContainer.setVisibility(View.GONE);
mCustomView.setVisibility(View.GONE);
customViewContainer.removeView(mCustomView);
customViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
}
it's a matter of properly distributing the back button.
first create a fragment that can handle it:
public abstract class BackButtonFragment extends Fragment{
public boolean onBackPressed(){
return false; // defaults to false
}
}
then you change all your fragments to extend from BackButtonFragment.
Then on your activity you will use the appropriate callback for backbutton (remove the onKeyDown from your activity and use this instead):
#Override public void onBackPressed() {
Fragment f = getFragmentManager().findFragmentById(R.id.fragment);
if(f instanceof BackButtonFragment){
if(((BackButtonFragment)f).onBackPressed())
return;
}
// if fragment didn't use back button do the normal stuff
super.onBackPressed();
}
then inside each one of your fragments you will override the method onBackPressed and return the correct value true or false depending on your needs.
I'm attempting to evaluate my preferences in my java code in order to enable/disable other options it they chose not to do other options... So far i'm trying to only implement the OnPreferenceClickListener however i never see the toast from the changes. What am i doing wrong? There seem to be alot of other questions like this but i cannot see my error in reference to them.
public class UserSettingActivity extends PreferenceActivity implements OnPreferenceClickListener{
SharedPreferences mPreferences;
Boolean frequency;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
#Override
public boolean onPreferenceClick(Preference preference) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
frequency = mPreferences.getBoolean("frequency", true);
Context context = getApplicationContext();
Toast.makeText(context, "Hello toast 0!", Toast.LENGTH_LONG).show();
if (!frequency) {
Context context2 = getApplicationContext();
Toast.makeText(context2, "Hello toast 1!", Toast.LENGTH_LONG).show();
} else if (preference.getKey().equals("schedulestop")) {
} else if (preference.getKey().equals("priority")) {
} else {
Context context3 = getApplicationContext();
Toast.makeText(context3, "Hello toast 0!", Toast.LENGTH_LONG).show();
}
return false;
}
}
You have to register for PreferenceClickListener each individual preference
somePreference.setOnPreferenceClickListener(this);
or you can use getSharedPreferences().registerOnSharedPreferenceChangeListener for all preferences.
public class UserSettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
SharedPreferences mPreferences;
Boolean frequency;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
#SuppressWarnings("deprecation")
#Override
protected void onPause()
{
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#SuppressWarnings("deprecation")
#Override
protected void onResume()
{
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#SuppressWarnings("deprecation")
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("schedulestop")) {
// do something
}
else if (key.equals(......
}
}
first implement "OnSharedPreferenceChangeListener"
PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
Then
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
settings.registerOnSharedPreferenceChangeListener(this);
}
THEN
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
switch (key) {
case "pref_key_auto_delete":
boolean notificationStatus = SP.getBoolean(key, false);
Log.d("stat", String.valueOf(notificationStatus));
break;
case "pref_key_notification_list":
String downloadType = SP.getString(key, "n/a");
Log.d("stat", String.valueOf(downloadType));
break;
}
Here is my Code:
public class SampleFB1Activity extends Activity {
WebView web2,childView =null;
private LinearLayout parentLayout;
private Activity MyActivity;
private String requestUrl="http://example.com/test.html";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);
parentLayout =(LinearLayout)findViewById(R.id.parentlayout);
MyActivity = this;
web2 = new WebView(this);
web2.setLayoutParams(getLayoutParams());
web2.setWebViewClient(new FaceBookClient());
web2.setWebChromeClient(new MyChromeClient());
web2.getSettings().setJavaScriptEnabled(true);
web2.getSettings().setAppCacheEnabled(true);
web2.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
web2.getSettings().setSupportMultipleWindows(true);
web2.getSettings().setSupportZoom(true);
web2.getSettings().setBuiltInZoomControls(true);
parentLayout.addView(web2);
web2.loadUrl(requestUrl);
}
private LayoutParams getLayoutParams() {
// TODO Auto-generated method stub
return new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
}
final class MyChromeClient extends WebChromeClient{
#Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
childView = new WebView(SampleFB1Activity.this);
childView.getSettings().setJavaScriptEnabled(true);
childView.getSettings().setSupportZoom(true);
childView.getSettings().setBuiltInZoomControls(true);
childView.setWebViewClient(new FaceBookClient());
childView.setWebChromeClient(this);
childView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
parentLayout.addView(childView);
childView.requestFocus();
web2.setVisibility(View.GONE);
WebView.WebViewTransport transport =(WebView.WebViewTransport)resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
return true;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
MyActivity.setProgress(newProgress*100);
}
#Override
public void onCloseWindow(WebView window) {
parentLayout.removeViewAt(parentLayout.getChildCount()-1);
childView =null;
web2.setVisibility(View.VISIBLE);
web2.requestFocus();
}
}
private class FaceBookClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("REQUEST URL",url);
return false;
}
}
#Override
public void onBackPressed() {
if(childView != null && parentLayout.getChildCount()==2){
childView.stopLoading();
parentLayout.removeViewAt(parentLayout.getChildCount()-1);
if(web2.getVisibility() == View.GONE)
web2.setVisibility(View.VISIBLE);
}else{
super.onBackPressed();
}
}
In version 3 I am able to see only half comment box and in 4 instead of comment box
it is showing button (Login to facebook to post comment) but will not display comment box.
Please help...