Fire action on other activity within TabHost? (Android) - 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);
}

Related

Activity in TabHost

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

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.

Chaining activities inside a tab

Here's my problem:
I've have an application with tabs.
in each tab I have a list that list to an other list then to a screen with an image, clickable text etc.
In one word I have Tasks inside tabs.
Question
Despite long seek among forum and tutorials I still can't figure witch is the best pactice to do that : switching activity insde a tab
or changing the view.
Here my code
public class App extends TabActivity {
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; // Reusable 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, Activity0.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("0").setIndicator("0",
res.getDrawable(R.drawable.ic_tab_0))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Activity1.class);
spec = tabHost.newTabSpec("1").setIndicator("1",
res.getDrawable(R.drawable.ic_tab_1))
.setContent(intent);
tabHost.addTab(spec);
then I have to switch activities (Activity0 -> Activity01) inside tab 0
public class Activity0 extends Activity{
..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Toast.makeText(getBaseContext(), "You clciked "+parentTypes.get(arg2).getLibelle(), Toast.LENGTH_LONG).show();
/*TODO database method */
TypeEvenement parent = parentTypes.get(arg2);
if (parent.getChildren().size()!=0)
{
Intent i = new Intent(TypeParentList.this, TypeChildList.class);
int id= new Long(parentTypes.get(arg2).getId()).intValue();
i.putExtra("typeid", id);
View view = lam.startActivity("TypeChildList",i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)).getDecorView();
setContentView(view);
then go back to this activity from Activity01
public class Activity01 extends Activity{
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(TypeChildList.this, Activity0.class);
LocalActivityManager lam = pa.getLocalActivityManager();
View view2 = lam.startActivity("Activity0",i).getDecorView();
setContentView(view2);
}
});
After go and back I get an error java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first.
I know it comes from my view but I can't figure out how to fix it
So is this the best practice to do multiple activity in one tab ?
Please help needed
If you use a ViewSwitcher or ViewFlipper you can keep the View attached to that and switch back and forth to your hearts desire.
One approach with a tab bar or a common shortcut bar is to have a separate class to handle the OnClickListeners that call startActivity(newIntent) and finish() back to back. I'm not too fond of what the default TabView looks like, so I tend to make a graphical toolbar and use this approach.

Change tab using onClickItem in ListActivity

Can someone please tell me how to change tab by clicking on element INSIDE the tab? I already tried it with global data. The code looks like this:
public class Tabs extends TabActivity {
int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
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, Tribocracy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Areas.class);
spec = tabHost.newTabSpec("areas").setIndicator("Areas",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("settings").setIndicator("Settings",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(tabNumber);
}
protected void onResume() {
super.onResume();
GlobalData globalData = ((GlobalData)getApplicationContext());
returnedTabNumber = globalData.getTabNumber();
tabHost.setCurrentTab(returnedTabNumber);
}
}
The global adapter looks like this:
public class GlobalData extends Application {
//----------------------------------------------------
private int Point1; //define the vars here
private int Point2; //define the vars here
private int Point3; //define the vars here
private int Point4; //define the vars here
private int Point5; //define the vars here
private int Point6; //define the vars here
private int tabNumber;
public int getTabNumber() //getter of the value
{
return tabNumber;
}
public int setTabNumber(int number) //setter of the value
{
tabNumber = number;
return tabNumber;
}
}
Now when I'm trying to change tab in my ListActivity tab by clicking on one of the items it doesn't do anything and stays on the ListActivity tab. Perhaps I shouldn't use onResume() here. Basically I want to go to first tab when I click on one of the items in the list. Please help!
One way to do this is to use Intents. And there are many ways to use Intents for this. This works especially well for some cases, for example where the activity is displaying some content from a content provider. I'll describe one possible method below:
For the Activity that hosts the tabs, give it an intent filter if it doesn't already have one. For example, if it's an 'item detail'-style Activity, maybe its intent filter can match for ACTION_VIEW on a certain content type like vnd.android.cursor.item/vnd.somecontent.
Have the Activity take an extra such as focus_tab, and in the Activity's onCreate, after setting up the tabs, set the active tab to the one specified by this focus_tab extra.
Override onNewIntent, and in that method, look for that same focus_tab extra and set the active to the one specified by that extra.
Now, in your button code (i.e. a button inside one of your tab content activities), create an Intent that will start your host Activity, and give it a focus_tab extra corresponding to the tab you want to switch to. Add the FLAG_ACTIVITY_SINGLE_TOP Intent flag so you don't create a new instance of the host Activity. And then, call startActivity on that Intent you just created.
Note: I haven't tried this myself, but it should work in theory.

Categories

Resources