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.

55 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. 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
  26. 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
  27. 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
  28. 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
  29. "The practical insights you provide are invaluable for anyone interested in topic.
    Digital Marketing Courses in Hamburg

    ReplyDelete
  30. 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
  31. The blog post provides clear and concise explanation on TabHost and TabWidget thanks for valuable information .
    data analyst courses in limerick

    ReplyDelete
  32. Android UI Tab View provides a convenient way to organize content into multiple tabs, improving user navigation within an app. Using components like `TabLayout` with `ViewPager2`, developers can create swipeable tabs, offering a smooth, dynamic experience while displaying different views or fragments in a compact space.
    Data science courses in Gurgaon

    ReplyDelete
  33. Great post on Android UI Tab View! It's always exciting to explore how tabs can enhance navigation and user experience. Keep up the good work—your detailed breakdowns are super helpful for developers!
    Data Science Courses in Singapore

    ReplyDelete

  34. Your experience with setting up the Android UI Tab View is a valuable insight for those venturing into Android app development. Tackling null pointer exceptions can be quite challenging, especially when dealing with UI components like TabHost and TabWidget. Your solution of defining the tabs programmatically rather than in XML is a practical approach that many developers can benefit from. Additionally, ensuring that activities are correctly referenced in the AndroidManifest.xml is crucial for smooth functionality.

    For anyone looking to enhance their programming skills further, I recommend exploring the data analytics courses in Ghana offered by IIM Skills. These courses can provide a solid foundation in data management and analysis, which can be beneficial for app developers who want to integrate analytics into their applications. Data Analytics Courses in Ghana






    ReplyDelete
  35. This topic is so relevant! I’d be interested to see a follow-up discussing the role of video content in digital marketing. It seems to be gaining a lot of traction!

    Data science courses in Gujarat

    ReplyDelete
  36. This blog post on implementing Android UI with TabHost is a fantastic resource for developers looking to enhance their applications. The clear explanations and step-by-step instructions make it easy to follow along, even for beginners. The examples provided are particularly helpful in understanding how to utilize TabHost effectively. Overall, a well-written and informative guide that simplifies the complexities of Android UI design. Great job!

    data analytics courses in dubai

    ReplyDelete
  37. Your knowledge about the topic is quite strong. The simple steps makes it easy to follow. Well written and interesting to read. I hope it will be useful to many developers. Thanks for sharing such an informative article.
    Data science courses in Kochi

    ReplyDelete
  38. Fantastic I like the way you explained the code and the code is not very complex very well researched and nicely explained.
    Online Data Science Course

    ReplyDelete
  39. Super refreshing article with good content. I understood it very easily.Looking for more such post
    Online Data Science Course
    .

    ReplyDelete
  40. "I just read about the Data Science Course in Dadar, and it sounds promising!
    The blend of theory and practical learning is exactly what I need.
    I’m particularly interested in the networking opportunities it offers.
    It’s encouraging to see such robust programs available locally.
    I’ll be exploring this option further!"

    ReplyDelete
  41. The Android UI Tab View is a navigation component that enables users to switch between different sections of an app's interface quickly. Tabs are typically displayed at the top of the screen, providing an organized, intuitive way to explore content. With customizable icons and labels, Tab View offers flexibility for developers to design tabs that enhance user experience. It is ideal for apps that need to present multiple screens or functions efficiently.

    Data science Courses in Germany







    ReplyDelete
  42. Great blog. Thank you for providing a clear and detailed guide . This blog is very informational, keep sharing.

    technical writing course

    ReplyDelete
  43. Very informative post. Thanks for sharing your knowledge with us!
    GST Course

    ReplyDelete
  44. In this blog post, the author shares their experience building an Android app with a TabView layout, specifically addressing the challenges of handling null pointer exceptions and visualizing the Tab content. After running into issues with XML layout for the TabHost and TabWidget, the author switches to defining the tabs programmatically in Java, which resolves the error. This post offers a helpful approach for developers encountering similar issues when working with TabViews in Android. Investment Banking Course

    ReplyDelete
  45. This is a fantastic article on Experience - Android UI Tab View! I really appreciate how you broke down the subject into easy-to-understand steps. Your examples were very helpful, and I learned a lot. Keep up the great work, and I’m excited to read more!
    Data Science Courses in Russia

    ReplyDelete
  46. The tutorial on "Experience - Android UI Tab View" offers a practical guide to implementing tabbed navigation in Android apps, enhancing user experience with an intuitive and organized interface.
    Data Science Courses in Uzbekistan

    ReplyDelete