Switch tab in android but content not change - android

I have class CustomTab extends TabActivity:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_tab);
tabHost = getTabHost();
Intent intentA = new Intent(getBaseContext(), A.class);
Intent intentB = new Intent(getBaseContext(), B.class);
Intent intentC = new Intent(getBaseContext(), C.class);
TabSpec tabA = tabHost
.newTabSpec("a")
.setIndicator("",
getResources().getDrawable(R.drawable.icon_a))
.setContent(intentA);
TabSpec tabB = tabHost
.newTabSpec("b")
.setIndicator(
"",
getResources()
.getDrawable(R.drawable.icon_b))
.setContent(intentB);
TabSpec tabC = tabHost
.newTabSpec("c")
.setIndicator("",
getResources().getDrawable(R.drawable.icon_c))
.setContent(intentC);
tabHost.addTab(tabA);
tabHost.addTab(tabB);
tabHost.addTab(tabC);
tabHost.setCurrentTab(0);
and activity B:
Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
((CustomTab) getParent()).getTabHost().setCurrentTab(3);
I want switch to tab 3(activity C) when run activity B so I tried with "((CustomTab) getParent()).getTabHost().setCurrentTab(3);" it only change tab, but content not change, I created a topic similar but not receive correct answer.

I managed this things calling TabActivity again.
In TabActivity
int tabNumber = getIntent().getExtras().getInt("tabNumber");
tabHost.setCurrentTab(tabNumber);
And calling TabActivity in child Activity like your Activity B like this,
Intent intent = new Intent(BActivity.this,
CustomTab.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("tabNumber",3);
startActivity(intent);
Dont no its correct solution for this or not. but I havn't got any other solution.
So, i used this.

Related

one activity to Tabhost Activity with current item

I am creating a android app.
which have a welcome screen where four button (a,b,c,d) have.
when click on the any button(a,b,c,d) its goes to the second activity.
next activity have (tabhost)four tabs(a,b,c,d).
how can it work--
when i click on "a" button in welcome screen it goes to "a tab" of second activity,and another tabs are also working.
when i click on "b" button in welcome screen it goes to "b tab" of second activity,and another tabs are also working.
when i click on "c" button in welcome screen it goes to "c tab" of second activity,and another tabs are also working.
when i click on "d" button in welcome screen it goes to "d tab" of second activity,and another tabs are also working.
public class Dashboard extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
}
public void helpB(View v) {
Button clickedButton = (Button) v;
switch (clickedButton.getId()) {
case R.id.points:
Intent i = new Intent(getApplicationContext(),AndroidActivity.class);
startActivity(i);
break;
case R.id.Search:
Intent i1 = new Intent(getApplicationContext(),AppleActivity.class);
startActivity(i1);
break;
case R.id.AboutUs:
Intent i2 = new Intent(getApplicationContext(),BlackBerryActivity.class);
startActivity(i2);
break;
case R.id.ContactUs:
Intent i3 = new Intent(getApplicationContext(),WindowsActivity.class);
startActivity(i3);
break;
}
}
}
and second Activity code -->
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this,
AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Points",
ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Search",
ressources.getDrawable(R.drawable.icon_apple_config))
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this,
WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setIndicator("About us",
ressources.getDrawable(R.drawable.icon_windows_config))
.setContent(intentWindows);
// Blackberry tab
Intent intentBerry = new Intent().setClass(this,
BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setIndicator(
"Contact us",
ressources
.getDrawable(R.drawable.icon_blackberry_config))
.setContent(intentBerry);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);
// set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
}
}
Do you need this?
public void helpB(View v) {
Button clickedButton = (Button) v;
int tab = 0;
Intent i = new Intent(getApplicationContext(),MainActivity.class);
switch (clickedButton.getId()) {
case R.id.points:
tab = 0;
break;
case R.id.Search:
tab = 1;
break;
case R.id.AboutUs:
tab = 2;
break;
case R.id.ContactUs:
tab = 3;
break;
}
i.putExtra("extra.tab", tab);
startActivity(i);
}
And MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this,
AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Points",
ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Search",
ressources.getDrawable(R.drawable.icon_apple_config))
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this,
WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setIndicator("About us",
ressources.getDrawable(R.drawable.icon_windows_config))
.setContent(intentWindows);
// Blackberry tab
Intent intentBerry = new Intent().setClass(this,
BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setIndicator(
"Contact us",
ressources
.getDrawable(R.drawable.icon_blackberry_config))
.setContent(intentBerry);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);
// set Windows tab as default (zero based)
int tab = getIntent().getExtras().getInt("extra.tab");
tabHost.setCurrentTab(tab);
}

How to call Tab of particular activity from other activity in android

I have created two tabs.Tab A and Tab B. From Tab A, i am navigating to new activity say C. From C how to navigate to Tab A of particular activity. When i tried, it's opening new activity and I can't call which is present inside the tab.Please help me.
public class TabsActivity extends TabActivity {
String Activity;
Intent i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Tab A");
// setting Title and Icon for the Tab
photospec.setIndicator("Tab A");
Intent photosIntent = new Intent(this, A.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Tab B");
songspec.setIndicator("Tab B");
Intent songsIntent = new Intent(this, B.class);
songspec.setContent(songsIntent);
// Tab for Videos
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
}
In A activity using button, i am moving to another activity..say for eg: NewActivity
In NewActivity, how to switch back to A which is present inside Tabs.
NewActivity.java
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(ListActivity.this,
listsearchbutton = (Button) findViewById(R.id.listfilter);
listsearchbutton.setOnClickListener(new View.OnClickListener() {
#Override
TabsActivity.class);
intent.putExtra("Activity", "A");
//i dont know where to use getIntent in TabsActivity to call A activity
startActivity(intent);
}
});
Thanks in advance.
Just use Activityname.this.finish(); on button click

How to use setCurrentTab method in an activity of tab?

I've been having this problem for 1 week.
I have a MainActivity extends TabActivity and A, B two activities in each tab.
Now I want to press a button in activity A to set the current page to the tab of activity B
But I can't use setCurrentTab method except in MainActivity.
How can I make this function work?
public class MainActivity extends TabActivity
public TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, StartActivity.class);
spec = tabHost.newTabSpec("tab1").setIndicator("A",res.getDrawable(R.drawable.start))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, WeatherActivity.class);
spec = tabHost.newTabSpec("tab2").setIndicator("B",res.getDrawable(R.drawable.weather))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
public class AActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_layout);
Button b = (button)findViewById(R.id.bt);
b.setOnClickListener(this);
}
public void onClick(View v) {
tabHost.setCurrentTab(1);
//Can't work here
}
}
Try making the following changes :
In Main activity :
Make the tabHost variable Static.
private static TabHost tabHost;
Add a new function to fetch the current tabhost.
public static TabHost getCurrentTabHost(){
return tabHost;
}
In AActivity, Use like this :
MainActivity.getCurrentTabHost().setCurrentTab(1);
If required, you can go for a null check.

How to call methods on the individual activities added to TabActivity?

I have created a TabActivity, which has two tabs and each of the tabs displays separate activities. I am unable to call method onConfigurationChanged, defined in both of these activities, when a user changes the tabs. Here is the code:
public class MyTabActivity extends TabActivity implements OnTabChangeListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_page);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec = null;
Intent intent = null;
tabHost.setOnTabChangedListener(this);
// ACTIVITY 1
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("act1").setIndicator("ACT1",
res.getDrawable(R.drawable.tab_home))
.setContent(intent);
tabHost.addTab(spec);
// ACTIVITY 2
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity2.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("act2").setIndicator("ACT2",
res.getDrawable(R.drawable.tab_home))
.setContent(intent);
tabHost.addTab(spec);
}
#Override
public void onTabChanged(String arg0) {
Log.d("DGTabActivity", "tab changed");
if (tabHost.getCurrentTab() == 0) {
// How to call method on Act1??
callOnConfigurationChanged("com.tab.Activity1"); // doesn't work
}
else if (tabHost.getCurrentTab() == 1) {
// How to call method on Act2??
}
}
I also tried to use the following code to call the method onConfigurationChanged, within the onTabChanged method, but it throws exception:
private void callOnConfigurationChanged (String className) {
try{
//onConfigurationChanged
//Class amnClass = Class.forName("android.app.ActivityManagerNative");
Class amnClass = Class.forName(className);
Object amn = null;
Configuration config = null;
Resources res = null;
// res = amn.getResources();
Method methodGetConfiguration = amnClass.getMethod("getResources");
methodGetConfiguration.setAccessible(true);
res = (Resources) methodGetConfiguration.invoke(amn);
config = res.getConfiguration();
// amn.onConfigurationChanged(config);
Method methodOnConfigurationChanged = amnClass.getMethod("onConfigurationChanged");
methodOnConfigurationChanged.setAccessible(true);
methodOnConfigurationChanged.invoke(amn, config);
}
catch (Exception e) {
Log.d("Tab host Exception" , e.toString());
}
}
Thanks for your time.
cheers
Ani
You can use intent filters. Broadcast configuration-changed
event in your tabs' activity and add listeners to activities in your tabs.

change from a tab to another

i want to make an intent from a tab to another.. how can i do that?
i only now how to make between activity's. and i need to go from a tab to another..
i have this code for the tabs
public class Main extends TabActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
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, Contas.class);
Intent a = new Intent(Main.this, Contas.class);
Intent b = new Intent(Main.this, Registros.class);
Intent c = new Intent(Main.this, Relatorios.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Contas").setIndicator("Contas",
res.getDrawable(R.drawable.ic_tab_accounts))
.setContent(a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
// Do the same for the other tabs
//intent = new Intent().setClass(this, Registros.class);
spec = tabHost.newTabSpec("Registros").setIndicator("Registros",
res.getDrawable(R.drawable.ic_tab_registry))
.setContent(b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
//intent = new Intent().setClass(this, Relatorios.class);
spec = tabHost.newTabSpec("Relatorios").setIndicator("Relatorios",
res.getDrawable(R.drawable.ic_tab_reports))
.setContent(c.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Rather then calling intent you can just set the desired tab selected.
tabHost.setCurrentTab(index)
Rather then calling intent you can just set the desired tab selected.
Use
intent.putExtra("tabIndex", index);
and call the activity. Now in the calling Activity's onCreate() or onResume() use
int index = getIntent().getExtraInt("tabIndex", -1);
if(index != -1)
tabHost.setCurrentTab(index);

Categories

Resources