I have a small webView in one of my layouts that act as a news feed from my website.Every time I changed rotation it would reload the web page.
so I edited the code to include a on config change and on restore state.Then adjusted my android manifest to include the config changes which from the dev site says to include orientation and screen size.
This solves the problem of saving the state but in turn creates a new problem which is I have different size layouts for when its in landscape mode on certain devices. but with the manifest set how it is it overrides screen size. is there away to save the state but with out using screen size in my manifest. so then i could use my landscape layout
here is my code for the orientation changes.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_homescreen);
if (savedInstanceState != null)
((WebView)findViewById(R.id.neweview)).restoreState(savedInstanceState);
svHomeScreen = (ScrollView) findViewById(R.id.svHomeScreen);
svHomeScreen.setScrollbarFadingEnabled(false);
wv = (WebView) findViewById(R.id.neweview
);
wv.setScrollbarFadingEnabled(false);
wv.getSettings().setJavaScriptEnabled(true);
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.NORMAL);
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
if (Build.VERSION.SDK_INT >= 19) {
// chromium, enable hardware acceleration
wv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
// older android version, disable hardware acceleration
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
wv.canGoBackOrForward(5);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.setPadding(0, 0, 0, 0);
wv.loadUrl(NEWS);
then my manifest file has
<activity
android:name=".Homescreen"
android:label="@string/title_activity_homescreen"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
/>
ive added a container an a web view fragment but now the view disappears when rotation is landscape. then disappears completly when i switch back. so i tried adding the save out state and savestate to it which loaded the webview when rotated to landscape but crashed the app when going back to portrait
Here's some sample code that places the WebView in the activity layout as a fragment. In portrait mode, the view is centered, in landscape, it is positioned at the bottom. Both orientations use the same single WebView instance.
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// load the fragment - we only need to do this when the activity is first created
// - not on a recreation due to screen rotation, etc
if (savedInstanceState == null) {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, new WebViewFragment());
ft.commit();
}
}
// this could go in a separate file
public static class WebViewFragment extends Fragment {
WebView mWebView;
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
View v = layoutInflater.inflate(R.layout.webviewfrag, null, false);
this.mWebView = (WebView)v.findViewById(R.id.NewsFeedWbview);
this.mWebView.loadUrl("https://google.com");
return v;
}
}
}
res/layout/main.xml (default/portrait mode)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- container layout for the webview fragment
- note the id for this view is the same in both this and the landscape version -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
res/layout-land/main.xml (landscape)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- container layout for the webview fragment
- note the id for this view is the same in both this and the portrait version -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
res/layout/webviewfrag.xml
<!-- make the WebView fill its container -->
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/NewsFeedWbview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With