thanks advance..
when i create custom tab in android application so no one tab display in main screen
how to solve this problem please any one tell me?
you mean that you want to create custom tab for your application.ok now you have two class that is homemyclass.java and myclass.java. At first you declare flowing two line in homemyclass.java
private Intent mytab;
private static final String TAB_MYCLASS = "myclass";
then you write flowing line into onCreate() function of homemyclass.java
mytab = new Intent(this, myclass.class);
addTab(TAB_MYCLASS, getString(R.string."label for tab"), R.drawable."icon for tab display when
tab is selected", R.drawable."icon for tab display when tab is not selected", mytab);
just add addTab() into homemyclass.java
private void addTab(String tag, String label, int icon, int ficon, Intent content) {
TabHost tabHost = getTabHost();
TabSpec tabspecDialer = tabHost.newTabSpec(tag).setContent(content);
//boolean fails = true;
tabspecDialer.setIndicator(label, getResources().getDrawable(icon));
tabHost.addTab(tabspecDialer);
}
you also need to modify androidManifest.xml by homemyclass.java and myclass.java
Related
I use a TabHost.
The below code to call AActivity.
intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
And it is in the tab.
But in AActivity I call BActivity.
The BActivity will open new page, but not in the tab.
How to let it on the tab frame?
AActivity use below code to call BActivity:
it = new Intent(this, BActivity.class);
startActivity(it);
If you want to open multiple activity in Tab then on Place of activity use Activity group for par tab and switch view in this activity group for open multiple Activity in single tab
you can take some help from this tutorial
You need to change the current selected tab. There's a method called setCurrentTabByTag(String tag) in the TabHost class that will do that, just pass the tag name of your tab (which in your case is B), or you can use the setCurrentTab(int index) and pass the tab index.
Example. Usually I have a MainActivity class, which is my TabActivity. Inside of this class, I will create all tabs that I need on the onCreate method.
First I set some static int with the tabs indexes.
// Tab index.
public static int FIRST_TAB = 0;
public static int SECOND_TAB = 1;
Later, I create my tabs in the onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setting the content view.
setContentView(R.layout.main);
// Getting the TabHost object.
mTabHost = getTabHost();
// Declaring the Intent object for the tabs.
Intent intent;
// Creating the First tab.
intent = new Intent().setClass(this, MyFirstActivity.class);
addTab(mTabHost, "First", FIRST_TAB, intent)
// Creating the Second tab.
intent = new Intent().setClass(this, MySecondActivity.class);
addTab(mTabHost, "Second", SECOND_TAB, intent);
// Setting the current tab.
switchTab(FIRST_TAB);
}
public void addTab(TabHost host, String title, String tag, Intent intent) {
TabHost.TabSpec spec = host.newTabSpec(tag);
spec.setContent(intent);
spec.setIndicator(title);
host.addTab(spec);
}
And last the method that will change the current tab.
public void switchTab(int index) {
mTabHost.setCurrentTab(index);
}
Later, inside of the MyFirstActivity you can call the MainActivity swichTab method and pass the index of the tab to change it.
You can retrieve the MainActivity calling the getParent() method of the Activity class.
MainActivity parent = (MainActivity)getParent();
In the tab activity class where the tabhost is created, implement the following method.
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
In AActivity/BActivity implement the following method and call it on any event(that you need):
public void switchTabInActivity(long indexTabToSwitchTo){
TabActivity tabActivity;
tabActivity = (TabActivity) this.getParent();
tabActivity.switchTab(indexTabToSwitchTo);
}
Here TabActivity is the class where tabhost is created
I made custom tabs using this tutorial: http://joshclemm.com/blog/?p=136
I have them completely customized and looking nice, but now I don't know how to add content to the tabs. I don't even know where to start with the way this code is written, any help? Thanks.
This is the code that I think sets the content:
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
I don't know what this is doing with "TabContentFactory"
I too followed the same blog and was facing problem, while adding the Intent's. So, finally I fixed it myself. What I did is,
1.) public class CustomTabActivity extends Activity here I changed it to extend TabActivity
2.) Just add the content using Intent
Intent intent = new Intent().setClass(this, Hello.class);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
That's it and it worked. Hope this works for you too.
Try following the tutorial given on the android website. Combined with the tutorial you used, it should give you completely what you want.
Specifically, to add content to to each tab you add and activity to each tab like so:
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
Look at this:
private void addActivityTab(int labelResId, int iconResId, Intent intent) {
String tabLabel = getString(labelResId);
View indicator = View.inflate(this, R.layout.simple_tab_spec, null);
ImageView icon = (ImageView) indicator.findViewById(R.id.simple_tab_spec_icon);
icon.setImageResource(iconResId);
TabSpec tabSpec = tabHost.newTabSpec(tabLabel).setIndicator(indicator).setContent(intent);
tabHost.addTab(tabSpec); }
The first argument is the title of your tab item, the second argument is the background image of your tab item.
You should create an intent object to set the parameters and the target activity.
Here is the scenario:
I have an activity with 4 tabs, each tab with a different intent, each intent with a different activity.
Works perfectly.
What I need is, to somehow change the intent on one of the tabs.
It would be as simple as adding extra parameters to the Intent that is used on the TabSpec.setContent(intent), but I have not found a way to retrieve that intent to change it.
The specific action I'm trying to do is: from another activity (that is inside the content of another tab) I call it's parent (the TabActivity) to open a different tab AND add some custom data to that tab's activity.
I can change the tab without a problem, but I haven't found a way to pass the extra parameters from one activity to the other, since the original intent used to create the TabSpec had no extra parameters.
Am I approaching this in the wrong way?
Is there a way to replace the TabSpec content's intent?
Thanks !
If you are just trying to find a way to pass values between activities you can override the Application class.
Way to use app context.
Extend the application class and add your values as its attributes. In any activity, if you call the below code, it will return a singleton.
MyApplication appContext = (MyApplication) getApplicationContext();
To make this work you need to add this to the application tag of the manifest file
android:name=".MyApplication"
This method is used to pass values around the app when sending through intent is not possible.
To remove the tab, you will need to use the clearAllTabs() method and add the tabs again. The above code should be better.
In my case I'm storing a reference to an intent in the TabActivity
mGalleryTabIntent = new Intent(this, AnActivity.class);
spec = getTabHost().newTabSpec(TAB_GALLERY).setIndicator(res.getString(R.string.footer_gallery),res.getDrawable(R.drawable.gallery_icon_sel)).setContent(mGalleryTabIntent);
public Intent getStoredTabIntent(){
return mGalleryTabIntent;
}
Then, when from a child I want to navigate to another tab passing an Extra along with the Intent
MainTabActivity parent = (MainTabActivity)getParent();
parent.getStoredTabIntent().putExtra(AnActivity.START_VIEW, AnActivity.PAGE_TWO);
//Navigate to the tab
parent.getTabHost().setCurrentTabByTag(AnActivity.TAB_GALLERY);
Then, in AnActivity's onCreate
Bundle extras = getIntent().getExtras();
if(extras != null && extras.containsKey(START_VIEW)){
switch (extras.getInt(START_VIEW)) {
case PAGE_TWO:
doSomething();
break;
default:
break;
}
//Erase the Extra so that navigating to this Tab always displays the standard view unless specified otherwise
MainTabActivity parent = (MainTabActivity)getParent();
parent.getStoredTabIntent().putExtra(AnActivity.START_VIEW, "");
}else{
doStandardStuff();
}
The Application solution is also good, but I don't think I'll be needing it for anything else, thus I'd rather stick with Activities.
TabSpec tabspec1;
private void createTabHost() {
// initialize tabHost
tabHost = (TabHost) this.findViewById(R.id.tabhost1);
tabHost.setup(this.getLocalActivityManager());
// create tab1
Intent intent = new Intent(this, AnotherActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("myData",
ObjData);
tabspec1 = tabHost.newTabSpec("tab1")
.setIndicator(Utilities
.prepareTabView(this, "Title"))
.setContent(intent);
tabHost.addTab(tabspec1 );
// create tab2
...
}
method that changes the content of that activity placed in your tab:
private void reloadTabSpec1() {
Intent i = new Intent(this, AnotherActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("myData", ObjData);
tabspec1.setContent(i);
// needed for refresh :(
tabHost.setCurrentTabByTag(tabspec2.getTag());
tabHost.setCurrentTabByTag(tabspec1.getTag());
}
i need to get the Activitys initialize , before anyone click on them . That is cause i have a video in one of them , and i need to have that Activity created to play the video from another tab (or activitys) .
I have this code to initialize the tab content but not the activitys into them :
//Init Tabs
Resources res = this.getResources();
TabHost tabHost = getTabHost();
tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(tabHost,MyInitActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_init));
setupTab(tabHost,MusicGroupActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_playList));
setupTab(tabHost,SearchActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_search));
setupTab(tabHost,VideoActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_video));
I have seen code like this to try it :
tabHost.setCurrentTab(number);
but that seems to not run when you change immediately cause when i do
Context context = getTabHost().getChildAt(3).getContext();
it throws to me a null exception.
Anyone knows how to do this?
ADDED
private void setupTab( TabHost mTabHost,Class<? extends Activity> activityclass,Drawable image, String tag)
{
View tabview = createTabView(mTabHost.getContext(),image,tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new Intent(this, activityclass));
mTabHost.addTab(setContent);
}
Thanks for all
I do the following in the onCreate() (say i have 4 tabs)
tabHost.setCurrentTab(0);
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(2);
tabHost.setCurrentTab(3);
tabHost.setCurrentTab(Tab_I_Want);
Can someone please tell me how to change tab by clicking on element INSIDE the tab? I already tried it with global data. The code looks like this:
public class Tabs extends TabActivity {
int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
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, Tribocracy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Areas.class);
spec = tabHost.newTabSpec("areas").setIndicator("Areas",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("settings").setIndicator("Settings",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(tabNumber);
}
protected void onResume() {
super.onResume();
GlobalData globalData = ((GlobalData)getApplicationContext());
returnedTabNumber = globalData.getTabNumber();
tabHost.setCurrentTab(returnedTabNumber);
}
}
The global adapter looks like this:
public class GlobalData extends Application {
//----------------------------------------------------
private int Point1; //define the vars here
private int Point2; //define the vars here
private int Point3; //define the vars here
private int Point4; //define the vars here
private int Point5; //define the vars here
private int Point6; //define the vars here
private int tabNumber;
public int getTabNumber() //getter of the value
{
return tabNumber;
}
public int setTabNumber(int number) //setter of the value
{
tabNumber = number;
return tabNumber;
}
}
Now when I'm trying to change tab in my ListActivity tab by clicking on one of the items it doesn't do anything and stays on the ListActivity tab. Perhaps I shouldn't use onResume() here. Basically I want to go to first tab when I click on one of the items in the list. Please help!
One way to do this is to use Intents. And there are many ways to use Intents for this. This works especially well for some cases, for example where the activity is displaying some content from a content provider. I'll describe one possible method below:
For the Activity that hosts the tabs, give it an intent filter if it doesn't already have one. For example, if it's an 'item detail'-style Activity, maybe its intent filter can match for ACTION_VIEW on a certain content type like vnd.android.cursor.item/vnd.somecontent.
Have the Activity take an extra such as focus_tab, and in the Activity's onCreate, after setting up the tabs, set the active tab to the one specified by this focus_tab extra.
Override onNewIntent, and in that method, look for that same focus_tab extra and set the active to the one specified by that extra.
Now, in your button code (i.e. a button inside one of your tab content activities), create an Intent that will start your host Activity, and give it a focus_tab extra corresponding to the tab you want to switch to. Add the FLAG_ACTIVITY_SINGLE_TOP Intent flag so you don't create a new instance of the host Activity. And then, call startActivity on that Intent you just created.
Note: I haven't tried this myself, but it should work in theory.