Setting tabActivity title in android - 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

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

Android ActivityGroup's child activity setTitle not working

I have TabActivityGroup:
MainActivity class contain some tab, that name loading from db. Sales, Admin, Inquiry like wise I have tab name
For Sales I created SalesActivityGroup.That class is :
public class SalesActivityGroup extends ActivityGroup {
public static SalesActivityGroup group;
private ArrayList<View> history;
private LocalActivityManager mActivityManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
mActivityManager = getLocalActivityManager();
Intent i = new Intent(getBaseContext(), SalesRouteActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("positions", -1);
i.putExtras(bundle);
View view = mActivityManager.startActivity("Sales",i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP )).getDecorView();
replaceView(view);
}
public void replaceView(View v) {
history.add(v);
setContentView(v);
}
public void back(){
if ( history.size() > 1 ){
history.remove(history.size() - 1);
View v = history.get(history.size() - 1);
setContentView(v);
}
else {
this.finish();
}
}
#Override
public void onBackPressed() {
SalesActivityGroup.group.back();
}
}
SalesRouteActivity is first Activity .In there i want to set up the title name.I did using this ways.But not working
public class SalesRouteActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.sales_routes);
//getWindow().setTitle("Route");
View viewToLoad = LayoutInflater.from(SalesActivityGroup.group).inflate(R.layout.sales_routes, null);
this.setContentView(viewToLoad);
//this.setTitle("Route");
//getWindow().setTitle("Route");
SalesActivityGroup.group.setTitle("Route");
}
}
Please advice me How can i set the Title name.
Thanks in advance
You can access the parent tab activity like
getParent().getParent().setTitle("New Tilte");
EXPLANATION:
Based on my understanding,
When you call the getParent the first time, you get the activity group that started the child activity.
When you call getParent the second time, you will get the tab activity that started the activity group.
setTitle should work for the activity window which is held by the tabactivity. The sub activities are rendered in the framelayout of the tab activity. So in the child activities access the parent tab activity to set the title.
The best way to do this is to implement the method
protected void onChildTitleChanged(Activity childActivity,CharSequence title) {
super.onChildTitleChanged(childActivity, title);
setTitle(title);
}
Implement this method in the parent activity. For example, In my case I have three activities.
Home Activity
Artists Activity
Album Activity
My Home activity contains a TabHost with Artists activity and Album activity.
I implemented the above method in the Home Activity. The title for Artists activity and Album activity is set in the OnResume methods of those respective activities.
It is not advisable to use ActivityGroup, it has been deprecated.
Refer to this link
Please use Fragment and FragmentManager using Compatibility Library

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"

How to use the same Activity for 3 different tabs in TabActivity with different inputs

I have an TabActivity which has 3 Tabs reusing the same Activity for all the 3 tabs.
I am differentiating by means of extras(intent.putextras()) which i shall display the contents of each tab
Now the problem is when we switch the tab from each other the extra values which i passed through Intent is coming wrong for the second tab.
As i understood when we created the FirstTab, the only Activity is created and so the contents are drawn as per it.
when we add the secondTab the intent storing the extra value
and again when we creating the third tab the Intent over-writing the extra value.
so when ever we are switching to 2nd tab, i'm receiving the 3rd Tab extra value and hence showing wrong contents to the user.
here is the code,
public class ContentsTab extends TabActivity implements OnTabChangeListener{
public static final String _ID = "_id";
public static final int ID_1 = 1;
public static final int ID_2 = 2;
public static final int ID_3 = 4;
private TabHost mTabHost;
private static Intent newIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
newIntent = new Intent(this, ShowContents.class);
mTabHost = getTabHost();
newIntent.putExtra(_ID, ID_1);
mTabHost.addTab(mTabHost.newTabSpec(INT_EXT_MEM)
.setContent(newIntent)
.setIndicator("one"));
newIntent.putExtra(_ID, ID_2);
mTabHost.addTab(mTabHost.newTabSpec(EXT_MEM)
.setIndicator("two")
.setContent(newIntent));
newIntent.putExtra(_ID, ID_3);
mTabHost.addTab(mTabHost.newTabSpec(INT_MEM)
.setContent(newIntent)
.setIndicator("three"));
mTabHost.setCurrentTabByTag("one");
mTabHost.setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tabId) {
//This is only coming once the Tab is changed
Log.d(TAG, "onTabChanged arg0 = "+tabId);
}
}
showcontents.class
public class ShowContents extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
int mId_ContentType;
mId_ContentType = extras != null? extras.getInt(_ID): -1;
updateData(mId_ContentType);
}
}
Here the illustration,
when user presses tab1, ShowContents.class should receive 1 as extras and same with tab2 and tab3, but the following values are received
Tab1 - 1
Tab2 - 4(correct value should be 2)
Tab3 - 4
Tab1 and Tab3 values are coming correct.
Is there any alternative to send the correct extra parameters to Tab2? using the same class Activity?
Please help me
Thanks for your time :)
You need to have 3 different instances of the activity. You can do something like this
newIntent1 = new Intent(this, ShowContents.class);
newIntent2 = new Intent(this, ShowContents.class);
newIntent3 = new Intent(this, ShowContents.class);
Then use these 3 instances for 3 tabs. Should be fine like this.

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