How to create DialogFragment layout programmatically - java

I need to programmatically create a DialogFragment layout, but I don't know how to proceed. I cannot use xml layout because the DialogFragment will be a part of closed-source JAR file.
Normal Dialogs accept an Activity in their constructor, so it is then possible to instantiate a new Layout like this: layout = new LinearLayout(this);. DialogFragments, on the other hand, do not usually take the Activity as a parameter, so I don't know how to perform this first step.
How should I create the layout?
Is it OK to ask for an Activity in the constructor?
Is creating a layout of DialogFragment any different from creating a layout of normal Dialog?
I will be grateful for any other advice regarding manual DialogFragment design.

How should I create the layout?
How ever you want it to look. If you can't/don't want to use a layout file that you inflate and return from the onCreateView method you'll have to build the entire view hierarchy of your new dialog in code.
Is it OK to ask for an Activity in the constructor?
There is no need for this, the Fragment will get a reference to an Activity, you'll have a reference to that context by using getActivity().
Is creating a layout of DialogFragment any different from creating a
layout of normal Dialog? I will be grateful for any other advice
regarding manual DialogFragment design.
No, it's not different. The documentation for the DialogFragment has a great example on how to build a custom DialogFragment, you should check it out.

Related

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html

Difference between creating a class and extending fragment and just creating a fragment

I am relatively new at Android. I am taking a program on college and I am on my third semester. When we first started looking at fragments in the course, we always created them by right clicking the java folder and directly making a blank fragment. this would create the java file and the xml layout. then we would with help of the fragment manager, replace the content of the activity with the new fragment.
I thought this was the only way of doing it so far, but this week I've started to take a look at the developers course android offers on their website, and looking at the Creating a Fragment section, I learned that you can also just create a class and make it extend Fragment. Then, add an actual xml fragment tag to your layout and link your fragment to this xml.
My concerns about this are:
1) Is both ways doing the same process?
2) Is one of them better than the other?
3) Is there a moment when I'll prefer doing one or the other?
There is no difference. Directly using the "Create a fragment" option is just a convenience option that does the following:
Create a new class that extends from Fragment
Create a new layout for your new custom fragment class
Automatically set this layout as the contentView of your fragment (in the onCreate method)
As a bonus, add some sample code for you to get started
You could do all these steps manually as well.

What is the difference between layout and activity?

I have some problems with the layout and activity and I don't know are they different,are they related?
I think the layout is a place we can add or remove our views and activity is just a place that shows any thing in our layout, is this true?
An activity:
is an instance of Activity, a class in the Android SDK. An activity is responsible for
managing user interaction with a screen of information.
You write subclasses of Activity to implement the functionality that your app requires. A simple application may need only one subclass; a complex application can have many.
A layout:
defines a set of user interface objects and their position on the screen. A layout is made up of definitions written in XML. Each definition is used to create an object that appears on screen, like a button or some text.
A layout deals with the user interface. Its where you set all your views that will be visible on the user interface.
The code behind (.java) sets the layout you created as the content view and manipulates the behavior of the views you have set. For example, sets the text for a text view.
The activity then is the whole thing, the layout and the code behind.
An activity is the java code which attaches actions and puts content to/in a layout. For this the Activity loads the layout.
Briefly,
Activity is the java part of your projects. The program and any kind of algorithms are implemented here. Also layout views come to life in an activity.
Layout is where you organize the views in your page. But without activity, they have no meaning. Because in activity, you have to get these views and use them programmaticaly.
All together, you load views from layout to activity and in activies you implement your whole program.
A layout defines all the appearance of an app and this is of no use without a java program which helps in real functioning of that visual display.
Thus we define what an app does by writing its java code and a special java class called activity decides which layout to use at a particular instant and tells the app how to respond to the user.

Creating a Static Menu Bar

What I've been trying to do is create a menu that stays "static" no matter what activity or layout is being showed. Is there any way to do this, or anything similar to this??
Thanks!
Yes,
1- Create a custom Activity (extend it from Activity)
2- Write the code for menu creation in that custom Activty.
3- Now extend all your Activities from this custom Activity.

Best practise for displaying multiple controls which require an extended class?

If I want to display a MapView inside my Activity I would have to extend my Class from MapActivity. If I want to display a Tabbed interface I would have to extend my Class from TabActivity. Similar is the case with some other controls which require user class to extend from a specific class.
Let's say inside my Activity I want to display both a MapView for displaying Google Map and a TabView to display some other data. I can't do it directly because Java doesn't support multiple inheritance. My question is how can I achieve this scenario? How can I display multiple controls inside my activity where each require your class to extend from a specific class. Is it possible first of all? If yes, what are the best practises to achieve this scenario?
Update I want to achieve this
I am using Map and Tab for sake of an example. I would like to know how you can tackle this scenario in general?
In this case it's simple: You can use the MapActivity within the TabActivity (it's designed to manage activities as tabs).
As general approach I always prefer to use views and nest them in an activity. I never use such things like ListActivity. They should make things easier but often look like a bad design decision to me. I never faced the fact that I had to combine two activities (expect TabActivity).
You can take a look at this question. It seems that activities never meant to be used that way. I think the situation which you describe is the reason why fragments where introduced.
You could build it via object composition. Initially I am not sure how to get the Activity started and add it to the layout, but then I found out about LocalActivityManager which allow you to embed other Activity as your view. Note that this class is deprecated since API Level 11. In any case here are the steps to embed other Activity that require extension as a View:
Create a LocalActivityManager to enable creation of Activity within Activity
Start the activity that you want to embed and get the View via getDecorView()
Add the View in your layout
The following is my test code that I tried within my Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create local activity manager so that I could start my activity
LocalActivityManager localActivityManager = new LocalActivityManager(this, true);
// dispatch the onCreate from this manager
localActivityManager.dispatchCreate(savedInstanceState);
// layout to hold the activity, optionally this could be set through XML file
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
this.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
// start the activity which is in this example is an extension of a TabActivity
Intent tabIntent = new Intent(this, DummyTabActivity.class);
Window tabWindow = localActivityManager.startActivity("tabView", tabIntent);
View tabView = tabWindow.getDecorView();
// start the activity that extends MapActivity
Intent mapIntent = new Intent(this, DummyMapView.class);
Window mapViewWindow = localActivityManager.startActivity("mapView", mapIntent);
View mapView = mapViewWindow.getDecorView();
// dispatch resume to the Activities
localActivityManager.dispatchResume();
// add to the tabView, optionally you could use other layout as well
layout.addView(tabView);
// add to the mapView, optionally you could use other layout as well
layout.addView(mapView);
}
My limited experiments show that object composition via the above method will achieve what you are trying to do. Having said that, I am not sure how common this approach is. Without your question, I wouldn't probably look for the above method, but your use case is interesting and might be applicable for future use. I will look into Fragment and see if I could do the same with it and update my answer if it is applicable.

Categories