Ok so i'm trying to create a small app that opens 3 different webviews within 3 different tabs. At the moment I Have my tabhost created ok and a separate class for my webview but when I open the app it doesn't display.
Tabhost code
public class HelloTabWidgetActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HelloWebViewActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("albums").setIndicator("News",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("SaintsTV",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("artists").setIndicator("Fixtures",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
webview code
public class HelloWebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
I can't see a problem and I have read other peoples posts regarding TabHostContentFactory but I have no idea how to make it load the webview when the app opens. Any help would be greatly received.
The above code is based on the HelloWebView tutorial and the HelloTabWidget tutorial
I had tried the same things sometime back and later figured out that I did not declare appropraite Internet access permissions for the app. That was the reason, webview in the tabhost was getting blank and there was no error in the Logcat either.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Adding above lines to the AndroidManifest.xml had resolved my problem.
I assume the problem itself it's in the main layout, because I run your code fine wiht the only modification:
public class HelloWebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
setContentView(mWebView);
}
}
this is to get you starting,
I suggest to use the Tabhost layout file for only the tab and the webview controller (and other intent layouts) should be in other layout files. For this you can use the tutorial:http://joshclemm.com/blog/?p=136
I hope this helps
Related
An android app I am making using with tabs widget through webview to show webpage inside the application.
The app while executing doesn't show proper data.
It only shows a html page that I added for a particular tab. But the html page is seen in all the tabs. And the main content is not visible.
http://prntscr.com/vgcoj
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th = (TabHost) findViewById(R.id.tabhost);
WebView home = (WebView) findViewById(R.id.webView1);
home.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);
findViewById(R.id.webView1).setVisibility(View.VISIBLE);
}
});
home.loadUrl("http://udaipurblog.com");
home.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
WebView static1 = (WebView) findViewById(R.id.webView2);
static1.loadUrl("file:///android_asset/1.html");
static1.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
WebView webview2 = (WebView) findViewById(R.id.webView3);
webview2.loadUrl("file:///android_asset/2.html");
webview2.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
th.setup();
TabSpec homespec = th.newTabSpec("home");
homespec.setContent(R.id.tab1);
homespec.setIndicator("Home");
th.addTab(homespec);
TabSpec static1spec = th.newTabSpec("static1");
homespec.setContent(R.id.tab2);
homespec.setIndicator("Static1");
th.addTab(homespec);
TabSpec static2spec = th.newTabSpec("static2");
homespec.setContent(R.id.tab2);
homespec.setIndicator("Static2");
th.addTab(homespec);
You have used homespec for every tab . Use respective specs for the tabs.
Sorry i can't comment due to my reputation .
You are adding always homespec tab
TabSpec homespec = th.newTabSpec("home");
homespec.setContent(R.id.tab1);
homespec.setIndicator("Home");
th.addTab(homespec);
TabSpec static1spec = th.newTabSpec("static1");
homespec.setContent(R.id.tab2);
homespec.setIndicator("Static1");
th.addTab(homespec);
TabSpec static2spec = th.newTabSpec("static2");
homespec.setContent(R.id.tab2);
homespec.setIndicator("Static2");
th.addTab(homespec);
I have not used the activityGroup in my project. Now I'm not is a position to implement the whole project using Activity group.
is it nesssory that I must implement the activityGroup class in my project to do so?
If yes then please give links for the basic tutorial of activityGroup implementation.
Here is my MainActvity.java which loads 4 other actvities in 4 tabs.
public class MainActivity extends TabActivity {
TabHost tabHost;
Context context = MainActivity.this;
Button btnGo;
TabSpec spec;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btn_GO);
tabHost = getTabHost();
// Android tab
Intent intentHome = new Intent();
intentHome.setClass(this, Home.class);
TabSpec tabSpecHome = tabHost
.newTabSpec("Home")
.setIndicator("Home",
getResources().getDrawable(R.drawable.home))
.setContent(intentHome);
tabHost.addTab(tabSpecHome);
Intent intentNowReading = new Intent().setClass(this, NowReading.class);
TabSpec tabSpecNowReading = tabHost
.newTabSpec("Now Reading")
.setIndicator("Now Reading",
getResources().getDrawable(R.drawable.now_reading))
.setContent(intentNowReading);
tabHost.addTab(tabSpecNowReading);
Intent intentFavourite = new Intent().setClass(this, Favorites.class);
TabSpec tabSpecFavourite = tabHost
.newTabSpec("Favourite")
.setIndicator("Favorites",
getResources().getDrawable(R.drawable.favorites))
.setContent(intentFavourite);
tabHost.addTab(tabSpecFavourite);
Intent intentProfile = new Intent().setClass(this, Profile.class);
TabSpec tabSpecProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile",
getResources().getDrawable(R.drawable.profile))
.setContent(intentProfile);
tabHost.addTab(tabSpecProfile);
tabHost.setCurrentTabByTag("Home");
...}
now I want to start the new activity in the Home tab area on the click event of Go button.(See the picture).
please note that I do not want to impelement the ActivityGroup class, How can I do that without this.
New Actvity must load in the HomeTab's area, not on the full screen.
ActvityGroup is a bad idea, this is old, deprecated API, don't use it.
You have to use Fragments API, just create a Fragment and add it to layout using FragmentTransaction, thats all what you need.
Please forgive me if this answer is obvious, I am very new to Android but making steady progress.
I have searched exhaustively but fear I may be using the wrong terminology.
I have an Activity that creates a WebView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webviewHome = (WebView) findViewById(R.id.webView1);
webviewHome.setWebViewClient(new WebViewClient1(){
});
webviewHome.getSettings().setJavaScriptEnabled(true);
webviewHome.loadUrl("http://192.168.1.3/codeholder.html");
The Activity also has a TabHost with 3 tabs:
//Tabs
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
//Tab for Sky
TabSpec skyspec = tabHost.newTabSpec("0");
skyspec.setIndicator("", getResources().getDrawable(R.drawable.sky));
Intent skyIntent = new Intent(this, sky.class);
skyspec.setContent(skyIntent);
//Tab for XBMC
TabSpec XBMCspec = tabHost.newTabSpec("1");
XBMCspec.setIndicator("", getResources().getDrawable(R.drawable.xbmc));
Intent XBMCIntent = new Intent(this, xbmc.class);
XBMCspec.setContent(XBMCIntent);
//Tab for PC
TabSpec PCspec = tabHost.newTabSpec("2");
PCspec.setIndicator("", getResources().getDrawable(R.drawable.windows));
Intent PCIntent = new Intent(this, pc.class);
PCspec.setContent(PCIntent);
tabHost.addTab(skyspec);
tabHost.addTab(XBMCspec);
tabHost.addTab(PCspec);
The WebView and the tabs work fine. What I can't work out is how to send (for example) webviewHome.loadUrl("javascript:button('right')") from one of the tabs to the WebView without having to reload the Activity.
Your expertise would be appreciated...
I have two tabs in my tabwidget and each tab display a list view.
ay
Initially my first tab is displayed as selected but below list correspond to second tab.
Once i click the tabs, i am getting correct display.
private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
.setIndicator(LIST_TAB_TAG1)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
.setIndicator(LIST_TAB_TAG2)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
LIST_TAB_TAG1 is highlighted when this sctivity is launched but the list displayed is listview2. This problem is only when activity starts. Upon clicking tabs its working fine
Can please help me in fixing this. Thanks for your time
I generally use the code given in docs which works fine for me ,try this one .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, UpcomingEvents.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this,PastEvents.class);
spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
getResources().getDrawable(R.drawable.bank_transaction1))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
I am currently in an Android project where our main view is a TabActivity and each tab is a separate Activity. One is a MapActivity and the other two are ordinary Activities.
First note that I think we must have each tab as separate activities, as there is too much code in the separate activities to just have the TabHost switch the content view on a tab change and have all of the code in the same class. Anyways, back to the problem.
One of the tabs include a button, which when pressed should make the TabActivity switch to the MapActivity and animate the map to a specific location.
The tutorial found on http://joshclemm.com/blog/?p=86 shows how to do it if the TabHost contains a mapview and a listview. If an item in the ListView is clicked, the TabHost switches to the mapview and animates to that location (those coordinates). This is exactly what i need to do when the button in the separate activity is pressed.
The MainView.java is created as follows:
public class MainView extends TabActivity implements OnTabChangeListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, MapGUI.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MissionView.class);
spec = tabHost.newTabSpec("mission").setIndicator("Mission",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SIPView.class);
spec = tabHost.newTabSpec("call").setIndicator("Call",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
The MissionView.java is as follows:
public class MissionView extends Activity implements Observer{
MissionController mc;
private TextView missionheader, missiondescription, missionaddress,
missiontime, missioninjuries;
private Button changedesc, gotoadress;
private String[] mission;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.missiontablayout);
missionheader = (TextView)findViewById(R.id.missionheader2);
missiondescription = (TextView)findViewById(R.id.missiondescription2);
missionaddress = (TextView)findViewById(R.id.missionaddress2);
missiontime = (TextView)findViewById(R.id.missiontime2);
missioninjuries = (TextView)findViewById(R.id.missioninjuries2);
changedesc = (Button)findViewById(R.id.gotoaddress);
changedesc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// DO SOMETHING HERE?
}
});
mc = new MissionController(MissionView.this);
}
public void update(Observable observable, Object data) {
if(data instanceof String[]){
mission = (String[]) data;
updateView(mission);
}
}
public void updateView(String[] missiontext){
missionheader.setText(missiontext[0]);
missiondescription.setText(missiontext[1]);
missionaddress.setText(missiontext[2]);
missiontime.setText(missiontext[3]);
missioninjuries.setText(missiontext[4]);
}
}
Anyone know how i could achieve this?
Note that the code supplied above has no implementation to actually draw to the actual location, but the question still remains, how do I make a button pressed in one activity to switch tab in the TabHost and fire a change on that tab activity?
changing tabs in a TabHostcan easily be done with setCurrentTab(int)
http://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)
Sending events to other Activities can simply be achieved by sending a broadcast intent and receiving/handling it in the other Activity.
Alternatively you could save static references to all your tab Activities (ugly...) and call methods directly.
Place the below line on button click where you want to switch to the Map activity
((MainView) getParent()).setTabMap();
and in MainView create the following function
public void setTabMap()
{
//as Map activity is your first tab so pass 0 as index
getTabHost().setCurrentTab(0);
}