Tabs With Intents

131

If you look at the Android widgets and classes related to tabs, you’ll see that a tab can have either a View or an Activity as its contents. Most of the time, people use Views, as they are simpler to implement and lighter-weight to run. However, there may be circumstances where you feel putting your activities in tabs would be useful, so let’s take a look at how that is done, in a modified excerpt from Version 1.9 of The Busy Coder’s Guide to Android Development.

If you want to use an Activity as the content of a tab, you provide an Intent that will launch the desired Activity; Android’s tab-management framework will then pour the Activity’s user interface into the tab. So, let’s create a tabbed Web browser.

Your natural instinct might be to use an http: Uri as the target of the Intents, one Intent per tab. That way, you could use the built-in Browser application and get all of the features that it offers. Alas, this does not work. It appears you cannot host other applications’ activities in your tabs, only your own activities, presumably for security reasons.

But, we can always use the WebView widget to provide a simplified browsing experience.

Here is the source to the main activity, the one hosting the TabView:

[sourcecode language=”java”]
public class IntentTabDemo extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TabHost host=getTabHost();

host.addTab(host.newTabSpec(“one”)
.setIndicator(“CW”)
.setContent(new Intent(this, CWBrowser.class)));
host.addTab(host.newTabSpec(“two”)
.setIndicator(“Android”)
.setContent(new Intent(this, AndroidBrowser.class)));
}
}
[/sourcecode]

As you can see, we are using TabActivity as the base class, and so we do not need our own layout XML — TabActivity supplies it for us. All we do is get access to the TabHost and add two tabs, each specifying an Intent that directly refers to another class. In this case, our two tabs will host a CWBrowser and an AndroidBrowser, respectively.

Those latter two activities are simple wrappers around WebView:

[sourcecode language=”java”]
public class CWBrowser extends Activity {
WebView browser;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

browser=new WebView(this);
setContentView(browser);
browser.loadUrl(“http://commonsware.com”);
}
}
[/sourcecode]

[sourcecode language=”java”]
public class AndroidBrowser extends Activity {
WebView browser;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

browser=new WebView(this);
setContentView(browser);
browser.loadUrl(“http://code.google.com/android”);
}
}
[/sourcecode]

They simply load a different URL into the browser: the CommonsWare home page in one, the Android home page in the other.

The resulting UI shows what tabbed browsing could look like on Android:

Using distinct subclasses for each targeted page is rather wasteful. Instead, we could have packaged the URL to open as an “extra” in an Intent and used that Intent to spawn a general-purpose BrowserTab activity, which would read the URL out of the Intent “extra” and use this. The proof of this is left as an exercise for the reader.

131 COMMENTS

  1. One more question.
    Say I have a tab called A
    I have an activity displayed on it on Tab A

    How do i display a new activity on Tab A without leaving the tab View?

    i.e. How do I launch a new activity within the current Tab?

  2. IN RESPONSE TO THE ABOVE:

    YES, sure its very possible.

    Instead of
    this.startActvity(…)
    use
    this.getLocalActivityManager().startActivity(…)

  3. I can’t seem to run that code… nor any code similar to that for that matter…

    Can anyone confirm this code still works for the SDK v1.1?

  4. @r45k:

    The code worked for me a week or two ago on the 1.1r1 SDK.

    Visit the CommonsWare site and download the source code to my book. You’ll find the above code in a complete project (Activities/IntentTabs). Try that and see if it works for you.

    If not, join the [cw-android] Google Group and post your symptoms, and I can look into the problem.

  5. didn’t work for me either

    no compiler errors but when i run in the emulator i get the following error message:

    “The application Tabs with Intents (process com.android.test) has stopped unexpectedly. Please try again”

    This is happening on SDK v1.1

    cheers

  6. Hello Mark…

    Even I am facing the same issue… Please lemme know how to tackle it… No Compilation errors … No Warnings… Simply.. the app fails to execute…

  7. The code shown in this blog post is not ALL the code you need to run the application. For example, you need to add your two in-tab activities to your AndroidManifest.xml file.

  8. Aha! My comment-posting problems seem to be when I include a URL. So, let’s try a more elaborate reply…

    The code shown in this blog post is not ALL the code you need to run the application. For example, you need to add your two in-tab activities to your AndroidManifest.xml file.

    A complete version of this project can be found in the source code for my Android book — visit http://commonsware.com/Android/ and scroll down to the Source Code link. In the ZIP file, you will find Activities/IntentTabs that contains the complete project.

  9. Hello Mark…

    Thanks for the reply…

    I did download that source code and tried running the project that youve provided… it dint workout… it didnt run either… 🙁

  10. Hello Mark;

    Reinstalled the whole environment!!!

    Problem Solved…. Thanks a lot for support…

    I am developing a downloader based app…. U have any resources to refer and you could point me to?? Would be of great Help for me…

    Regards n Best Wishes

  11. @Sujay:

    Glad you got it working!

    I am not sure what a “downloader based app” is. I would recommend you post your query to the [android-developers] Google Group, anddev.org, or the Android board at JavaRanch. Be sure to describe in somewhat greater detail what it is you are trying to do or need assistance with.

  12. Hello Mark;

    Thnaks for the great suggestion last time…

    Ive built the app halfway…

    Now my problem is… the user is forced to use the search feature which has been implmented using html and php ….. which further yeilds the download result and download link…

    Everything works fine untill user searches for the keywords…. but the search results are displayed in a new browser window…. and it escapes out of my actual application.
    The same happens even when i try to open a hyperlink… how do i keep the whole process contained within that tab??

    My whole aplication would require the webpage in these stages..

    1-> On Application start; it will show the search page in the search tab and allow user to search…

    2-> It must show the search results and download link within the tab…. ( Here it escapes it of the app… and opens the browser)

    3-> On clicking on the download link…. the downloader is invoked… ( Here ill be forcing my downloader function to take control)

    Currently my aim is to keep the stage 2 within the tab….
    Help will be much appreciated….

    Regards;
    Sujay

  13. Hi, nice tutorial!

    I followed the example, then made one of the child activities (e.g. CWBrowser) display a dialog using showDialog(). Now if I rotate the device, the dialog disappears and is not re-created, although dialogs of the parent activity (e.g. IntentTabDemo) are re-created.

    Anyone else experiencing this problem? Any ideas on how to solve it?

  14. Читал про это уже на каком то другом сайте, но у вас гораздо прикольней написано 😉

  15. Hey Mark,

    doesnt think open the URL in a new view?

    Ive followed this and while it did infact load a page when that tab was selected, it was not in the view within the tab, it was a new window and not as show in your pics.

    Any Idea what could be causing this?

    many thanks

  16. Just a question:

    What is the beast and easiest way to add an icon to the tab?

    Thank a lot for any clue/answer

  17. @Profete162:

    Resources res = getResources();
    spec = tabHost.newTabSpec("Items").setIndicator("Items", res.getDrawable(R.drawable.items48_mdpi)).setContent(intent);

  18. Can anyone get the back button to work with this? I setup a way to do it and it crashes the app every time the back button is pressed. I want it to go back to a previous page.

  19. Thanks for posting this tutorial. Once I figured out that I needed to update the manifest, I got it running. I’ve got two questions:
    (1) Originally I put the small Activity classes inside the main IntentTab class. I though if I did that I didn’t need to declare it in the manifest. Is that not true? Or is nesting an Activity class inside another a no-no?
    (2) Now that the app is running, when I select the Android Browser tab it kicks off a browser window seperate from the IntentTab app. That doesn’t appear to be the expected result from your screen shots above.

    Thanks!

  20. Gems {from the|in the|from your|through the|with the|within the} {internet|web|world wide web|net|online|world-wide-web|online world|word wide web|on-line|on line|web-based}…

    highly handful of web-sites that happen to become complete underneath, from our perspective are unquestionably clearly definitely worth looking at…

  21. {Blogs|Weblogs} ou {should|ought to|must|really should|need to|should really|will need to|should certainly|have to|may want to|should preferably} be {reading|studying|reading through|looking at|examining|looking through|browsing}…

    The following is really a appealing Blog page You could possibly Come across helpful that we Stimulate You to see…

  22. Check out was remarkable, remain in contact…

    you should have a look at the online sites we adhere to, together with this an individual, given it signifies our picks with the web site…

  23. {Check|Verify|Examine|Check out|Test|Look at|Take a look at|Investigate} this out…

    which is the conclusion of this content. Below you will obtain some sites that we presume you’ll appreciate, just click on the backlinks through…

  24. {Cool|Awesome|Great|Amazing|Interesting|Neat|Fascinating|Trendy|Nice|Wonderful|Fantastic} {sites|websites|web sites|internet sites|web-sites|web pages|online websites|online sites|webpages|internet websites}…

    we came throughout a fascinating web site that you could possibly appreciate. Take a look in order for you…

  25. Gems within the world-wide-web…

    highly couple of web-sites that come about to be specific below, from our point of view are undoubtedly very well worthy of trying out…

  26. Look over was unique, remain in contact…

    you should take a look at the online websites we adopt, consisting of this an individual, due to the fact it signifies our picks within the web…

  27. {You should|You need to|You ought to|You must|It is best to|You’ll want to|It’s best to|Make sure you|You might want to|It is advisable to|You really should} {check|verify|examine|check out|test|look at|take a look at|investigate} this out…

    Web pages of curiosity we now have a backlink to…

  28. Gems {from the|in the|from your|through the|with the|within the} {internet|web|world wide web|net|online|world-wide-web|online world|word wide web|on-line|on line|web-based}…

    particularly few web-sites that come to pass to be specific underneath, from our perspective are undoubtedly effectively well worth looking at…

  29. Intercontinental Customers Machine analysis…

    please have a look at the world wide web pages we adhere to, similar to this just one certain, because it signifies our picks from your web site…

  30. {You should|You need to|You ought to|You must|It is best to|You’ll want to|It’s best to|Make sure you|You might want to|It is advisable to|You really should} {check|verify|examine|check out|test|look at|take a look at|investigate} this out…

    Websites of fascination we now have a website link to…

  31. Intercontinental Customers Machine analysis…

    please have a look at the world wide web pages we adhere to, similar to this just one certain, because it signifies our picks from your web site…

  32. {Fastest|Quickest|Speediest|Swiftest} {way to|method to|approach to|strategy to|technique to|solution to|option to|way for you to|tactic to|route to|process to} {burn|burn up|burn off|melt away|melt off} {belly|stomach} {fat|body fat|excess fat|unwan…

    Each individual once inside of a when we choose for weblogs that we check out. Outlined underneath are the most active web pages that we decide for…

  33. {Read|Study|Examine|Go through|Browse|Read through|Learn|Check out|Look over|Look at} was {interesting|fascinating|intriguing|exciting|appealing|engaging|attention-grabbing|unique|remarkable|captivating|useful}, {stay|remain|keep|continue to be} {in …

    please see the webpages we carry out, such as this one, as it represents our picks with the web…

  34. Examine was fascinating, remain in contact…

    make sure you stop by the web-sites we follow, which include this 1, given it signifies our picks through the web…

  35. {Cool|Awesome|Great|Amazing|Interesting|Neat|Fascinating|Trendy|Nice|Wonderful|Fantastic} {sites|websites|web sites|internet sites|web-sites|web pages|online websites|online sites|webpages|internet websites}…

    we came across a interesting webpage that you would possibly love. Consider a glance in order for you…

  36. Gems {from the|in the|from your|through the|with the|within the} {internet|web|world wide web|net|online|world-wide-web|online world|word wide web|on-line|on line|web-based}…

    rather very few web pages that occur to become thorough underneath, from our point of view are without doubt nicely price checking out…

  37. Gems {from the|in the|from your|through the|with the|within the} {internet|web|world wide web|net|online|world-wide-web|online world|word wide web|on-line|on line|web-based}…

    exceptionally very few websites that transpire to generally be precise underneath, from our perspective are unquestionably nicely definitely worth trying out…

  38. {Websites|Web sites|Internet sites|Sites|Internet websites|Web pages|Web-sites|Website pages|Online websites|Webpages|Internet sites} {you should|you need to|you ought to|you must|it is best to|you’ll want to|it’s best to|make sure you|you might wa…

    beneath you are going to find the hyperlink to some sites that we consider it is advisable to take a look at…

  39. Gems {from the|in the|from your|through the|with the|within the} {internet|web|world wide web|net|online|world-wide-web|online world|word wide web|on-line|on line|web-based}…

    very few online websites that happen to become thorough below, from our standpoint are definitely perfectly worthy of looking at…

  40. {Awesome|Amazing|Great|Wonderful|Brilliant|Magnificent|Fantastic|Impressive|Remarkable|Excellent|Spectacular} {website|web site|site|internet site|web page|webpage|web-site|online site|blog|websites|web pages}…

    some time to check out or take a look at the content or internet sites we have linked to underneath the…

  41. {Websites|Web sites|Internet sites|Sites|Internet websites|Web pages|Web-sites|Website pages|Online websites|Webpages|Internet sites} {worth|really worth|well worth|value|truly worth|worthy of|price|definitely worth} {visiting|going to|checking out|b…

    listed here are a few one way links to web-sites that we website link to as a result of we assume they really are well worth going to…

  42. {Blogs|Weblogs} ou {should|ought to|must|really should|need to|should really|will need to|should certainly|have to|may want to|should preferably} be {reading|studying|reading through|looking at|examining|looking through|browsing}…

    Below is usually a Very good Web site You might Come across Interesting that we Persuade You…

  43. {Websites|Web sites|Internet sites|Sites|Internet websites|Web pages|Web-sites|Website pages|Online websites|Webpages|Internet sites} {you should|you need to|you ought to|you must|it is best to|you’ll want to|it’s best to|make sure you|you might wa…

    under you will discover the backlink to some web-sites that we feel you might want to visit…

  44. {Read|Study|Examine|Go through|Browse|Read through|Learn|Check out|Look over|Look at} was {interesting|fascinating|intriguing|exciting|appealing|engaging|attention-grabbing|unique|remarkable|captivating|useful}, {stay|remain|keep|continue to be} {in …

    please check out the websites we adopt, which includes this a single, due to the fact it represents our picks with the web…

  45. level.if you’ve never heard this before then…

    probably the best piece advice for your blog i can give you is that your content is king. ok then, what kind of content you may be wondering, and what is considered to be good content?we all have our own unique…

  46. {Sites|Websites|Web sites|Internet sites|Web-sites|Web pages|Online websites|Online sites|Webpages|Internet websites} we Like…

    Every last once in a even though we decide upon blogs that we look over. Listed beneath will be the hottest websites that we prefer…

Comments are closed.