android - How to add tabs dynamically that can be linked to a users choice of webpage -


i developing application uses tabs each tab being linked webpage user able see , interact using webview. having trouble implementing add command user able use add tab url of choice works others

below code

here main java file other files use

public class universityofcolorado extends tabactivity {   @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      tabhost host=gettabhost();      host.addtab(host.newtabspec("one")             .setindicator("google")             .setcontent(new intent(this, hello.class)));      host.addtab(host.newtabspec("two")                     .setindicator("colorado main site")                     .setcontent(new intent(this, coloradomainsitebrowser.class)));      host.addtab(host.newtabspec("three")                     .setindicator("culearn")                     .setcontent(new intent(this, culearnbrowser.class)));      host.addtab(host.newtabspec("four")             .setindicator("culink")             .setcontent(new intent(this, culinkbrowser.class)));      host.addtab(host.newtabspec("five")             .setindicator("mycuinfo")             .setcontent(new intent(this, mycuinfobrowser.class)));      host.addtab(host.newtabspec("six")             .setindicator("campus map")             .setcontent(new intent(this, campusbrowser.class)));      host.addtab(host.newtabspec("seven")             .setindicator("notes")             .setcontent(new intent(this, notepadv3.class))); }            // inflates menu when "menu key" pressed     @override     public boolean oncreateoptionsmenu(menu menu) {         menuinflater inflater = getmenuinflater();         inflater.inflate(r.menu.menu, menu);         return true;     } } 

then have each webpage defined in seperate java file main file calls below 1 of them

public class coloradomainsitebrowser extends activity {  webview webview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); webview = (webview) findviewbyid(r.id.webview); webview.setwebviewclient(new hellowebviewclient()); webview.getsettings().setjavascriptenabled(true); webview.loadurl("http://colorado.edu/"); } private class hellowebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); return true; } } public boolean onkeydown(int keycode, keyevent event) { if ((keycode == keyevent.keycode_back) && webview.cangoback()) { webview.goback(); return true; } return super.onkeydown(keycode, event); } } 

i have menu defined in main file need construct methods buttons suppose do. great

ok thought i've answered question here

besides, seem replicate similar questions here , here

like i've told you, can acomplish creating activity accepts url of intent.

taking code base start:

browser.java

import android.app.activity; import android.os.bundle; import android.view.keyevent; import android.webkit.webview; import android.webkit.webviewclient;   public class browser extends activity {      private webview webview;      private string url;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.browser);          bundle extras = getintent().getextras();          if (extras == null) {              url = "http://www.evonytools.org/";          } else {              this.url = extras.getstring("url");         }          getwebview();      }      public void getwebview() {          webview = (webview) findviewbyid(r.id.webview);         webview.setwebviewclient(new hellowebviewclient());         webview.getsettings().setjavascriptenabled(true);         webview.loadurl(this.url);     }      private class hellowebviewclient extends webviewclient {          @override         public boolean shouldoverrideurlloading(webview view, string url) {             view.loadurl(url);             return true;         }     }      public boolean onkeydown(int keycode, keyevent event) {         if ((keycode == keyevent.keycode_back) && webview.cangoback()) {         webview.goback();         return true;         }     return super.onkeydown(keycode, event);     } } 

layout/broswer.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" >     <webview         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:id="@+id/webview"     /> </linearlayout> 

main.java

public class main extends tabactivity{          private tabhost tabhost;          private edittext addressbar;          private final static string default_url = "http://www.evonytools.org/";          private int z = 0;          /** called when activity first created. */         @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.tabs_main);              this.tabhost = gettabhost();  // activity tabhost             this.addressbar = (edittext) findviewbyid(r.id.address_bar);             this.addressbar.settext(default_url);               imagebutton addbtn = (imagebutton) findviewbyid(r.id.add_btn);              addbtn.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                      addmethod();                     }             });              intent openbrowser = new intent();              openbrowser.setclass(this, browser.class);             openbrowser.putextra("url", default_url);             tabhost.addtab(tabhost.newtabspec("main").setindicator(gethost(default_url)).setcontent(openbrowser));          }             private void addmethod() {              string websiteurl = validateurl(addressbar.gettext().tostring().trim());             string websitename = gethost(websiteurl);              intent openbrowser = new intent();              openbrowser.setclass(this, browser.class);             openbrowser.putextra("url", websiteurl);              tabhost.addtab(tabhost.newtabspec(websitename + integer.tostring(z)).setindicator(websitename).setcontent(openbrowser));              ++z;         }          private void deletemethod() {              // since can't delete tab             // hide              int position = tabhost.getcurrenttab();              if (position != 0 ) {                  tabhost.getcurrenttabview().setvisibility(8);                 tabhost.setcurrenttab(0);             }         }          // inflates menu when "menu key" pressed         @override         public boolean oncreateoptionsmenu(menu menu) {             menuinflater inflater = getmenuinflater();             inflater.inflate(r.menu.menu, menu);             return true;         }          // method called once menu selected         @override         public boolean onoptionsitemselected(menuitem item) {               switch (item.getitemid()) {                  case r.id.add:                      addmethod();                      break;                  case r.id.delete:                      deletemethod();                      break;             }             return true;         }          private string validateurl(string url) {              stringbuffer urlb = new stringbuffer();              // checks if addressbar has valid url             // can add stuff here in order validate             // example             if (url.startswith("http://")) {urlb.append(url);} else {urlb.append("http://");}              try {                 url urltry = new url(urlb.tostring());                  return urlb.tostring();              } catch (malformedurlexception e) {                  return "http://www.google.com/";             }            }          private string gethost(string url) {              try {                  url urltry = new url(url);                  return urltry.gethost().replace("www.", "").replace(".com", "").replace(".org", "").replace(".net", "");              } catch (malformedurlexception e) {                  return "browser";             }         } } 

tabs_main.xml

<?xml version="1.0" encoding="utf-8"?> <tabhost xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@android:id/tabhost"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <linearlayout         android:orientation="vertical"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:padding="5dp">         <tabwidget             android:id="@android:id/tabs"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:tag="tabpane"              />         <relativelayout         android:orientation="vertical"         android:layout_width="fill_parent"         android:layout_height="wrap_content">             <edittext                 android:id="@+id/address_bar"                 android:layout_width="270px"                 android:layout_height="50px"                 android:layout_alignparentleft="true"                 android:layout_alignparenttop="true"             />             <imagebutton                 android:id="@+id/add_btn"                 android:layout_width="50px"                 android:layout_height="50px"                 android:src="@android:drawable/ic_menu_add"                 android:background="@android:color/transparent"                 android:layout_torightof="@id/address_bar"             />          </relativelayout>         <framelayout             android:id="@android:id/tabcontent"             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:padding="2dp" />     </linearlayout>     

hope i'm not doing homework.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -