where to get R.id.container from in Android? - java

I am new to android. I have created a new android project selecting tabs as a template Eclipse implements tabs and pager itself. Each tab view has a fragment. Then I implemented ListView in a fragment.
Now when a list item is tapped, new view (fragment) should be open. To open a new fragment view on clicking list item, I need to give R.id.fragment_container to it. this id tells us on which container fragment view will be shown.
My Question is where should I get R.id.container as there is no container is defined with this id in the Main Activity. I have tried many things came in google like to add a layout (Relative/Linear) as well but it crashes and also I have tried to give pager id as well but nothing happens.
Think i have some understanding problem with fragments. Any explanation/help specific to my case would be highly appreciated. Thanks

R.id.fragment_container or R.id.container are the IDs of a View in your MainActivity's layout file. Open that activity_main.xml (or whatever it is called) and add a container to hold your fragment.
You'll need to add the android:id="#+id/fragment_container" line to the View/Layout you choose.

add andriod:id="#+id/id_name_". in your fragment layout and call IT wherever u want

Related

How to add / remove tabs from inside a fragment (AppCompat)

I am having a really tough time with one small issue in my AppCompat activity with tabs. I am using the Android Design Support Library, and have implemented a tabbed application with fragments.
Now, I have no problem creating tabs & fragments in the Activity's onCreate() method, but I cannot for the life of me find if it is possible to add tabs programmatically from within a fragment.
For reference, all of my tabs use the same fragment (OneFragment.java). I have tried using FragmentManager / FragmentTransaction, but although this creates a fragment (I think!), it does not make a tab.
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI. Any help would be much appreciated!
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI
If you managed to add a tab to the FragmentPagerAdapter but the UI didn't change then you probably forgot to call the notifyDataSetChanged() method on it.

Is it possible to put a FragmentActivity inside a fragment container?

I have a Fragment activity which hosts a fragment list. The fragment activity also has a couple of text views which summarise the data in the fragment list.
FragmentActivity = Layout that contains a fragment container, text views and a button.
ListFragment = goes inside of the FragmentActivity's fragment container and is a list of data
This works fine as an activity, but now I'm trying to put the whole lot into another fragment container to display it in an action bar tab on another activity. I can only get it to work with the ListFragment (obviously this is because the FragmentActivity can't go inside of a fragment container) but this means that the summary text views and the buttons are missing and I really need them there too.
I wondered if anyone had suggestions of the best way to implement this? Some thoughts of solutions I've had:
Change the Layout of the activity holding the tabs if this certain tab is clicked
Programmatically add the text views and button to the fragment container if this tab is clicked
Add another fragment to the fragment activity, below the list fragment
Either way I'm stumped so any help is really appreciated.
Create a Fragment that resembles the layout of you FragmentActivity and add it instead. you can't add the FragmentActivity as a Fragment simply because it's not a Fragment but an Activity that was added a Fragments support for older versions.
So instead of adding you button and TextViews directly to the FragmentActivity Layout. Create a Fragment with the same components and add it to the Activity layout.
That way could could reuse this set of components in another location in your application if you needed to, using the same Fragment.

Display the same Android fragment twice and use call getById

So I have 2 fragments visible if the user uses a device with a big enough screen (like a Samsung Galaxy Tab).
Right now I am displaying a fragment list twice (same fragment twice). Inside the code of the fragment I use this to hide a progressbar.
ProgressBar pb = (ProgressBar)getActivity().findViewById(R.id.progress);
pb.setVisibility(View.GONE);
Problem is that it only works on one of the fragments. Both have the same id since its the same fragment?
Should I create 2 identical fragments or is it possible to find the "correct" progress-bar in the correct fragment?
In stead of finding the view (the ProgressBar) in the activity's view hierarchy, find it in the fragment's view hierarchy. So inside the fragment, do the following:
ProgressBar pb = (ProgressBar) getView().findViewById(R.id.progress);
Generally, you don't want to do lookups in the parent's view hierarchy, so above basically applies to all views in the fragment's layout.

How to reset a fragment's view?

If I have a fragment where I dynamically set a view with onCreateView(), how would I go about calling it again?
I want to implement some kind of "refresh" where the view changes based on the JSON response. I tried making a new function that does midnightSV.removeAllViews(), but how can I call onCreateView() again?
You can't without detaching and re-attaching the fragment.
If you just want to update the data in the view, you can find those views and refresh them from the existing fragment.
If you really want multiple sets of unique layouts, you can look into using a ViewFlipper for your fragment layout and then call setDisplayedChild() to switch to a specific view.

Is it possible to have a ListView in a Fragment - or a ListFragment in a Fragment?

In my application I want a panel that contains some text information then below it a list of menu items. What is the best way to do this?
Is it possible to have a ListView within the fragment along with other TextView's etc.
If not is it possible to have a ListFragment within a Fragment? That is what I am attempting at the moment but when I try and run it I get an error:
java.lang.IllegalStateException: Fragment LeftMenuFragment{4089c090} not attached to Activity
Yes you can have listview in a fragment. Like ListActivty there is Listfragment which you can use for using ListView in your application.
See this market tutorial for help
The answer is 'Yes' we can have a listview in fragment. The error is self explanatory. You hav not added it to the activity or any layout.

Categories

Resources