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);
Related
I have a tabActivity where I am adding tabs on runtime. So I think this is the usual code to do that:
_tabSpec = TabHost.newTabSpec("More");
_tabSpec.setIndicator("", Resources.GetDrawable(Resources.Drawable.myIcon).SetContent(intent);
TabHost.AddTab(_tabSpec);
Now the thing is, I have defined an options menu and I want to pop that up when the user clicks on the 'More' tab. I don't know how to do that. I tried not setting a content on that tab and simply use the OpenOptionsMenu() to pop it, but it doesn't seem to work.
Any clue how to achieve that?
P.S.: This is a C# code written in Xamarin. It might not look like the native java-android code, but its almost the same.
Alright, here's an answer to what I was trying to do.
Motive: Add a 'More' tab to the existing TabActivity. When user clicks it, open some kind of a PopUpWindow or ContextMenu, etc.
Steps to do that:
1) Create a tabSpec and add that tab to the TabHost as shown in the question.
2) Now you need to take in this last added tab as a View type variable. Do this by..
View v = TabWidget.GetChildAt(index)
Remember index of tabs starts from 0
3) Now on the onCreate() method of your main activity (one that holds the TabActivity) add an onTouchListener() (I use C#, so I added v.Click+=myFunction()) and write your code of the PopupWindow or ContextMenu or whatever you want to do there.
private String lastTab = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tab1");
tabSpec.setIndicator("Tab 1");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tab2");
tabSpec.setIndicator("Tab 2");
tabSpec.setContent(new Intent(this, TwoActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("more");
tabSpec.setIndicator("More");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equalsIgnoreCase("more")){
openOptionsMenu();
tabHost.setCurrentTabByTag(lastTab);
}
else lastTab = tabId;
}
});
}
I am using a Tab host two show two list on two tabs but I want that when click on second tab then it should reload the first tab data.How is it possible?
My code is like this
TabHost tabs = (TabHost)findViewById(R.id.tabhost_spices);
tabs.setup();`enter code here`
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.list_spices_fav);
tab1.setIndicator("Favorite",getContext().getResources().getDrawable(R.drawable.tab_fav_icon));
tabs.addTab(tab1);
// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.list_spices_categories);
tab2.setIndicator("View List",getContext().getResources().getDrawable(R.drawable.tab_cat_icon));
tabs.addTab(tab2);
Here is a tutorial,This Help me- link
Extends your activity with TabActivity
TabHost tabHost;
tabHost = getTabHost();
Resources res = getResources();
TabSpec tabspec1 = tabHost.newTabSpec("").setIndicator("Tab1",res.getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, FirstActivity.class));
tabHost.addTab(tabspec1);
TabSpec tabspec2 = tabHost.newTabSpec("").setIndicator("Tab2",res.getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, SecondActivity.class));
tabHost.addTab(tabspec2);
For refreshing the data of new Tab you have to keep the code for refereshing the data inside onResume() of that Activity because when TabActivity is created its all the tabs make a call to the respective Activities calling its onCreate()'. So, when you click the Tab once its loaded itsonResume()method is called and notonCreate()`.
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...
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/
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