As per the android developer docs for creating tab UI you need to have a TabHost and TabWidget and the TabHost must be the root node for the layout.
All perfect, I tried the example and everything's fine.
Just while looking at the API Samples of tabs, I came across tabs1.java (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs1.html) which was not using any tab elements in the layout.
Here is the sample working code that creates a tab, without using any layout at all.
public class HelloAndroid extends TabActivity implements TabHost.TabContentFactory {
/** 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("tab1").setContent(this));
}
public View createTabContent(String tag) {
TextView text = new TextView(this);
text.setText("tab1");
return text;
}
}
Can anyone explain that how this is working ? And how this is different that using the Layout based approach as explained in the tutorial.
Thanks.
This is because TabActivity programatically creates a TabHost layout.
You can check http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/app/TabActivity.java&q=TabActivity&sa=N&cd=1&ct=rc
Related
I want to create a layout in which upper half is just some area with normal views and lower half is a tab layout .
I've seen some examples but they're all how to create tabs at activity level i.e by extending TabHostActivity which covers all the activity area.
So i decided to create 2 fragments in the activity ,in which lower fragment will have the tablayout.
But the problem is i cant make this fragment class extend Fragment as well as TabHostActivity...
So any help how could i implement this ?
Here's the lower fragment's code -
public class PFrag extends Fragment {
View mRoot;
TabHost tabHost;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.payfrag, container, false);
try {
Resources resources = getResources();
tabHost = (TabHost)mRoot.findViewById(R.id.tabHost);
Intent netbintent = new Intent(getActivity().getApplicationContext(), NB.class);
TabHost.TabSpec tabSpecNB = tabHost.newTabSpec("NB");
tabSpecNB.setIndicator("", resources.getDrawable(R.drawable.netb));
tabSpecNB.setContent(netbintent);
Intent ccardintent = new Intent(getActivity().getApplicationContext(), Cc.class);
TabHost.TabSpec tabSpecCc = tabHost.newTabSpec("CC");
tabSpecCc.setIndicator("", resources.getDrawable(R.drawable.cc));
tabSpecCc.setContent(ccardintent);
tabHost.addTab(tabSpecNB);
tabHost.addTab(tabSpecCc);
} catch(Exception e) {
AlertDialog.Builder ad = new AlertDialog.Builder(getActivity().getApplicationContext());
ad.setMessage(e.toString());
ad.show();
}
return mRoot;
}
}
Sounds like you need to use FragmentTabHost inside your "bottom" fragment.
Another link that could help you out here.
Also, it seems on only work in API 17 and above, so be sure to keep that in mind!
I will not recommend FragmentTabHost its somehow difficult to use and don't have swipe Left-Right. TabHostActivity is also deprecated, use SlidingTabLayout its open source and easily to use and update and have swipe.
SlidingTabLayout
I am trying to make an activity have swipable tabs, with each tab having a different fragment (I dont know yet how many fragments I am going to have, so assume they will be at least 5).
So I am having problems make the parent activity with the tabs (if I said that correctly) and then the fragments themselves have some dynamically added views (text views and one image view) which get their stuff from different async tasks, executed when the fragment is showed.
Don't know if I explained it correctly, but here is my code and I'll ask you please to add in it the needed stuff.
So here is the parrent activity which must host the tabs and the fragments:
public class SecondActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
and here is one of the fragments (the others are similar):
public class Fragment1 extends Fragment {
LinearLayout layout;
ImageView iv;
String anotherURL;
ArrayList<InfoStuff> ci;
public Fragment1() {
// Empty constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
layout = (LinearLayout) rootView.findViewById(R.id.layout);
iv = (ImageView) rootView.findViewById(R.id.ivPortrait);
Bundle b = this.getArguments();
ci = b.getParcelableArrayList("infoStuff");
regionUrl = b.getString("someURL");
createViews();
return rootView;
}
public void createViews() {
TextView tv;
tv = new TextView(v.getContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("le text");
layout.addView(tv);
tv = new TextView(v.getContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("some text");
layout.addView(tv);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(v.getApplicationContext()).build();
ImageLoader.getInstance().init(config);
String imgUrl = "someURL";
ImageLoader.getInstance().displayImage(imgUrl, iv);
}
}
I am also having troubles making the ImageView work, as it is from an additional library (forgot the name, heres the import though import com.nostra13.universalimageloader.core.ImageLoader;)
EDIT: Sorry, forgot to mention the tabs must be swipeable
The article Creating Navigation tabs using TabHost and Fragments in Android may help you.
In this article, we will develop an Android application containing navigation tabs. There are many ways by which we can add navigation tabs to an application.
Beginning with Android 3.0, the standard way to create navigation tab is using action bar. [...]
For Android 2.x and above, we can use Action bar Sherlock library to create navigation library. [...]
In this application, we are creating navigation tabs using TabHost and Fragments. [...]
This application is developed in Eclipse 3.7.2 with ADT plugin (20.0.2) and Android SDK ( R20.0.1 ) .
I just started doing something very similar to what you want to do. I followed this tutorial on Android Hive, which was very helpful in getting the tabs, swiping, and fragments down. (The fragments in this example are just textviews with different colors, but you can easily figure out where to add in your own views).
Hope this helps - it did for me!
actually I have a FragmentActivity with a Tabhost, some Fragment inside and you can slide between the different Fragment thanks to a ViewPager.
With this FragmentActivity, I would like to incorporate it into a other Tabhost, and so have the one below the other.
For the moment, I have this solution :
public class TabsViewPagerFragmentActivity extends ActivityGroup {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout
setContentView(R.layout.main);
TabHost mTabHost = (TabHost)findViewById(R.id.testtabhost1);
mTabHost.setup(getLocalActivityManager());
TabSpec ts = mTabHost.newTabSpec("tab_test1").setIndicator("TAB1");
ts.setContent(new Intent(this,PageGaucheFragment.class)); <--- PageGaucheFragment is the FragmentAtivity with the ViewPager that I would like to add into the TabHost
mTabHost.addTab(ts);
mTabHost.setCurrentTab(0);
}
It works, but ActivityGroup was deprecated :( And I don't find any other solution to solve this problem.How can I have 2 TabHost and that the second can slide between the different Fragment ?
Thanks for your answer, and sorry for my English ;)
Look here: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html
I've used it successfully.
Hi i have set up a view pager and fragments for each page and that's all working as it should however i can not get a preference screen to show on one of my pages no matter what i try.
My fragment that i'm trying to add a preference screen looks like this
public class Extras extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.extras, null, false);
return V;
}
}
So then i read the documentation on PreferenceFragments here
http://developer.android.com/reference/android/preference/PreferenceFragment.html
But when i add this in my Extras class as the above link shows
public static class Extras extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
I error out just extending PreferenceFragment errors in my view pager i read the below link though that PreferenceFragments aren't part of the compatiblity library which i believe is my issue. So is it possible to add a preference screen in one of my pages in my viewpager or is this something i'm going to have to do with a listview and basically make my own preference screen
Was PreferenceFragment intentionally excluded from the compatibility package?
Thank you for any help with this issue
Never got the answer i was looking for so i ended up going with a listview instead
I posted my solution here http://forum.xda-developers.com/showthread.php?t=1363906
I simply rebuilt the hidden Preference* class via Reflection.
I am making a chat application. Where i am using tabhost. I have a activity A to show the frndlist and activity B for chat window. Now when an entry in the frndlist clicked a tabwindow opens where i add tab dynamically using the intent of activity B. Now if there are more than one tab when i nevigate through those tab nothing get called(oncreate, onpause,onresume) and the containt remain same for all tabs. Only one instance of activity B is created.
Is there any idea to create chat application with tab(like yahoo mail chat).
thanks in advance
Please help
Rawcoder
Create this class too in your TabActivity
class PreExistingViewFactory implements TabContentFactory {
private final View preExisting;
protected PreExistingViewFactory(View view) {
preExisting = view;
}
public View createTabContent(String tag) {
return preExisting;
}
}
This works for me.