How can I update a WebView from within a TabHost? - android

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...

Related

tabs in tabhost don't show proper content

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);

How to Start New activity in Tab without using ActvityGroup

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.

Switch Taps from Activitys by buttons, etc

I have following TapControl in the StartActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for A Tap
TabSpec atapspec = tabHost.newTabSpec("ATap");
// setting Title and Icon for the Tab
atapespec.setIndicator("ATap", getResources().getDrawable(R.drawable.state_atap));
Intent atapIntent = new Intent(this, ATapActivity.class);
atapspec.setContent(atapIntent);
// Tab for B Tap
TabSpec btapspec = tabHost.newTabSpec("BTap");
btapspec.setIndicator("BTap", getResources().getDrawable(R.drawable.state_btap));
Intent btapIntent = new Intent(this, BtapActivity.class);
btapspec.setContent(btapIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(atapspec); // Adding a tab
tabHost.addTab(btapspec); // Adding b tab
}
Each Tap has its own Activity.
Now my problem, how can I switch with a button to the next Tap-Activity?
I tried to start just the Activity but then the Tap-control was missing.
startActivity(new Intent(this, BTapActivity.class));
I found that it should be sometion like that:
setCurrentTabByTag("BTab");
but i don't know how.
EDIT:
When I put "tabHost.setCurrentTabByTag("BTap");" on the end of the onCreate Method, BTap is selected.Therefore it is the correct command. But from another Activity I'm not able to access the tabHost.
When I make the the tabHost to a global Object, The App crashes:
TabHost tabHost = getTabHost();
I tried it like this from the Tab-Activity:
startActivity StartAct = new startActivity ();
StartAct.setTap("BTap");
This Method is in the StartActivity:
public void setTap(String tap) {
tabHost.setCurrentTabByTag(tap);
}
What can I do? Sorry, but I'm a beginner ...
You can use TabHost's setCurrentTab(int index) function to open tabs programmatically.

How to display tabbar in whole app in android?

Hi I am creating an application. In this I need to display a TabBar in all Activities. I set the TabBar bottom using 4 tabs; home, contact, about and call us.
Inside home tab I have some buttons. When I click inside home any button that time I need to move some other Activity. Using intent I moved to another Activity but here the TabBar was not displayed. However, I need to display the same TabBar in all Activities. If any one knows how to do this, please suggest a solution to me.
DefenceLaywer.java:
public class DefenceLaywer extends TabActivity {
TabHost tabhost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabhost.newTabSpec("Home");
TabSpec secondTabSpec = tabhost.newTabSpec("Claimonline");
TabSpec thirdTabSpec = tabhost.newTabSpec("CallUs");
TabSpec fourthTabSpec = tabhost.newTabSpec("AboutUs");
// TabSpec thirdTabSpec = tabhost.newTabSpec("Interactive");
firstTabSpec.setIndicator("Home", getResources().getDrawable(R.drawable.home));
secondTabSpec.setIndicator("ContactUs", getResources().getDrawable(R.drawable.contactus));
thirdTabSpec.setIndicator("CallUs", getResources().getDrawable(R.drawable.callus));
fourthTabSpec.setIndicator("AboutUs", getResources().getDrawable(R.drawable.aboutus));
firstTabSpec.setContent(new Intent(this,HomeTab.class));
secondTabSpec.setContent(new Intent(this,ContactUs.class));
thirdTabSpec.setContent(new Intent(this,CallUs.class));
fourthTabSpec.setContent(new Intent(this,AboutUs.class));
tabhost.addTab(firstTabSpec);
tabhost.addTab(secondTabSpec);
tabhost.addTab(thirdTabSpec);
tabhost.addTab(fourthTabSpec);
}
}
use fragments in activity and set that activity on tabspecs.
To learn FrAgment use following lin:
http://developer.android.com/guide/topics/fundamentals/fragments.html
and for how to use fragments to implement tab, follow followig tutorial:
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

Trying to open WebView in TabHost

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

Categories

Resources