How to access to a class of a tab? - android

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

Related

android: Actionbar for tabactivity

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

Setting tabActivity title in android

I have a TabActivity in which there are two tabs , tabs may increase in future.So i need to change the title of MyTabActivity from anyone of them according to situation so that it is visible from all the tabs.
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabContext = this;
setTitle("My App Name");
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec(TAB_1).setIndicator(TAB_1, getResources().getDrawable(R.drawable.tab1)).setContent(new Intent(this, first.class)));
tabHost.addTab(tabHost.newTabSpec(TAB_2).setIndicator(TAB_2, getResources().getDrawable(R.drawable.tab2)).setContent(new Intent(this, second.class)));
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
setTabTitle();
}
});
tabHost.setCurrentTabByTag(TAB_1);
}
}
i have written setTabTitle() function to set my tabActivity title.
public void setTabTitle()
{
PrefManager prefManager = new PrefManager(getApplicationContext());
String tabtitle = prefManager.getTitle();
tabContext.setTitle("My App Name" + " " + tabtitle);
}
i am trying to change the title of MyTabActivity from the TAB_1(ListActivity) or TAB_2(Activity) when i get some data in these two activities .But when i change the tab then only the title gets changed because i have called it in (setOnTabChangedListener).But i want to change it as soon as i get some data in any one of my activity and calling this setTabTitle function.
i have tried two things as of now.Making setTabTitle() as static and calling it from two activities as MyTabActivity.setTabTitle("My Title") but its not working.
public static void setTabTitle(String title)
{
tabContext.setTitle("My App Name" + " " + title);
}
Other is as suggested by a member of this forum
MyTabActivity myTabs = (MyTabActivity) this.getParent();
myTabs.setTitle("This is First cool Activity");
But both of these is not working .Do i need to do some kind of REFRESH in activity ?.
Help me how to do this.
Thanks

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

Multiple layout in one Tabactivity

How could I few layout and require to create in one Tabactivity.
I have try the code below but no luck got error.
tabHost.addTab(tabHost.newTabSpec("Sales Order").setIndicator("Sales Order").setContent(R.layout.frm_txn_so_item_list));
OK let me explain clearly.
I have a code below, as you can see I have 4 tab layout page. Each of it has it own activity class. I have a button which is belong to cls_so_item_list.class, whenever I am try to call it in my cls_so it always return me null value.
So I have come out an idea, to remove all the tab page(item,product,summary,Report) activity classes then create it one standalone class which is cls_so.
My question is how do I put the layout page inside the tabHost.addTab? Thanks
public class cls_so extends TabActivity implements OnClickListener {
protected TabHost tabHost;
int intSalesOrderId;
src_txn_so.cls_so_obj objSalesOrder;
static final String LIST_ID = "list_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
newTabIntent("Item", null, cls_so_item_list.class);
newTabIntent("Product",
getResources().getDrawable(R.drawable.so_product),
cls_so_prd_list.class);
newTabIntent("Summary",
getResources().getDrawable(R.drawable.so_summary),
cls_so_summary.class);
newTabIntent("Report",
getResources().getDrawable(R.drawable.so_report),
cls_so_summary.class);
Button btnSOLineDiscount = (Button) findViewById(R.id.txn_so_btn_line_discount);
btnSOLineDiscount.setOnClickListener(this);
tabHost.setCurrentTab(0);
}
protected void newTabIntent(String label, Drawable icon, Class<?> pageClass) {
TabSpec tabSpec = tabHost.newTabSpec(label);
tabSpec.setIndicator(label, icon);
Intent SOIntent = new Intent().setClass(this,pageClass);
SOIntent.putExtra(LIST_ID, -1);
tabSpec.setContent(new Intent(this, pageClass));
tabHost.addTab(tabSpec);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
It doesn't work like that. In your main layout, add in the FrameLayout with the id of tabcontent an element <include> that points on that layout.
In your code you'll have to change it to.
tabHost.addTab(tabHost.newTabSpec("Sales Order").setIndicator("Sales Order").setContent(R.id.my_included_layout));
if you're <include> has the id "#+id/my_included_layout"

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