android: Actionbar for tabactivity - android

I have a tabactivity with two tabs. I have added an action bar to this main activity ( tabactivity ). The search should happen in the corresponding child activity. How this can be implemented. Whichever is the current tab, search should be on that current activity

TabActivity is deprecated. Use Fragmentactivity instead of TabActivity.
Get the current tab , based on that implement search
public class MainActivity extends TabActivity {
static TabHost mytabs;
mytabs = getTabHost();
mytabs.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
}
});

Related

How to access to a class of a tab?

I use a TabActivity, and I have a menu (on the title bar).
I want that when I click on a menu button, I instantiate this class in one of my tabs.
So, my tab :
public class Tab1 extends ListActivity {
...
class Chargement extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... args) {
//traitement
}
}
}
In my TabActivity, I have tried to do that :
Tab1 t = new Tab1();
a.new Chargement().execute();
But it doesn't work.
Thank's for you help.
Tab1 t = new Tab1(); // wrong
Don't instantiate Activities, they can not be instantiated., don't create Object for Activity. Android will take care of that. If you create such type your life cycle method won't execute.
Edit:
To get the current tab use this
tabhost= getTabHost(); // get the tabhost
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
}
});

How to change tabs in Android?

I have three tabs that each has its own Activity. The tabs are as follows:
Home [HomeActivity]
Search [SearchActivity]
Account [AccountActivity]
I have a Main Activity which handles the main TabHost object and this is its content:
public TabHost tabHost;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Home").setContent(new Intent(this, HomeActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Search").setContent(new Intent(this, SearchActivity.class).putExtra("callX", true)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Account").setContent(new Intent(this, AccountActivity.class)));
tabHost.setCurrentTab(0);
}
Now I have a button in Search tab which I need when it is clicked, no matter what, the Home tab should activate. I guess I should somehow call the setCurrentTab() method on tabHost object but I don't how to access it inside the SearchActivity class?
I probably should use Intent for that which I have no idea how to use.
set a method to my main class, which extends TabActivity let's call it "MainActivity"
public TabHost getMyTabHost() {
return tabHost;
}
Then add my tab activity class;
MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);
or follow a better aproach at this

TabActivity listener with onCreate()

I have a project with two tabs. Tabs are created in the main class. Here I added the tablistener too, to handle the changes between the tabs. Here is one tab's instant:
TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab1.class);
spec = tabHost.newTabSpec("tab1").setIndicator("",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(intent);
tabHost.addTab(spec);
The listener method:
public void onTabChanged(String tabName) {
if (tabName.equals("tab1")){
tab1.load();
}
}
And similar for the second tab too. My question is, if the onCreate() methods run only once in the Tab1 and Tab2 classes, how can I "force" the main class to show the corresponding Activity after the tab changes? I receive NullPointerException
The tabs' classes are something like this:
public class Tab1 extends Activity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.tab1);
load();
}
public void load(){
//....
}
}
onCreate() method calls only once when your Activity first time loaded.
If you want to perform any functionality on each time you view your Activity, put that functionality in onResume() method.

Android TabHost with only selected tab on stack

I have a TabHost with 4 tabs. I need only the selected tab activity to be available on the stack. When user changes the tab, how to finish the activity under previous tab. I tried the following code. Here showing code for first tab. It similar for remaining tabs:
spec = tabHost.newTabSpec("tab1").setIndicator("Tab1",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(new Intent(this, Tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP));
But the above code is deleting the Tab1 Activity on stack/heap only when the user comes again to that tab but not when user changes to new tab.
I've had a look at this, what is your reason for this?
How do you know what's on the stack? Are you depending on onDestroy() or something?
I haven't got a full answer but you can see which tab is active:
Let your class implement OnTabChangeListener
public class YourClass extends TabActivity implements OnTabChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Load all your normal objects as well as TabHost
// make your tabhost listen for tab changes
mTabHost.setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tabId) {
int currentTabNumber = mTabHost.getCurrentTab();
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
if(i != currentTabNumber){
System.out.println("I'm not a currently active tab");
}
}
}
}

OnClickListener on Tabs not working

Greetings,
I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.
Am i registering for the wrong View?
Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?
public class TestDroidViewTab extends TabActivity
implements TabContentFactory
, OnTabChangeListener, OnClickListener {
private static final String LOG_KEY = "TEST";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
ts.setIndicator("1");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_2");
ts.setIndicator("2");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_3");
ts.setIndicator("3");
ts.setContent(this);
tabHost.addTab(ts);
tabHost.setOnClickListener(this);
tabHost.setOnTabChangedListener(this);
}
public void onClick(View v) {
Log.d(LOG_KEY, "OnClick");
}
public void onTabChanged(String tabId) {
Log.d(LOG_KEY, "OnTabChanged");
}
If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.
The hierarchy of views in a tab implementation is:
TabHost
TabWidget
(tab)
(tab)
FrameLayout
The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));
You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);
In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.
Here is a full example, which lets you intercept the click event, and change the content below the tab:
final String myTabTag = "My Tab";
final int myTabIndex = 3;
getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );
getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
getTabHost().setCurrentTab(myTabIndex );
}
}
});
use setOnTabChangedListener instead of OnClickListener ;)
static TabHost tabHost;
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
}
});
Your clause is wrong, use:
...
if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
getTabHost().setCurrentTab(myTabIndex );
}
...
into my code, it shows some errors and ask me to create new methods in
those names like getTabWidget(), getTabHost(), etc. Waiting for your
response.
Try this
tabHost.getTabHost().setCurrentTab(myTabIndex);

Categories

Resources