Liyr Web Brower

Just created a custom web browser. After tweaking it a bit I really like it. However I love that I learned how to not reset states when I change the orientation.

To do that I first created a FrameLayout to act as a holder for my WebView.


</LinearLayout>

<FrameLayout
 android:id="@+id/wvBrowser"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

</LinearLayout>

Then I placed the WebView inside the layout.


protected void UI() {
 LinearLayout bgColor = (LinearLayout) findViewById(R.id.LOLiyr);
 bgColor.setBackgroundColor(Color.WHITE);
 wvHolder = ((FrameLayout) findViewById(R.id.wvBrowser));

if(wv == null) {

wv = new WebView(this);
 wv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
 wv.getSettings().setJavaScriptEnabled(true);
 wv.getSettings().setLoadWithOverviewMode(true);
 wv.getSettings().setUseWideViewPort(true);
 wv.getSettings().setLoadsImagesAutomatically(true);
 wv.getSettings().setSupportZoom(true);
 wv.setWebViewClient(new wvClient());
 wv.requestFocus(View.FOCUS_DOWN);
 wv.setOnTouchListener(new View.OnTouchListener() {

@Override
 public boolean onTouch(View v, MotionEvent event) {
 // TODO Auto-generated method stub
 switch(event.getAction())
 {
 case MotionEvent.ACTION_DOWN:
 case MotionEvent.ACTION_UP:
 if(!v.hasFocus())
 {
 v.requestFocus();
 }
 break;
 }
 return false;
 }
 });
 wv.loadUrl("http://google.com");
 }
 wvHolder.addView(wv);
 }

public void onCofigurationChanged(Configuration config) {

if (wv != null)
 wvHolder.removeView(wv);

super.onConfigurationChanged(config);
 setContentView(R.layout.liyr);
 UI();
 }

The last step is to add


android:configChanges="orientation"

to the manifest. I’m not 100% sure why but I know it makes sure that a new activity isn’t started and that the config is kept. I love it! I could use this technique for everything.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s