What is fragment_main? - java

I've recently setup eclipse and the android SDK on a new computer but I fear something has gone wrong when setting it up.
When I create a new android project with a blank activity two files I've never seen before appear:
fragment_main.xml
and
appcompat_v7_2
What are they and do I need to worry about them?

appcompat-v7 :
As stated in Android's Support Library Overview, it is considered good practice to include the support library by default because of the large diversity of devices and the fragmentation that exists between the different versions of Android (and thus, of the provided APIs)
Reference : Why does Eclipse automatically add appcompat v7 library support whenever I create a new project?
Fragment_main :
fragment_main.xml is the Layout for the fragment.
Refer this : Building a Dynamic UI with Fragments
hope this helps

The first, let read code in MainActivity!
You can see fragment_main is layout of one fragment in it.
when application running, fragment will be added to layout and show this layout.
Let read more about here for understanding about fragment. It is very important if you want to make android app.

Related

Why Android Studio is creating two fragments?

So here's the problem, when I create a new project using Basic Activity template it generates two fragments and every page or tutorial I've seen it's not generated. Is it something normal? How to I to avoid Android Studio creating this two fragments?
That started in version 3.6 of Android Studio. I believe they're trying to promote their Navigation Component, where the principle is to use one single Activity (host) and many Fragments (for all the other screens of the app).
As the other answers have suggested, you can either select "Empty Activity" instead or delete these fragments after they're created.
My suggestion would be to try and learn about the new Navigation Component, as it might become the new Standard in the near future.
I honestly don't know why Android Studio does that but if you want just one activity you can start with "Empty Activity" Or just "Add No Activity" option. And then add an activity with the App->New->Activity->Empty Activity.

Cloning an android activity dynamically?

I'm creating an application with a rather dynamic UI that allows users to add/change the positions of its views and layouts dynamically. If the user decides that current UI design satisfies him he would be able to save all views(buttons/labels/switches/etc.) of the activity into a list of "saved" activities. Is there a possible way to clone an activity or at least save the positions of all views and layouts? I saw that views have functions .getX() and .getY() but as far as I understood it is only within a layout.
Update
I found a solution that works. I created a simple Sqlite Database the stored the XY locations/names/text/id of all my views in to a table dynamically and then displayed them in a list of 'saved' configurations. The hard part is creating dynamically again all the views after you get the DB. If there is a request, I can also put some code up.
I'd create a base-activity, that has the functionality that both of your Activities need. This functionality would have to be slightly abstracted so that both activites could use this functionality by extending this BaseActivity and use it for their individual purposes.
If you post some code (the part of code both of your activities should have), I could update my answer to show you how that would look like in your case.
Further,
Step #1: Open the old project in Android Studio.
Step #2: Open the new project in Android Studio, choosing to open it in a new window (rather than the window you have from Step #1).
Step #3: Drag and drop the Java class files from the old project into the new project.
Step #4: Drag and drop the resources used by those Java classes from the old project into the new project.
Step #5: Find the elements from the manifest of the old project and copy those into the same basic location in the manifest of the new project.

Should I create a fragment for my app?

I'm a bit confused as to wether I should create a plain activity or a fragmentactivity. My app is displaying an expandableListView and a dialog. Thats it (pretty much).
I cannot find any instance that my app would be better if I used a fragmentActivtiy because first of all my app is not designed for tablets, and second I'm not adding or doing anything to my main activity during runtime to change the views.
My problem is that it seems it is a must to use fragments, because when I checked out Android dev site on dialogs, it only explains how to implement them using fragments. Are plain activities highly un-recommended or I just have to judge my app, and are all features such as dialogs available for plain activities? (And can someone please give me a link to a good source where I can learn about dialogs in a normal activity?)
http://developer.android.com/guide/topics/ui/dialogs.html
It says here that you should use a fragment but does that mean i have to go all out and make my main activity a fragment?
Here is some discussion about using Fragment or Activity for dialog: Show fragment as a dialog or as a usual activity
If you want to make your dialog as Activity, hope this tutorial helps:
How to create Dialog activity in Android?
A FragmentActivity is an Activity because it extends Activity See here. The question the mean to ask is really a question for your self, do you want your app to support API >= 11 (3.0 Honeycomb) or API >= 4?
In API 11 Android introduced the concept of Fragments to deal with creating apps particularly tablets. But to add support for all the devices still running < 11 they created the Support v4 library which is where FragmentActivity lives.
If you are not using Fragments at all then just use Activity because it's available at all API levels. (it just got added functionality in >=11). If you want to support >=4 and may use Fragments then use FragmentActivity and your covered. I noticed things in the v4 library have started to be deprecated (starting to be phased out by Google). Personally, I won't make a new app with API <14 and suggest you stay above 11.
As far as Dialogs go. You don't need to used a DialogFragment but same question above applies to API level. I typically just used Dialog to show mine even if I'm using Fragments.
Hopefully this clears some things up for you. Happy Codin'

Android studio appcombat actionbar setup

I've managed somehow to add the action bar when i extend ActionBarAtcitivy, but what if i want to call for example: ListActivity and still have the action bar?
I have a strange feeling that i missed something when i added the appcombat library, because in the preview design window actionbar is never displayed, though if i launch the app, the action bar is there(But only if i extended the ActionBarActivity).
How i installed the actionbar:
I downloaded the library and the repository.
I added the compile com.android.support:appcompat-v7:21.0.3 in build.gradle.
I have the android:minSdkVersion="8" android:targetSdkVersion="21" declared in my build.gradle script.
Ive done these parts, as told in the android studio documentation.
Another interesting thing, when i'm trying to import the android.support.v7.app.ActionBar it just disappears immediately. whatsthat about?
You need to stick with ActionBarActivity if you want to use this library. For your particular case, instead of having a ListActivity, you should:
have an activity extending ActionBarActivity;
load a ListFragment into it, possibly from the support libraries as well (android.support.v4.app.ListFragment).
Regarding your second question, the "design" view will not show your toolbar if you load that at runtime. It just reads your layout XML file without running any code, according to the theme you have set. It should not be intended as a real, reliable preview of what your activity will look like. For that you need an emulator running the OS, or a real phone.

How to remove android v7 -app compat support

I am facing some problem in android suppport v7 app compat. is giving error that some jar missing. I want to make a simple hello world app with android support v4 or something, but how to remove android support v7?
It's a bit of a chore to remove it. It's easiest to just create a new project, unchecking "Create Activity" in the wizard and then add a new class to src.
But if you really, really want to remove the v7 stuff from an existing project, here's one way to remove it, the action bar, and the fragments stuff:
Delete the android.libary.reference line from project.properties, ignoring the warning at the top.
Change the type of MainActivity from ActionBarActivity to just Activity.
Delete the v7 and v4.Fragment imports.
Delete the whole "if (savedInstanceState)" statement from MainActivity.
Delete the PlaceholderFragment class from MainActivity.
Replace the contents of activty_main.xml file with the contents of fragment_main.mxl or your own layout.
If you didn't use your own layout, remove the tools:context attribute/value from activity_main.xml.
Edit each of the 3 styles.xml files (under values*) and change the parent of style AppBaseTheme to "android:Theme.Light" (or your choice).
Edit menu/main.mxl and remove the app:showAsAction attribute/value.
Organize imports and clean your project.
Just to be clear: this isn't something you really want to do for a real app, but more something that you might want to do if you're just creating a demo or proof of concept.
Edit:
Going forward, you can add an additional template that creates an Activity without appcompat_v7. You can get one by CommonsWare here. There's a nice write about it here.
For your information this happened because you upgraded your ADT to the most recent version (22 I think).

Categories