change from a tab to another - android

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

Related

Switch tab in android but content not change

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.

Android tab listener wont refresh on selected tab

I hope you can help, I can't seem to get the current tab to reload if a tab is currently selected and then a user click on it again, could someone point out what I'm doing and perhaps modify my code to show me what I'm doing wrong as I have viewed a lot of threads on here and via google but now one seems to know the answer, that or I'm just dumb :D thank you :)
public class HelloTabWidget extends TabActivity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
final 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, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
// Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());
switch (tabHost.getCurrentTab()) {
case 0:
//do what you want when tab 0 is selected
test();
break;
case 1:
//do what you want when tab 1 is selected
break;
default:
break;
}
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
First create one variable in application class like below:
public class GlobalClass extends Application {
public int displayTab = 0;
public int getDisplayTab() {
return displayTab;
}
public void setDisplayTab(int displayTab) {
this.displayTab = displayTab;
}
}
and modify your Activity as below:
public class HelloTabWidget extends TabActivity implements OnClickListener {
GlobalClass globel;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
globel = (GlobalClass) getApplication();
Resources res = getResources(); // Resource object to get Drawables
final 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, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(globel.getDisplayTab());
//Here Handling first tab click
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
test();
Log.d("Clickedddddddddddd", "1"
+ getTabHost().getCurrentTabTag());
HelloTabWidget.this.finish();
globel.setDisplayTab(0);
Intent i = new Intent(HelloTabWidget.this, HelloTabWidget.class);
startActivity(i);
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
}

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.

Progress bar in Tab View

I have created a tab view. What i want is that when i switch between 2 views of my tab a progress bar is generated. How can i do so.....
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
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
TabWidget tabs = new TabWidget(this);
tabs.setId(android.R.id.tabhost);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ImageExercise.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("images").setIndicator("Images",
res.getDrawable(R.drawable.imageicon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AudioPlay.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.audioicon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, VideoActivity.class);
spec = tabHost.newTabSpec("videos").setIndicator("Videos",
res.getDrawable(R.drawable.videoicon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}

How to keep the header of a Tab in a SubActivity?

i've got this Tabhost:
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
/** Initialization Tab1 */
intent = new Intent(this, Tab1.Class);
spec = tabHost.newTabSpec("Tab1").setIndicator("Tab1").setContent(intent);
tabHost.addTab(spec);
/** Initialization Tab2 */
intent = new Intent(this, Abstand.class);
spec = tabHost.newTabSpec("Tab2").setIndicator("Tab2").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
The content of Tab2 has some buttons, when one of these is clicked, it starts a SubActivity with:
final Intent i = new Intent(this, SubActivity.class);
final Bundle b = new Bundle();
this.mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
b.putString("key", "value");
i.putExtras(b);
startActivityForResult(i, REQUEST_CODE);
}
});
Within the SubActivity, the Tabhost isnt available anymore... so how can the Code be changed, showing the Tabhost in a SubActivity?

Categories

Resources