- final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
- mainWebView = (WebView) findViewById(R.id.mainWebView);
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
+ final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
+ final Activity mainWebViewActivity = this;
+
+ final ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ // Remove the title from the action bar.
+ actionBar.setDisplayShowTitleEnabled(false);
+
+ // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+ actionBar.setCustomView(R.layout.app_bar);
+ actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
+
+ // Set the "go" button on the keyboard to load the URL in urlTextBox.
+ EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
+ urlTextBox.setOnKeyListener(new View.OnKeyListener() {
+ public boolean onKey(View v, int keyCode, KeyEvent event) {
+ // If the event is a key-down event on the "enter" button, load the URL.
+ if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
+ (keyCode == KeyEvent.KEYCODE_ENTER)) {
+ // Load the URL into the mainWebView and consume the event.
+ try {
+ loadUrlFromTextBox();
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ // If the enter key was pressed, consume the event.
+ return true;
+ }
+ // If any other key was pressed, do not consume the event.
+ return false;
+ }
+ });
+ }