package mediatutorial.web.id.metuwebbrowser;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import java.net.URL;
public class MainActivity extends ActionBarActivity {
String theUrl = "";
public WebView webBrowserKu;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webBrowserKu = (WebView)findViewById(R.id.myWebView);
webBrowserKu.setWebViewClient(new WebViewClient());
WebSettings theWebSetting = webBrowserKu.getSettings();
theWebSetting.setJavaScriptEnabled(true);
//
//yuk kita menangkap intent (bila ada)
Intent intent = getIntent();
Uri data = intent.getData();
URL url = null;
try {
url = new URL(data.getScheme(), data.getHost(),data.getPath());
} catch (Exception e) {
e.printStackTrace();
}
if(url != null){
theUrl = url.toString();
webBrowserKu.loadUrl(theUrl);
//
EditText theEditText = (EditText)findViewById(R.id.urlTxt);
theEditText.setText(theUrl);
}
//berikut juga bisa digunakan
/*String myString = null;
Intent intent = getIntent();
myString = intent.getDataString();
if(myString != null){
webBrowserKu.loadUrl(myString);
//
EditText theEditText = (EditText)findViewById(R.id.urlTxt);
theEditText.setText(myString);
}*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void gotoUrl(View view) {
EditText theEditText = (EditText)findViewById(R.id.urlTxt);
theUrl = theEditText.getText().toString();
//
webBrowserKu.loadUrl(theUrl);
}
public void goRefresh(MenuItem item) {
webBrowserKu.reload();
}
public void goStop(MenuItem item) {
webBrowserKu.stopLoading();
}
public void goBack(MenuItem item) {
webBrowserKu.goBack();
}
public void goForward(MenuItem item) {
webBrowserKu.goForward();
}
}