Switch Taps from Activitys by buttons, etc - android

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.

Related

How to Start New activity in Tab without using ActvityGroup

I have not used the activityGroup in my project. Now I'm not is a position to implement the whole project using Activity group.
is it nesssory that I must implement the activityGroup class in my project to do so?
If yes then please give links for the basic tutorial of activityGroup implementation.
Here is my MainActvity.java which loads 4 other actvities in 4 tabs.
public class MainActivity extends TabActivity {
TabHost tabHost;
Context context = MainActivity.this;
Button btnGo;
TabSpec spec;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btn_GO);
tabHost = getTabHost();
// Android tab
Intent intentHome = new Intent();
intentHome.setClass(this, Home.class);
TabSpec tabSpecHome = tabHost
.newTabSpec("Home")
.setIndicator("Home",
getResources().getDrawable(R.drawable.home))
.setContent(intentHome);
tabHost.addTab(tabSpecHome);
Intent intentNowReading = new Intent().setClass(this, NowReading.class);
TabSpec tabSpecNowReading = tabHost
.newTabSpec("Now Reading")
.setIndicator("Now Reading",
getResources().getDrawable(R.drawable.now_reading))
.setContent(intentNowReading);
tabHost.addTab(tabSpecNowReading);
Intent intentFavourite = new Intent().setClass(this, Favorites.class);
TabSpec tabSpecFavourite = tabHost
.newTabSpec("Favourite")
.setIndicator("Favorites",
getResources().getDrawable(R.drawable.favorites))
.setContent(intentFavourite);
tabHost.addTab(tabSpecFavourite);
Intent intentProfile = new Intent().setClass(this, Profile.class);
TabSpec tabSpecProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile",
getResources().getDrawable(R.drawable.profile))
.setContent(intentProfile);
tabHost.addTab(tabSpecProfile);
tabHost.setCurrentTabByTag("Home");
...}
now I want to start the new activity in the Home tab area on the click event of Go button.(See the picture).
please note that I do not want to impelement the ActivityGroup class, How can I do that without this.
New Actvity must load in the HomeTab's area, not on the full screen.
ActvityGroup is a bad idea, this is old, deprecated API, don't use it.
You have to use Fragments API, just create a Fragment and add it to layout using FragmentTransaction, thats all what you need.

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

Fire action on other activity within TabHost? (Android)

I am currently in an Android project where our main view is a TabActivity and each tab is a separate Activity. One is a MapActivity and the other two are ordinary Activities.
First note that I think we must have each tab as separate activities, as there is too much code in the separate activities to just have the TabHost switch the content view on a tab change and have all of the code in the same class. Anyways, back to the problem.
One of the tabs include a button, which when pressed should make the TabActivity switch to the MapActivity and animate the map to a specific location.
The tutorial found on http://joshclemm.com/blog/?p=86 shows how to do it if the TabHost contains a mapview and a listview. If an item in the ListView is clicked, the TabHost switches to the mapview and animates to that location (those coordinates). This is exactly what i need to do when the button in the separate activity is pressed.
The MainView.java is created as follows:
public class MainView extends TabActivity implements OnTabChangeListener{
#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
intent = new Intent().setClass(this, MapGUI.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MissionView.class);
spec = tabHost.newTabSpec("mission").setIndicator("Mission",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SIPView.class);
spec = tabHost.newTabSpec("call").setIndicator("Call",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
The MissionView.java is as follows:
public class MissionView extends Activity implements Observer{
MissionController mc;
private TextView missionheader, missiondescription, missionaddress,
missiontime, missioninjuries;
private Button changedesc, gotoadress;
private String[] mission;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.missiontablayout);
missionheader = (TextView)findViewById(R.id.missionheader2);
missiondescription = (TextView)findViewById(R.id.missiondescription2);
missionaddress = (TextView)findViewById(R.id.missionaddress2);
missiontime = (TextView)findViewById(R.id.missiontime2);
missioninjuries = (TextView)findViewById(R.id.missioninjuries2);
changedesc = (Button)findViewById(R.id.gotoaddress);
changedesc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// DO SOMETHING HERE?
}
});
mc = new MissionController(MissionView.this);
}
public void update(Observable observable, Object data) {
if(data instanceof String[]){
mission = (String[]) data;
updateView(mission);
}
}
public void updateView(String[] missiontext){
missionheader.setText(missiontext[0]);
missiondescription.setText(missiontext[1]);
missionaddress.setText(missiontext[2]);
missiontime.setText(missiontext[3]);
missioninjuries.setText(missiontext[4]);
}
}
Anyone know how i could achieve this?
Note that the code supplied above has no implementation to actually draw to the actual location, but the question still remains, how do I make a button pressed in one activity to switch tab in the TabHost and fire a change on that tab activity?
changing tabs in a TabHostcan easily be done with setCurrentTab(int)
http://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)
Sending events to other Activities can simply be achieved by sending a broadcast intent and receiving/handling it in the other Activity.
Alternatively you could save static references to all your tab Activities (ugly...) and call methods directly.
Place the below line on button click where you want to switch to the Map activity
((MainView) getParent()).setTabMap();
and in MainView create the following function
public void setTabMap()
{
//as Map activity is your first tab so pass 0 as index
getTabHost().setCurrentTab(0);
}

Tab switching in android?

i have Tab host which is shown below,
private TabHost myTabHost;
......
setContentView(R.layout.vidtab);
Intent intent=getIntent();
intent = new Intent().setClass(this, RecordActivityGroup.class);
myTabHost = (TabHost)this.findViewById(android.R.id.tabhost);
myTabHost.setup();
TabSpec rectab = myTabHost.newTabSpec("Record");
rectab.setIndicator("Record",getResources().getDrawable(R.drawable.irecord));
rectab.setContent(intent);
myTabHost.addTab(rectab);
intent = new Intent().setClass(this, sharingProject.class);
TabSpec setting = myTabHost.newTabSpec("Hint");
setting.setIndicator("Hint",getResources().getDrawable(R.drawable.isettings));
setting.setContent(intent);
myTabHost.addTab(setting);
in that record Activity group i have following code,
RecordActivityGroup extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("RecordingActivity", new Intent(this,Record.class));
}
and Record.class is an activity which contain one button, i need to switch to second tab while clicking button in Record class, how to do that? i any one know that please help me out.
You can use an method in your main class like following
public void switchToTab(int tabid){
myTabHost.setCurrentTab(tabid);
}
And now in Record class on your Button click call this
YourStartCalss parentActivity;
parentActivity= (YourStartCalss) this.getParent();
parentActivity.switchToTab(yourtabid);

Switch between Android tabs using intents

I wish to find out how to switch between tabs using intents.
In my case I'm using the both tabs:
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
// Capture tab
spec = tabHost.newTabSpec("capture").setIndicator(null,
res.getDrawable(R.drawable.ic_tab_capture))
.setContent(new Intent(this,CaptureActivity.class));
tabHost.addTab(spec);
// Upload tab
spec = tabHost.newTabSpec("upload").setIndicator(null,
res.getDrawable(R.drawable.ic_tab_capture))
.setContent(new Intent(this,ImageUpload.class));
tabHost.addTab(spec);
To simplify my goal, my CaptureActivity.java includes the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
Intent intent = new Intent(this, ImageUpload.class);
startActivityForResult(intent, 0);
}
What I'm expecting is, the app should switch instantly to the second tab (ImageUpload activity) which works fine, BUT the tabs themselves disappear. I get the ImageUpload activity as stand-alone page, not within the tab itself.
Any idea what's going wrong there ?
First calling firing ImageUpload.java only fires ImageUpload.class but surely tabhost will disappear.
You need to fire your MainActivity-TabActivity where you added your two tabHost
1.ImageUpload
2.CaptureActivity
which will maintain your TabLayout
call intent like these
Intent i=new Intent(getApplicationContext(),MainActivity.class)//which is your mainActivity-Launcher
i.addFlags(Intent.FLAG_ACTIVITY_BRING_TO_FRONT);
startActivity(i);//will bring MainACtivity to Front
Now Main activity is already running so pointer directly goes to onNewIntent() inside Main Activity
Override onNewIntent() inside MainActivity
=====================================
public class MainActivity extends TabActivty
{
pubilc static TabHost tabhost;
public void onCreate()
{
//where you added your two tab spec
//added them
}
public void onNewIntent(intent)
{
super.onNewIntent(intent);
tabHost.setCurrentTab(1);//1-depends where you want to switch-tabIndexno, I assume Upload is at 2nd position (so "1")
Activity currentActivity=getCurrentActivity();
if(currentActivity instanceof Upload)
{
((upload)currentActivity).onCreate();//watever method you want to call
}
}
}
In the parent activity class where the tabhost is created I implemented a method like the one below:
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
Inside of the tab that I would like to be able to switch internally to another tab I created the method below:
public void switchTabInActivity(int indexTabToSwitchTo){
YOURTABHOSTACTIVITY ParentActivity;
ParentActivity = (YOURTABHOSTACTIVITY) this.getParent();
ParentActivity.switchTab(indexTabToSwitchTo);
}
If you would like a good example of this code, you can take a look at my MintTrack project here and here.

Categories

Resources