When switching tabs in Android, what does it constitute? - android

I have three tabs and each one is its own activity. When I switch tabs I want my Spinner to update but I don't know what method gets called on tab switch. Any help?

Hook up a listener to the TabHost.OnTabChangeListener. You main activity extends TabActivity and its onCreate method probably looks something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
}
To hook up the listener, add the following code in your onCreate() method:
tabHostt.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(tabId.equals("tab1")) {
//tab1
}
else if(tabId.equals("tab2")) {
//tab2
}
else if(tabId.equals("tab3")) {
//tab3
}
}
});
HTH

Something like this should work:
tabHost.setOnTabChangedListener(new OnTabChangeListener()
{
public void onTabChanged(String tabId)
{
//Do stuff in here
}
});

Related

Move back to parent activity from child activity android

In my app, I have one tab which includes two activities say 1 and 2. Activity 1 extends ActivityGroup, which has one child activity say A. Now scenario becomes like this,
In first tab --> Activity 1 and its child activity A.
In second tab --> Activity 2. After doing some functionality in Activity 1 of first tab, user moves to child activity A of first tab. Now what I want to do, when user presses back button from child activity A, user should move back to its parent activity 1. How to do that? Below is my code..
MainActivity.java
public class MainActivity extends TabActivity {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
tabHost = getTabHost();
Intent intentProfile = new Intent().setClass(this, Tab1.class);
TabSpec tabProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentProfile);
Intent intentFriends = new Intent().setClass(this, Tab2.class);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("Friends", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentFriends);
tabHost.addTab(tabProfile);
tabHost.addTab(tabFriends);
tabHost.setCurrentTab(0);
}
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
#Override
public void onBackPressed() {
// Called by children
MainActivity.this.finish();
}
}
Tab1.java
public class Tab1 extends ActivityGroup {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Tab1.this, Second.class);
replaceContentView("activity3", in);
}
});
}
public void replaceContentView(String id, Intent newIntent)
{
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
}
Second.java
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Tab2.java
public class Tab2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
#Override
public void onBackPressed() {
// this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Here I can move to tab 1 from tab 2 on back button pressed. But, I can't move back to parent activity from child activity of tab 1.
You have to override back button in parent activity and make a call from child. For more info please check this link : Navigate Between Activities in an ActivityGroup
In Tab1:
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public void onPause()
{
super.onPause();
finish();
}
in Tab1

onTabChanged method is not inwalking,

My Method onTabChanged() is not called when I clicked the tab bar to change the tab
public class ZoobuzzActivity extends TabActivity implements OnTabChangeListener {
/** Called when the activity is first created. */
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_tab);
}
public void onTabChanged(String tabId) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.GREEN);
}
}
please help me I want to change color when tab selection is change.
You need to set OnTabChangeListener to your TabHost as below:
//set tab change listener
tabHost.setOnTabChangedListener(this);
//where 'this' is a reference to your activity ZoobuzzActivity

Problems with OnClickListener for a tab in android

I'm new to android programing and I'm having problems with OnClickListener for tabs in my app. I found on stack a solution how it should be done, but for some reason it's not working.
I'm trying to use the 2nd answer
For some reason I'm getting 2 errors.
First one is on the name on of my activity: The type DragonLords must implement the inherited abstract method View.OnClickListener.onClick(View).
Second one is on the OnClick method: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method.
Here is a part of my code:
public class DragonLords extends TabActivity implements OnClickListener{
/** 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 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, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("home",
res.getDrawable(R.drawable.hometab))
.setContent(intent);
tabHost.addTab(spec);
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
After that I'm creating more tabs. With out the onclicklistener it's working, the thing is I need to be able to reload the tabs when they are active.
Anyone have an idea what I'm doing wrong?
I added the necessary imports.
Gatz
You must implement the onClick method not in an anonymous inner class like you have done in your code.
Try using new TabWidget.OnClickListener instead of just the normal OnClickListener
Something akin to the following:
public class TestActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getTabWidget().getChildAt(0).setOnClickListener(new TabWidget.OnClickListener() {
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
}
public void onClick(View theView) {
// Do something with view here
}
}
you are doing it pretty hard way.. i did it like this...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tablayout);
res = getResources();
tabHost=(TabHost)this.findViewById(android.R.id.tabhost);
tabHost.getTabWidget().setDividerDrawable(R.drawable.vertical_seperator);
setupTab(new TextView(this), "Login",new Intent().setClass(this,loginForm.class));
setupTab(new TextView(this), "Can't Login",new Intent().setClass(this,ForgotPwd.class));
setupTab(new TextView(this), "Register",new Intent().setClass(this,RegisterUser.class));
}
private void setupTab(final View view, final String tag,final Intent myIntent)
{
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(
new TabContentFactory()
{
public View createTabContent(String tag)
{return view;}
}).setContent(myIntent);
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
hope this helps....
tabs_bg is just an xml with
<TextView android:id="#+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="15dip"
android:textColor="#drawable/tab_text_selector" />

change TabActivity title dynamically according to selected activity tab data

i have created MyTabActivity extending TabActivity class with two tabs.
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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) {
}
});
tabHost.setCurrentTabByTag(TAB_1);
}
}
i need to change the title of MyTabActivity according to change in one of activity data.
public class first extends Activity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//change the MyTabActivity title from here ???
}
}
Please help me how to do this.
Thanks
You can get your TabActivity inside child Activity like this.
If you are in FirstActivity or SecondActivity try something like this.
MyTabActivity myTabs = (MyTabActivity) this.getParent();
myTabs.setTitle("This is First cool Activity");
you can do the same in SecondActivity.
Cheers!!

How to close TabWidget when a tab is selected in Android

In Android, how do I close TabWidget when a tab is selected?
I want close the TabWidget where some tab is selected.
//I use the OnTabChangeListener solve the question.
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if (DEBUG) Log.d(TAG, "onTabChanged()");
if("testTabId".equals(tabId)) {
//destroy earth
Intent intentTabMe = new Intent(MainActivity.this, UserDetailsActivity.class);
intentTabMe.setAction(Intent.ACTION_VIEW);
startActivity(intentTabMe);
}
}
});

Categories

Resources