I really want to move from an activity within a tab view to another activity within the same tab.
public void onClick (View view) {
// This creates an intent to call the 'Called' activity
i1 = new Intent(this.getBaseContext(),Called.class);
// calls the method to replace View.
replaceContentView("Called", i1);
}
try this...
TabSpec spec=...//our tabSpec
Intent intent = new Intent(this.getBaseContext(), Called.class);
spec.setContent(videosIntent);
Related
I have following TapControl in the StartActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for A Tap
TabSpec atapspec = tabHost.newTabSpec("ATap");
// setting Title and Icon for the Tab
atapespec.setIndicator("ATap", getResources().getDrawable(R.drawable.state_atap));
Intent atapIntent = new Intent(this, ATapActivity.class);
atapspec.setContent(atapIntent);
// Tab for B Tap
TabSpec btapspec = tabHost.newTabSpec("BTap");
btapspec.setIndicator("BTap", getResources().getDrawable(R.drawable.state_btap));
Intent btapIntent = new Intent(this, BtapActivity.class);
btapspec.setContent(btapIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(atapspec); // Adding a tab
tabHost.addTab(btapspec); // Adding b tab
}
Each Tap has its own Activity.
Now my problem, how can I switch with a button to the next Tap-Activity?
I tried to start just the Activity but then the Tap-control was missing.
startActivity(new Intent(this, BTapActivity.class));
I found that it should be sometion like that:
setCurrentTabByTag("BTab");
but i don't know how.
EDIT:
When I put "tabHost.setCurrentTabByTag("BTap");" on the end of the onCreate Method, BTap is selected.Therefore it is the correct command. But from another Activity I'm not able to access the tabHost.
When I make the the tabHost to a global Object, The App crashes:
TabHost tabHost = getTabHost();
I tried it like this from the Tab-Activity:
startActivity StartAct = new startActivity ();
StartAct.setTap("BTap");
This Method is in the StartActivity:
public void setTap(String tap) {
tabHost.setCurrentTabByTag(tap);
}
What can I do? Sorry, but I'm a beginner ...
You can use TabHost's setCurrentTab(int index) function to open tabs programmatically.
I have two tabs A, B with corresponding TabActivityA and TabActivityB. I have a third ActivityA1 which is not on the tab but is an intermediate activity coming from ActivityA.
Here is the code in sequence
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Home
TabSpec homeSpec = tabHost.newTabSpec("Home");
homeSpec.setIndicator("Home", getResources().getDrawable(R.drawable.icon_home_tab));
Intent homeIntent = new Intent(this, TabActivityA.class);
homeSpec.setContent(homeIntent);
// Tab for my cases
TabSpec helppec = tabHost.newTabSpec("Help");
// setting Title and Icon for the Tab
helppec.setIndicator("Help", getResources().getDrawable(R.drawable.icon_cases_tab));
Intent helpIntent = new Intent(this, TabActivityB.class);
mycasesspec.setContent(helpIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(homeSpec); // Adding home tab
tabHost.addTab(help); // Adding help tab
}
ActivityA1 extends Activity
{
}
TabActivityA extends ActivityGroup
{
.....
Intent nextScreen = new Intent(getApplicationContext(), ActivityA1.class);
View view = getLocalActivityManager().startActivity("ActivityA1", nextScreen.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)).getDecorView();
setContentView(view);
}
Note: I am doing this because I want to show the same tabs for ActivityA1
This does show the tab (Home and Help) on ActivityA1, however on clicking the Home tab I want the user to go to TabActivityA but right now it is staying on ActivityA1 only.
Any help will be greatly appreciated!!
Ok I think I understand what you are asking now, simply keep an arrayList of View, add to this list before you call SetContentView, then when you are ready to go back to the previous View do SetContentView(list[count - 1]), also make sure you remove the previous View and dispose of it or it will hang around in memory forever...
I use a TabHost.
The below code to call AActivity.
intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
And it is in the tab.
But in AActivity I call BActivity.
The BActivity will open new page, but not in the tab.
How to let it on the tab frame?
AActivity use below code to call BActivity:
it = new Intent(this, BActivity.class);
startActivity(it);
If you want to open multiple activity in Tab then on Place of activity use Activity group for par tab and switch view in this activity group for open multiple Activity in single tab
you can take some help from this tutorial
You need to change the current selected tab. There's a method called setCurrentTabByTag(String tag) in the TabHost class that will do that, just pass the tag name of your tab (which in your case is B), or you can use the setCurrentTab(int index) and pass the tab index.
Example. Usually I have a MainActivity class, which is my TabActivity. Inside of this class, I will create all tabs that I need on the onCreate method.
First I set some static int with the tabs indexes.
// Tab index.
public static int FIRST_TAB = 0;
public static int SECOND_TAB = 1;
Later, I create my tabs in the onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setting the content view.
setContentView(R.layout.main);
// Getting the TabHost object.
mTabHost = getTabHost();
// Declaring the Intent object for the tabs.
Intent intent;
// Creating the First tab.
intent = new Intent().setClass(this, MyFirstActivity.class);
addTab(mTabHost, "First", FIRST_TAB, intent)
// Creating the Second tab.
intent = new Intent().setClass(this, MySecondActivity.class);
addTab(mTabHost, "Second", SECOND_TAB, intent);
// Setting the current tab.
switchTab(FIRST_TAB);
}
public void addTab(TabHost host, String title, String tag, Intent intent) {
TabHost.TabSpec spec = host.newTabSpec(tag);
spec.setContent(intent);
spec.setIndicator(title);
host.addTab(spec);
}
And last the method that will change the current tab.
public void switchTab(int index) {
mTabHost.setCurrentTab(index);
}
Later, inside of the MyFirstActivity you can call the MainActivity swichTab method and pass the index of the tab to change it.
You can retrieve the MainActivity calling the getParent() method of the Activity class.
MainActivity parent = (MainActivity)getParent();
In the tab activity class where the tabhost is created, implement the following method.
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
In AActivity/BActivity implement the following method and call it on any event(that you need):
public void switchTabInActivity(long indexTabToSwitchTo){
TabActivity tabActivity;
tabActivity = (TabActivity) this.getParent();
tabActivity.switchTab(indexTabToSwitchTo);
}
Here TabActivity is the class where tabhost is created
I have 4 tabs in a page view when i click first time on tab it executes it oncreate() method of corresponding tab and when i go to other tab on same page and again click on previous clicked tab then its oncreate() method not execute why?why it is not work as button click means each time i click its oncreate() method run.
my code for tab activity class is below
intent1 = new Intent().setClass(this, keywordxmlparsing.class);
spec1 = tabHost.newTabSpec("Activity2").setIndicator("keyword/ search...").setContent(intent1);
tabHost.addTab(spec1);
intent2 = new Intent().setClass(this, filter.class);
spec2 = tabHost.newTabSpec("Activity1").setIndicator("filter search").setContent(intent2);
tabHost.addTab(spec2);
intent3 = new Intent().setClass(this, OpeningToday.class);
spec3 = tabHost.newTabSpec("Activity3").setIndicator("opening today").setContent(intent3);
tabHost.addTab(spec3);
intent4 = new Intent(keywordresulttab.this,Map.class);
spec4 = tabHost.newTabSpec("Activity4").setIndicator("Map").setContent(intent4);
tabHost.addTab(spec4);
dear if i use
#Override
public void onTabChanged(String label) {
// TODO Auto-generated method stub
if(label == "Activity2") {
}
try{
if(label == "Activity4") {
Intent intent4;
intent4 = new Intent(keywordresulttab.this,Map.class);
startActivity(intent4);
Log.i("suiuawhd","maps class");
}
then can i go to other activity means using
intent4 = new Intent(keywordresulttab.this,Map.class);
startActivity(intent4);
we can load again activity on tab click??then what to written in place of
tabHost.addTab(spec3);
intent4 = new Intent(keywordresulttab.this,Map.class);
spec4 = tabHost.newTabSpec("Activity4").setIndicator("Map").setContent(intent4);
tabHost.addTab(spec4);
Tabs that contain activities are implemented by means of ActivityGroup. When someone changes a tab, corresponding activity is created only if necessary. When you move to any tab for the second time, the activity is already existing and only its window is shown.
You should use instead:
TabHost.setOnTabChangedListener(TabHost.OnTabChangeListener l)
TabActivity with separate activities as a content are a little bit tricky so maybe you should consider using Views instead. If not you can use the following code to access all activities from each other:
get instances of content activities from TabActivity:
TabActivity.getLocalActivityManager().getActivity(String name)
where name is given in newTabSpec() method.
get instance of TabActivity from content activities:
FirstTab.getParent()
Using these method you can create communication between all activities (using proper casting). For example:
public void onTabChanged(String label) {
if(label.equals("Activity2")) {
SecondActivity sa = (SecondActivity) getLocalActivityManager().getActivity(label);
sa.modifySomething();
}
}
If you want to change activities under tabs you should use:
tabHost.clearAllTabs ()
and create all TabSpecs once again.
In the android oncreate() lifecycle called once in life of the activity (like constructor), as you press again that time the activity is alive and in memory so it will not call onCreate again.
so you have to implement
TabHost.setOnTabChangedListener(TabHost.OnTabChangeListener l).
I am having following code :
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnSrch){
Intent i = new Intent(this, SearchByCode.class);
View view = SearchGroup.group.getLocalActivityManager()
.startActivity("SearchByCode",i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
SearchGroup.group.replaceView(view);
}
}
But when I am clicking the button it shows the next screen abruptly. I want to show the slide-in animation here.
I can't quite tell but I think you want to know how to launch another activity called SearchByCode from your current activity.
To do this you need to create an Intent object, optionally add flags or bundles to the intent, then call startActivity(Intent).
Here is a code sample:
Intent i = new Intent(this, SearchByCode.class)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(i);