How to add multiple views to the detail fragment without using xml? - android

In an Activity I have two fragments A and B. A is a ListFragment and B extends Fragment. I was successful in establishing a communication between the two. When you select an Item in Fragment A the Fragment B is supposed to show a TextView, and a couple of button. So I put the views in a ViewGroup and tried to pass it to Fragment B. When debugged, it executed the right lines, but the views never appear in Fragment B. I have no idea how to approach this problem.
Now, given that I am not allowed to use xml to create views, how can I accomplish this?
All the examples, and questions in stackoverflow are either about just one view or views that have been created with xml. I dont know how inflater would be of any help to me.

In the onCreateView function of your Fragment B , you can create a view group at run time that can be returned .

#umesh I tried, it didn't work for me. However, after hours of browsing on the web I learnt that I can use LinearLayout. add the views to it and return the layout. That worked! Thanks though.

Related

Dynamically add and remove one instances of on fragment to view pager

I am trying to figure out how I can implement the following scene. I have an activity where the user needs to enter some information through edit texts, like names start times etc.. The user has the opportunity to add more than one information block. By hitting the add button as you can see in the drawing below. I thought about implementing it wit a ViewPager and a fragment. As I need only the same fragments multiple times I don't know how to dynamically add fragments to the Pager and how to remove them dynamically. If anybody knows a solution or an other approach that would be great. Thanks in advance.

How to get the parameters(like Width and Height) of a view of an fragment to a Activity?

I'm beginner to android and currently I'm working on a small project.
In my project I have a fragment with text and imageview, and in my Main activity I have a button and imageview.
When I press the button in my activity class it opens up the fragment, but I want to animate(Move) the the imageview in main activity to the position of the imageview present in the fragment.
Is there any simple way of getting the position of the views present in the fragments to activity class?
I'm stuck in this situation from few hours. Please help me.
What you are attempting is difficult because that is not how you are supposed to do it. Fragments are supposed to be self contained units, completely modular and interchangeable. On the other hand Activities are just supposed to be empty containers for Fragments. All the logic and UI has to be contained in the Fragments them selves and the Activities are supposed to be used to arrange and display those Fragments. Nothing from outside a Fragment should have anything to do with something inside the Fragment. So if you restructure your app with that in mind you will find that everything will be MUCH simpler.
A few pointers:
Move all of the UI to the Fragment
It is completely fine to perform FragmentTransactions from inside a Fragment. You don't have to take a detour through the Activity.
Try to understand the difference between Fragments and Activities and don't let yourself be mislead by tutorials which don't adhere to this separation. Most Android tutorials on the internet are outdated and wrong. Refer to the official tutorials here.

Handling fragment transaction in android.

Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.

Working with two fragments

There are two fragments. However it is slightly different from the tutorials case when there is one ListFragment and one DetailFrament, because I have two ListFragments. Now the problems is when I use this in one fragment :
getListView().addFooter(someView);
It would automatically assign this footer (or header) to another fragment as well, because getListView() method "Gets the activity's list view widget" and my two ListFragments are both in the main activity.
Any ideas how to get around it???
Considered having a reference to you listview by an id in the xml? Then you could find the listview in oncreate and do something like following:
myListView.addFooter(someView);
Uhu solved.
The problem was that one of my fragments was actually called "ListFragment" and at some point my fragment and the actual class ListFragment were confused and it caused a problem.
I took this idea from this tutorial but it seems that that guy has already tweaked it for the better. Anyway I shall punch him in the face on the first occasion.

Maintaining the View of the Layout when user gets back to the View from another view

I am working on an application where I am using the TabHost and I have four Tabs. Each of the tabs are given in a class MyTabHost that extends TabActivity.
Again I have one class that extends ActivityGroup and I have given all the tab classes in that class.
Now the problem is that when I move to another activity from one activity and when I come back to the previous activity, the position of the view gets refreshed and I don't want that. I want to maintain the view when I get back to the previously left activity on demand.
Is there any solution to anybody?
Thanks,
david
I am not exactly sure if I understand you correctly and without source it is even harder.
Having said this, In Zwitscher I am also using tabs and switching between them does not change the content. Have a look at this code snippet:
https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/TabWidget.java#L82
Perhaps this helps finding your issue.

Categories

Resources