I set the TabHost through the code without using the Layout XML. How do I control Tab appearing at the bottom instead on top. normally its controlled through XML by android:layout_alignParentBottom="true".
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.main);
TabHost host=getTabHost();
host.addTab(host.newTabSpec("Offers")
.setIndicator("Offers", getResources().getDrawable(R.drawable.icon_light))
.setContent(new Intent(this, Offer_Popup.class)));
host.addTab(host.newTabSpec("Account")
.setIndicator("Account", getResources().getDrawable(R.drawable.icon_wrench))
.setContent(new Intent(this, Offer_Popup.class)));
host.addTab(host.newTabSpec("Settings")
.setIndicator("Settings", getResources().getDrawable(R.drawable.icon_user))
.setContent(new Intent(this, Offer_Popup.class)));
host.addTab(host.newTabSpec("Awards")
.setIndicator("Awards", getResources().getDrawable(R.drawable.icon_list))
.setContent(new Intent(this, List_Items.class)));
}
}
I guess you are using relative layout (since you are using android:layout_alignParentBottom="true").
Then I belive you need to create RelativeLayout layout parameters and apply them to your TabWidget, something like this:
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(..., ...);
rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
tw.setLayoutParams(rllp); //where tw is your TabWidget
It's programmatical equivalent of using android:layout_alignParentBottom="true"
Related
I want to add background images for Tabhost which i already added in selector xml. But not sure how to add resource while initiating Tab. Below is tab :
Code to add :
getResources().getDrawable(R.drawable.tabicon)
/* Tabs */
Bundle bundle = getIntent().getExtras();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// First Activity
intent = new Intent().setClass(this, InfoListView.class);
spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
tabHost.addTab(spec);
// Second Activity
intent = new Intent().setClass(this, LogListView.class);
spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;
/* Tabs ends */
Create a TextView , set text to that textView, set background drawable to it, and set this textview as indicator
Somewhat like this:
TextView mTv = new TextView(mContext);
mTv.setText(/*Your-text*/);
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/));
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);
tabHost.addTab(spec);
After your tabhost is intilize with indicator and other things add background to it like this
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab
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 wrote a Tabhost below:
public class AppTabhost extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**find tab host**/
TabHost tabHost = getTabHost();
/**add tabs**/
//tab 1
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(new Intent(AppTabhost.this, anotherActivity.class)));
//tab 2
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(new Intent(AppTabhost.this, theotherActivity.class)));
}
I want to put it on the bottom of every Activity in my App
How can I do?
You could slap an Intent in there like
Intent I= new Intent(this, Apptabhost.class);
startIntent(I);
But if you want the tabhost to be physically at the bottom of the screen, I would suggest you define a class that will extend tabhost and not tabhost activity. Since a view hierarchy contains views and not activities.
If you want to put it in bottom the you can write this code
android:layout_alignParentBottom="true"
on your tabHost activity xml file
Following is the example of tabs with intent data.
While debugging i found that always when first tab we add in tab host in our case following tab
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("list")
.setContent(new Intent(this, List1.class)));
oncreate method of "List1" intent get called regardless it is our current tab or not even if if i define tab2 as a current tab how to fix this ?
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("photo list")
.setContent(new Intent(this, List8.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
// This tab sets the intent flag so that it is recreated each time
// the tab is clicked.
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("destroy")
.setContent(new Intent(this, Controls2.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
}
setDefaultTab(1);
seems not to be working in TabActivity when separate Activities are used as Tab Content.
Use following instead of this method,
tabHost.setCurrentTab(1);
This will set "photo list" (i.e second tab) as the selected or default tab...
I have found this same behavior as well, and I do not have a specific fix. But I do know of a work-around.
Instead of attaching Activities to each tab, attach a View to each tab. You can then handle the data passing very easily as each view will be in the same Activity. This also eliminates the need to pass information using Intents. Furthermore, you can create (or inflate) your Views as you need them and with more control.
Good luck,
-scott