Search This Blog

Monday, August 2, 2010

Experience - Android UI Tab View

I have started an Android app at work. I was diving in head first and started with hello views tutorial. The tab view was interesting, but after copy-n-pasting some code and running the app I was less than satisfied with the results. First I was unable to view the XML layout of the Tabs and second there was a null pointer exception. I have seen a couple other notes and questions about this type of problem.

TabWidget causing null pointer
Why do I get a null pointer exception from TabWidget?

This article explains some of the things I did to improve on this issue.

Using XML to layout and define the TabHost and TabWidget I found no way around the null pointer exception. I was able to instead define these in code. The activity that runs on startup is called SampleTab. Below is its source.

package app.tabsample;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent;

public class TabSample extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TabHost tabHost = getTabHost();

tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("MSG")
.setContent(new Intent(this, SelectActivity.class)));

tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("ARROW")
.setContent(new Intent(this, ArrowsActivity.class)));

tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("OPT")
.setContent(new Intent(this, OptionsActivity.class)));

tabHost.addTab(tabHost.newTabSpec("tab4")
.setIndicator("EDIT")
.setContent(new Intent(this, EditActivity.class)));

tabHost.setCurrentTab(1);
}
}


For the tab indicator, your choices are: 1) set a label 2) set a label and an icon. Note: api level 4 also adds 3) set a view. For the tab content, your choices are: 1) the id of a View 2) a TabContentFactory that creates the View content. 3) an Intent that launches an android.app.Activity. For simplicity I chose to set a label for the tab indicator and an Intent that launches an activity for the tab content. Note that the Activities need to be listed in the AndroidManifest.xml. Now looking at the first tab which calls the SelectActivity.

package app.tabsample;

import android.app.Activity;
import android.os.Bundle;

/**
* @author Administrator
*
*/
public class SelectActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabstest1);
}
}


This activity simply sets its content to the XML file tabstest1.xml. There we have it a simple and easy to use way to create a TabHost and TabWidget without having a null pointer exception and still being able to see the XML tab content graphically in Eclipse. Just for reference my tabstest1.xml contains the following.

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<textview android:layout_width="fill_parent">
android:layout_height="wrap_content" android:text="@string/hello" />
<spinner android:id="@+id/Spinner01" android:layout_width="wrap_content">
android:layout_height="wrap_content" android:entries="@array/Spinner_Content">
</spinner>
</textview>
</linearlayout>


Below is a screen shot of my tabstest1 with the layout tab.

43 comments:

  1. And as I saw in your next posts, you weren't really happy with native tab functionality in android :)
    so am I

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete



  3. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

    angularjs Training in online

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    ReplyDelete
  4. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
    python training in velachery | python training institute in chennai



    ReplyDelete
  5. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    blueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training

    ReplyDelete
  6. Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
    Java training in Annanagar | Java training in Chennai

    Java training in Chennai | Java training in Electronic city

    ReplyDelete
  7. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    Data Science training in Chennai | Data science training in bangalore

    Data science training in pune | Data science online training

    Data Science Interview questions and answers


    ReplyDelete
  8. azure have good scope in it azure training is to improve the skills on azure technology

    ReplyDelete
  9. 1croreprojects offer android training in chennai Bestandroid training in chennai – DLK, which we are guaranteeing your progress in your SIMULINK / CARSIM Practice, to give you the best practical android training courses for specialized areas.

    ReplyDelete
  10. php internship training The BCA, MCA, MBA, BBA, B. Com and M.com, B.Sc, M.Sc Understudies who need to partake in a free PHP temporary job in Chennai will utilize ongoing activities and business capacities for a long range of time.

    ReplyDelete
  11. ME mini college engineering project centers in chennai We offer Mini tasks in Chennai and Main Projects in Chennai for the understudies who are seeking after B.E, B.Tech, M.E, M.Tech, BCA, MCA, B.Sc., M.Sc., BBA, MBA, and Diploma in all Streams.

    ReplyDelete
  12. free ipt training in chennai to final year understudies preparing in Software advancement structuring and creating and Website Development utilizing HTML, PHP, Java, Dot Net and so on. We will prepare your abilities in a superior manner through our ipt preparing.

    ReplyDelete
  13. thanks for your valuable information it is helpful for me, i like to share you something
    http://rexapparels.com/women-half-sleeve-t-shirt-manufacturer-in-tirupur-india/women
    http://rexapparels.com/women-long-sleeve-t-shirt-manufacturer-in-tirupur-india/women
    http://rexapparels.com/women-v-neck-t-shirt-manufacturer-in-tirupur-india/women
    http://rexapparels.com/women-polo-t-shirt-manufacturer-in-tirupur-india/women
    http://rexapparels.com/leggings-manufacturer-in-tirupur-india/

    ReplyDelete

  14. Excellent! I love to post a comment that "The content of your post is awesome" Great work!

    top data analytics courses in mumbai

    ReplyDelete
  15. Attend The Business Analytics Courses From ExcelR. Practical Business Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
    Business Analytics Courses
    Data Science Interview Questions

    ReplyDelete
  16. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    ExcelR data analytics courses

    ReplyDelete
  17. Its as if you had a great grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from more than one angle.

    data science course

    ReplyDelete
  18. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. lovely page.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  19. Really awesome blog!!! I finally found a great post here.I really enjoyed reading this article. Nice article on data science . Thanks for sharing your innovative ideas to our vision. Your writing style is simply awesome with useful information. Very informative, Excellent work! I will get back here.Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  20. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai


    ReplyDelete
  21. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Also refer to Business Analytics Course

    ReplyDelete
  22. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest posts. I will visit your blog regularly for Some latest posts.
    aws online training in hyderabad

    ReplyDelete
  23. It is extremely nice to see the greatest details presented in an easy and understanding manner.
    data science training institute in hyderabad

    ReplyDelete
  24. I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.
    data scientist course in hyderabad

    ReplyDelete
  25. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post. data science training in surat

    ReplyDelete
  26. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.. data analytics course in mysore

    ReplyDelete
  27. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

    ReplyDelete
  28. I would also motivate just about every person to save this web page for any favorite assistance to assist posted the appearance. data science training in surat

    ReplyDelete
  29. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you! data science course in kanpur

    ReplyDelete
  30. Manage the facts like sale, credits, debits. This is easy software which is perfect for all users involved in improved business organization, improved secretarial. Tally ERP 9 With Crack Full Version Zip Download

    ReplyDelete
  31. Reading about your experience with the Android UI tab view is intriguing! Overcoming obstacles and sharing your solution might assist those dealing with comparable problems. I appreciate you providing code examples and procedure details. Well done!
    Data Analytics Courses in India

    ReplyDelete
  32. It's interesting to read about your experiences with the Android UI tab view. Overcoming challenges and sharing your answer may help those facing similar issues. Thank you for the code snippets and procedural information.
    Data Analytics Courses in Agra

    ReplyDelete
  33. "The practical insights you provide are invaluable for anyone interested in topic.
    Digital Marketing Courses in Hamburg

    ReplyDelete
  34. I found the blog post very informative and detailed explanation on using XML and code for defining TabHost and TabWidget in Android applications .
    Digital Marketing Courses in Italy

    ReplyDelete
  35. The blog post provides clear and concise explanation on TabHost and TabWidget thanks for valuable information .
    data analyst courses in limerick

    ReplyDelete