Android | Browser Acitvity

Files 1) SimpleBrowser.java      2) simplewebbrowser.xml     3) Manifest.xml


package myapplication.example1.com.hackonlycom;

import android.app.Activity;
import android.content.Context;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;


public class SimpleBrowser extends Activity implements OnClickListener {
    WebView ourBrow;
    Button bGo,bRefresh,bForward,bGoBack,bClearHistory;
    EditText url;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);
        setContentView(R.layout. simplewebbrowser);

        ourBrow=(WebView) findViewById(R.id. wvWebBrowser);

        ourBrow.getSettings().setJavaScriptEnabled( true) ;


        //ourBrow.getSettings().setLoadWithOverviewMode( true);        //ourBrow.getSettings().setUseWideViewPort( true);

        ourBrow.setWebViewClient( new OurViewClient());

        try{
            ourBrow.loadUrl( "http://www.hackonly.com");

        } catch(Exception e){
            e.printStackTrace();

        }




        bGo=(Button) findViewById(R.id. bGo);
        bRefresh=(Button) findViewById(R.id. bRefresh);
        bForward=(Button) findViewById(R.id. bForward);
        bGoBack=(Button) findViewById(R.id. bBack);
        bClearHistory=(Button) findViewById(R.id.bHistory);
        url=(EditText) findViewById(R.id. etBrowser);

        bGo.setOnClickListener( this);
        bRefresh.setOnClickListener( this);
        bForward.setOnClickListener( this);
        bGoBack.setOnClickListener( this);
        bClearHistory.setOnClickListener(this);

    }
    @Override    public void onClick(View arg0) {
        // TODO Auto-generated method stub        switch(arg0.getId()){


            case R.id.bGo :

                String website= "http://";
                website+= url.getText().toString();
                ourBrow.loadUrl(website);
                //Hiding Keyboard after using edittext                InputMethodManager imm =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE );
                imm.hideSoftInputFromInputMethod( url.getWindowToken(), 0);


                break;
            case R.id.bBack :
                if(ourBrow .canGoBack())
                    ourBrow.goBack();


                break;
            case R.id.bForward :
                if(ourBrow .canGoForward())
                    ourBrow.goForward();

                break;
            case R.id.bHistory :
                ourBrow.clearHistory();

                break;
            case R.id.bRefresh :
                ourBrow.reload();
                break;


        }
    }


    private class OurViewClient extends WebViewClient {
        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub            view.loadUrl(url);
            return true ;
        }
    }
}




2. simplewebbrowser.xml


xml version= "1.0" encoding ="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width= "match_parent"    android:layout_height= "match_parent"    android:orientation= "vertical" >
    <LinearLayout        android:layout_width= "match_parent"        android:layout_height= "wrap_content"        android:orientation= "horizontal"        android:weightSum= "10">
        <EditText            android:id="@+id/etBrowser"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight= "2"            />



        <Button            android:id="@+id/bGo"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Go"            android:layout_weight="8" />
    </LinearLayout>
    <LinearLayout        android:layout_width= "match_parent"        android:layout_height= "wrap_content"        android:orientation= "horizontal"        android:weightSum= "8"        android:padding= "1px">
        <Button            android:id="@+id/bBack"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Go Back a Page"            android:layout_weight="2" />
        <Button            android:id="@+id/bForward"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Go Forward"            android:layout_weight="2" />
        <Button            android:id="@+id/bRefresh"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Refresh Page"            android:layout_weight="2" />
        <Button            android:id="@+id/bHistory"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Clear History"            android:layout_weight="2" />


    </LinearLayout >

    <WebView        android:id="@+id/wvWebBrowser"        android:layout_width= "fill_parent"        android:layout_height= "fill_parent" />

</LinearLayout>

3.) Manifest.xml update



<uses-permission android:name="android.permission.INTERNET"/>






Reactions:

0 comments:

Post a Comment