Dec 1, 2014

Android Web browser


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

  1.     <LinearLayout
  2.        android:layout_width="match_parent"
  3.        android:layout_height="0dp"
  4.        android:layout_weight="1"
  5.        android:orientation="horizontal">
  6.        
  7.         <ImageButton
  8.            android:layout_width="0dp"
  9.            android:layout_height="match_parent"
  10.            android:layout_weight="1"
  11.            android:id="@+id/btnBack"
  12.            android:scaleType="centerCrop"
  13.            android:src="@drawable/back"/>
  14.         <ImageButton
  15.            android:layout_width="0dp"
  16.            android:layout_height="match_parent"
  17.            android:layout_weight="1"
  18.            android:id="@+id/btnForward"
  19.            android:scaleType="centerCrop"
  20.            android:src="@drawable/forward"/>
  21.         <EditText
  22.            android:layout_width="0dp"
  23.            android:layout_height="match_parent"
  24.            android:layout_weight="7"
  25.            android:id="@+id/address"
  26.            android:singleLine="true"           
  27.            /> 
  28.         <ImageButton
  29.            android:id="@+id/btnRefresh"
  30.            android:layout_width="0dp"
  31.            android:layout_height="match_parent"
  32.            android:layout_weight="1"
  33.            android:scaleType="centerCrop"
  34.            android:src="@drawable/refresh"/>
  35.     </LinearLayout>
  36.     <WebView
  37.        android:layout_width="match_parent"
  38.        android:layout_height="0dp"
  39.        android:layout_weight="10"
  40.        android:id="@+id/internet"
  41.        />

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.

  1.         final WebSettings webSetting = internet.getSettings();
  2.         webSetting.setJavaScriptEnabled(true);
  3.         webSetting.setLoadWithOverviewMode(true);
  4.         webSetting.setUseWideViewPort(true);

button setting(on click listener)

  1.         @Override
  2.     public void onClick(View v) {
  3.         switch(v.getId()){
  4.        
  5.         case R.id.btnBack:
  6.              
  7.             internet.goBack();
  8.                
  9.             break;
  10.         case R.id.btnForward:
  11.             internet.goForward();
  12.                
  13.             break;
  14.         case R.id.btnRefresh:
  15.             String add=address.getText().toString();
  16.             if(!add.contains("http://"))
  17.                 add="http://"+add;
  18.             internet.loadUrl(add);
  19.             break;
  20.         }
  21.        
  22.     }

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

  1.         internet.setWebViewClient(new WebViewClient(){
  2.             @Override
  3.             public void onPageFinished(WebView view, String url) {
  4.                 address.setText(url);
  5.                 super.onPageFinished(view, url);
  6.             }
  7.            
  8.         });
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!!

  1.     @Override
  2.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  3.         if((actionId==EditorInfo.IME_ACTION_NEXT)||(event !=null &&event.getKeyCode()==KeyEvent.KEYCODE_ENTER)){
  4.             String add=address.getText().toString();
  5.             if(!add.contains("http://"))
  6.                 add="http://"+add;
  7.             internet.loadUrl(add);
  8.         }
  9.         return false;
  10.     }


No comments:

Post a Comment