Generally installed web browser in smartphone.
But sometimes we need web browser our application.
Recently I added web browser in my application Labor pains Timer V3.
Labor pains Timer V3
Okay let's go~
Layout
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:orientation="horizontal">
- <ImageButton
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:id="@+id/btnBack"
- android:scaleType="centerCrop"
- android:src="@drawable/back"/>
- <ImageButton
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:id="@+id/btnForward"
- android:scaleType="centerCrop"
- android:src="@drawable/forward"/>
- <EditText
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="7"
- android:id="@+id/address"
- android:singleLine="true"
- />
- <ImageButton
- android:id="@+id/btnRefresh"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:scaleType="centerCrop"
- android:src="@drawable/refresh"/>
- </LinearLayout>
- <WebView
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="10"
- android:id="@+id/internet"
- />
Line 7 : imagebutton is back
Line 14 : imagebutton is forward
Line 21 : EditText is address
Line 28 : imagebutton is refresh
Line 37 : webview is web browser
layout is simple!!
Now JAVA code
First, this setting is enable javascript by web browser.
- final WebSettings webSetting = internet.getSettings();
- webSetting.setJavaScriptEnabled(true);
- webSetting.setLoadWithOverviewMode(true);
- webSetting.setUseWideViewPort(true);
button setting(on click listener)
- @Override
- switch(v.getId()){
- case R.id.btnBack:
- internet.goBack();
- break;
- case R.id.btnForward:
- internet.goForward();
- break;
- case R.id.btnRefresh:
- if(!add.contains("http://"))
- add="http://"+add;
- internet.loadUrl(add);
- break;
- }
- }
internet means WebView.
Line 7 : goBack() method is go to last back page.
Line 11: goForward() method is go to last forward page.
Line 15~17 : Checking EditText. If not include 'http://' in edit text ,automatically add.
Add function 1
- internet.setWebViewClient(new WebViewClient(){
- @Override
- address.setText(url);
- super.onPageFinished(view, url);
- }
- });
This is page changed, display new page's address in EditText.
Add function 2
Anti change display.
If you use display keyboard, your layout is changing!!
But you use this code, not changed your layout!!
- @Override
- if((actionId==EditorInfo.IME_ACTION_NEXT)||(event !=null &&event.getKeyCode()==KeyEvent.KEYCODE_ENTER)){
- if(!add.contains("http://"))
- add="http://"+add;
- internet.loadUrl(add);
- }
- return false;
- }
No comments:
Post a Comment