changing activity on sliding the screen - android

i have three activities in my application .
first one is the default activity when i slide right to left second activity should be open with its layout and when i slide left to right third one should open with its layout
i want to change the activities on sliding not only background or layout..
give me appropriate way to do this.
i m using this tab view but this is not giving any slider functionality
public class TabDemo extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host);
TabHost tabHost=getTabHost();
// no need to call TabHost.Setup()
//First Tab
TabSpec spec1=tabHost.newTabSpec("activity 1");
spec1.setIndicator("activity 1");
Intent in1=new Intent(this, activity 1);
spec1.setContent(in1);
TabSpec spec2=tabHost.newTabSpec("activity 2");
spec2.setIndicator("activity 2");
Intent in2=new Intent(this,activity 2);
spec2.setContent(in2);
Toast toast = Toast.makeText(getApplicationContext(), "Welcome in Resident center",Toast.LENGTH_SHORT);
toast.show();
TabSpec spec3=tabHost.newTabSpec("activity 3");
spec3.setIndicator("activity 3");
Intent in3=new Intent(this,activity 3);
spec3.setContent(in3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.setCurrentTab(1);
}
}

Simplest way to create an App with Swiping functionality is in my opinion to create a new Android Application via Eclipse Wizard with following options selected:
Create Activity -> BlankActivity
Navigation Type = Tabs + Swipe
This will create an Application with (i think) 3 pages you can swipe through.
Now you can check the created files/code and change whatever you want/need to.
And as mentioned in the comments to your answer, this isn't possible with Activites, you need to use Fragments!

Related

Opening OptionsMenu on the touch of a tab of the TabActivity

I have a tabActivity where I am adding tabs on runtime. So I think this is the usual code to do that:
_tabSpec = TabHost.newTabSpec("More");
_tabSpec.setIndicator("", Resources.GetDrawable(Resources.Drawable.myIcon).SetContent(intent);
TabHost.AddTab(_tabSpec);
Now the thing is, I have defined an options menu and I want to pop that up when the user clicks on the 'More' tab. I don't know how to do that. I tried not setting a content on that tab and simply use the OpenOptionsMenu() to pop it, but it doesn't seem to work.
Any clue how to achieve that?
P.S.: This is a C# code written in Xamarin. It might not look like the native java-android code, but its almost the same.
Alright, here's an answer to what I was trying to do.
Motive: Add a 'More' tab to the existing TabActivity. When user clicks it, open some kind of a PopUpWindow or ContextMenu, etc.
Steps to do that:
1) Create a tabSpec and add that tab to the TabHost as shown in the question.
2) Now you need to take in this last added tab as a View type variable. Do this by..
View v = TabWidget.GetChildAt(index)
Remember index of tabs starts from 0
3) Now on the onCreate() method of your main activity (one that holds the TabActivity) add an onTouchListener() (I use C#, so I added v.Click+=myFunction()) and write your code of the PopupWindow or ContextMenu or whatever you want to do there.
private String lastTab = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tab1");
tabSpec.setIndicator("Tab 1");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tab2");
tabSpec.setIndicator("Tab 2");
tabSpec.setContent(new Intent(this, TwoActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("more");
tabSpec.setIndicator("More");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equalsIgnoreCase("more")){
openOptionsMenu();
tabHost.setCurrentTabByTag(lastTab);
}
else lastTab = tabId;
}
});
}

How to return to a Parent Activity from a sub activity when using Tabs in Android

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...

not run oncreate() ,on tab change in same page, 2nd time

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

Opening the activity in another Tab on Click

I am using TabHost in my application and I want to open a webPage when I click on the Item from the List of one Tab into the another Tab.
I mean the list is in one Tab and when I click on an item from the list, I want to open the WebPage in to another Tab.
Can I doe this? If yes than please tell me how?
Thanks,
david
In the assumption that you've got your tabs setup like this :
LocalActivityManager localActivityManager = new LocalActivityManager(this, false);
tabHost.setup(localActivityManager);
TabSpec spec = tabHost.newTabSpec("tab1").setIndicator("My List").setContent(R.id.layout_tab1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tab2").setIndicator("My Browser").setContent(R.id.layout_tab2);
tabHost.addTab(spec);
In your action (a button click in the snippet below), just set the current tab on your tabhost.
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tabHost.setCurrentTab(1);
}
});

android first tab intent oncreate always called regardless we set tab2 as default tab

Following is the example of tabs with intent data.
While debugging i found that always when first tab we add in tab host in our case following tab
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("list")
.setContent(new Intent(this, List1.class)));
oncreate method of "List1" intent get called regardless it is our current tab or not even if if i define tab2 as a current tab how to fix this ?
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("photo list")
.setContent(new Intent(this, List8.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
// This tab sets the intent flag so that it is recreated each time
// the tab is clicked.
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("destroy")
.setContent(new Intent(this, Controls2.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
}
setDefaultTab(1);
seems not to be working in TabActivity when separate Activities are used as Tab Content.
Use following instead of this method,
tabHost.setCurrentTab(1);
This will set "photo list" (i.e second tab) as the selected or default tab...
I have found this same behavior as well, and I do not have a specific fix. But I do know of a work-around.
Instead of attaching Activities to each tab, attach a View to each tab. You can then handle the data passing very easily as each view will be in the same Activity. This also eliminates the need to pass information using Intents. Furthermore, you can create (or inflate) your Views as you need them and with more control.
Good luck,
-scott

Categories

Resources