I made a program that uses FragmentTabs for implementing ActionBar Tabs.
I was wondering how it is possible to add a widget to a inflated Fragments in Tabs. It's more complicated than normal because the fragment is returning only an onCreateView to the TabListener, so how would I add in functioning widgets? I don't need specific code, just the logic please!
Here is a sample Fragment being inflated when a Tab is clicked (inside of which I need widgets):
public class IdFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.id_layout, container, false);
}
}
Related
I have decided my layout on the basis of JSON response and make a layout at run time without any xml . I just take a Activity and call a api that give me json array on the basis of which i make my layout. I have no problem with that but now i want to attach a navigation drawer with this activity . I have used navigation drawer in my previous Activity by making them fragment but in this case i don't know how to convert my activity to fragment because in navigation drawer we need fragment not activity.
i just do not know how to convert this Dynamic ui to fragment
View rootView = inflater.inflate(R.layout.`dashboard`,container, false);
what should i write in place of dashboard because i have no xml for that particular activity ..
I did it this way:
public class BaseFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
View view = //view from JSON
scrollView.addView(view);
return scrollView;
}
}
in navigation drawer handler
getFragmentManager().beginTransaction().replace(R.id.content_frame, new BaseFragment()).commit();
I have a FragmentActivity which sets a main layout with 3 tabs. Each tab has it's own fragment which inflates is own layout into main layout.
For example, one of the three Fragments:
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.fragment1, container, false);
}
}
When I add all the fragments and connect them with tabs everything works. When I click on tab1, layout of the Fragment1 shows, when I click on Tab2, layout of the Fragment2 shows.
The problem is that when I want to change something in that inflated layout (for example execute setText to textView in fragment1 layout), I receive a NullPointerException in that line and application stops.
This doesn't work:
TextView test = (TextView) findViewById(R.id.fragmentOneText1);
test.setText("Test text!");
How to set data to inflated layout?
You have to inflate your fragment specific layout first by using LayoutInflater and map the control in your Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.R.layout.fragment1, container, false);
TextView text = (TextView) fragmentView.findViewById(R.id.fragmentOneText1);
mTextNoResult=(TextView)fragmentView.findViewById(R.id.text_noresult);
return fragmentView;
}
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);
}
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.
Looking at the Android tutorial on:
http://developer.android.com/guide/topics/fundamentals/fragments.html
... it seems fragments have their layouts defined programmatically. Is there a way to use the usual XML files instead?
Thank you
You just missed it, this is the first code snippet in the fragment documentation:
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
It uses a (supplied) LayoutInflater to inflate a layout, in this case named example_fragment.xml (inflate means parse XML and generate a layout structure out of it). So yes, certainly possible.
Basically all you have to do is return your fragment layout from onCreateView(). How you generate it inside is up to you, and since you get an inflater supplied, it's intended to use XML too.