Android make activity with tabs and dynamically add views in each fragment - android

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!

Related

TabHost inside a fragment of an activity

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

ActionBar on the TOP and BOTTOM on Android App

I'm developing an Android app and I want to have 2 ActionBar (one on the top and another on the bottom) like this: http://developer.android.com/design/media/action_bar_pattern_considerations.png
I'm using Actionbar Sherlock and for now I created only the top ActionBar. I serched in the web, but I didn't find a solution yet, only some piece of code.
Can anyone help?
Take a look at the following links:
http://developer.android.com/guide/topics/ui/actionbar.html
How can I force the Action Bar to be at the bottom in ICS?
Changing position of Android Action Bar
try this...
public class MainActivity extends SherlockActivity {
private View contentView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(contentView);
LinearLayout layout = (LinearLayout) contentView.getParent().getParent();
View view = layout.getChildAt(0);
layout.removeViewAt(0);
layout.addView(view);
}

Assign Content to Tabs in a Fragment - Android

Just created a new project in the newest ADT release in Eclipse and found that it will setup up certain environments for you to get things started. I choose Tabs + Swipe.
It has this code I have question on:
public static class DummyFragment extends Fragment {
public DummyFragment () {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
Both tabs refer to this same fragment. All it does is switch the content on the TextView that has the tab position number on it (1,2, or 3).
The more advanced question first: I want two different Fragments that the tab switches to. In the example code, it points to the same fragment. Where does this change take place? and can I see brief code example?
Easier question: I have two pre-defined XML layouts I'd like to set each tab (or Fragment) with. Do I do this in the actual Fragment? And if so, where? setContentView does't seem to be working in the onCreateView method?
Not really sure what this question is asking exactly, but if I understand correctly the TabHost (or whatever you are using to manage the Fragment tabs) is instantiating multiple instances of the DummyFragment and then attaching each to the screen when a tab is clicked. This is all done behind the scenes... all you need to worry about is implementing the Fragment and telling the TabHost when it should be instantiated/displayed.
Fragments don't have a setContentView method. You should inflate your Fragment's layout from xml in onCreateView. For instance with,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}

Fragments inside a Fragment [duplicate]

This question already has answers here:
Fragment Inside Fragment
(13 answers)
Closed 9 years ago.
Is there any help, resources, example or a tutorial available for putting fragments inside a fragment?
I have two tabs put up using Action Bar Navigation tabs, which essentially are fragments. What I want to do is, put in a ListFragment and a DialogFragment (to display a view with details) in one of those Action Bar Navigation tabs fragment.
Possible?? Please help. Thanks
Here's what I have done till now:
Followed Android's article on implementing navigation tabs on Action Bar and implemented a two tab action bar, this is running fine and I am able to show two different layouts.
In one of these tabs, I want to show a Fragmented view, for this I have created a FragmentList class with custom ArrayAdapter and data items, the data detail layout and class.
I am now stuck on how to display the fragments inside the tab. Following is the code which shows the content of first tab, how can I modify it to initialize the list fragment properly?
.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View viewer = (View) inflater.inflate(R.layout.doodle_list, container,false);
return viewer;
}
Is there any help, resources, example or a tutorial available for putting fragments inside a fragment?
Fragments inside of fragments is not supported.
Fragments inside of fragments is supported on Android 4.2+ and via the Android Support package's backport of fragments. I'd still look to avoid it where possible.
I have two tabs put up using Action Bar Navigation tabs, which essentially are fragments.
You chose to do that. There is nothing about action bar tabs that requires the use of fragments.
I solved this problem. You can use Support library and ViewPager. If you don't need swiping by gesture you can disable swiping. So here is some code to improve my solution:
public class TestFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag, container, false);
final ArrayList<Fragment> list = new ArrayList<Fragment>();
list.add(new TrFrag());
list.add(new TrFrag());
list.add(new TrFrag());
ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
#Override
public Fragment getItem(int i) {
return list.get(i);
}
#Override
public int getCount() {
return list.size();
}
});
return v;
}
}
P.S.It is ugly code for test, but it improves that it is possible.

Creating a tabbed UI without using Layout

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

Categories

Resources