Using Android Studio on a project with the minimum SDK set to API 15, if I use the UI to generate a new "Blank Activity with Fragment" (or any other activity template that uses fragments), the generated fragment will inherit from android.support.v4.app.Fragment. But if I use the UI to generate a new "Blank Fragment," it inherits from the native android.app.Fragment. What logic does the IDE use to select which one to inherit from? Is there any way to force it to always use one or the other, so I don't have to go about changing the imports?
You can not change the default Android Fragment class creation in the IDE and I do not think this is a huge thing.
But if you are really worried about this, :) you can create your own custom File Template for Fragment in Android Studio and reuse it all the time or Gist it and keep it.
Related
My old eclipse version worked perfectly but after updating eclipse, when I create a new Android project, it creates two XML layouts. 1st is activity_main.xml and second is fragment.xml. So when i add some item in activity_main.xml it wont display anything.
So how to use fragment layout?
This is the new Project Structure for Android by developers.
If you want to view any UI widgets put them inside fragment.xml file.
And any java code should put inside PlacementHolder class that is inner class of Main.java.
As others mentioned that's just the recent new project style from Android Team. But I guess you were also looking for an introduction into Fragments when you asked:
So how to use fragment layout?
Basically:
Android team has completely covered Fragments in their documentation. Start from here and then refer here. Then download this official source code and examine it closely.
Now I'm gonna share a personal experience:
I suggest you first study this tutorial on fragments from Vogella's website.
Why? Because the Android documentation is slow and rather boring there fore hard to understand, whereas the Vogella version is to-the-point and sharp.
You can then go back and study Android documentation.
at least read this: http://developer.android.com/guide/components/fragments.html it will give you an idea how the fragments work. And better switch now to Android Studio, the sooner the better.
I have an existing app that is using a Dashboard style pattern where there's a main menu, and clicking icons on the main menu drive start different activities... In order to navigate to a different function, you need to go back to the Dashboard menu and select another icon.
I want to change my application to a tabbed format similar to the one below with Icons:
What type of View is being used below? Is this a FragmentActivity? Also, what is the best approach to go about conversion? I have a bunch of layouts with listviews in linear/relative layouts... Will I be able to reuse any existing code? I want also to be able to use the solution with Android 2.1 and up if possible.
Thanks!
In the image you provided, it looks to be a TabHost that is used (which can be within a normal Activity or a Fragment Activity) and will be available for Android 2.1 and beyond when using the Android Support library. Based upon your description, you most likely have an Activity per each of your items that you will probably want to convert into a different Fragment. This may take a little time, but a Fragment is very similar to a normal activity in many ways, so once you start getting used to it, converting over the old Activities should be a breeze.
If you plan on using these tabs and you follow the Android design UI guidelines, you may want to use the TabHost in conjunction with a ViewPager. There is a great tutorial for this online that also allows for backward compatibility (down to at least 2.1) found here: http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
Support library for fragments/viewpager: http://developer.android.com/tools/extras/support-library.html
More info about a TabHost and using Tabs with fragments can be found here:
http://developer.android.com/reference/android/widget/TabHost.html
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
You can use TabLayout and a TabActivity to achieve layout in picture. Bad news is these are deprecated as of Android 3.0. Google advises to use fragments instead of these.
I've got a project written for API 11 and up, no I need it available for 2.3.3 OS devices either, so need to use Support Library. What is the easiest way to go with it? I know some methods have different names for Fragments, for instance. Can it be possible to use "Find and Replace All"?
For the most part, yes it will be possible. You'll also have to change less than you think, for example the Fragment class is fine.
You will have to change getFragmentManager to getSupportFragmentManager, as well as changing your base Activity to a FragmentActivity. Those are the most common two off the top of my head.
The easiest way is to change the target to 2.2, add the support-package as a library, and see what breaks and then fix it-- and once you have a replacement fix you can use find and replace all to fix all instances of the issue.
Can it be possible to use "Find and Replace All"?
As far as I'm concerned, it's never a good idea to do a simple "Find and Replace All"... it's far too easy to make a mistake.
What is the easiest way to make the transition?
The change is very simple and should not require you to change your build target at all (assuming you are targeting Android 1.6 or above, of course). The key changes you must make are:
For each Activity that makes use of the support libraries, have it extend FragmentActivity instead.
In each FragmentActivity, replace all calls to getLoaderManager() with getSupportLoaderManager().
In each FragmentActivity, replace all calls to getFragmentManager() to getSupportFragmentManager().
Replace all relevant android.* imports with their corresponding support.v4.* import statements.
I want to show my activities inside of a running activity. I need something like frame in html language that is used for showing other pages inside a page. I know Tabhost has this ability. Which one of other controls has this ability?
Thanks,
Google introduced fragments in Android 3.0 and upper to create a portion of user interface in an Activity. But it is not two Activity, becuase activity <> window. For lower version you can manually load xml layout in your activity.
The Fragment API is really your best choice, it's quite easy to use and you can dynamicly add them to your Activity Layout (take a look to Framgent For All - google Article) by code using the FragmentManager, this feature it's since 3.0 altough Google also released a pretty nice Compatibility Package that you can download out of the SDK Manager and added to your project like this Fragment For All:
I am working on android app where I am thinking to develop reusable UI interface. How it can be developed and included in my .xml's
Here I want to develop a progress bar with my image and it will be display on some .xml's.
Please provide any code help.
I am a new in this field.
you can use <Include> xml tag within your layout xml,
read this :)
http://developer.android.com/resources/articles/layout-tricks-merge.html
I think you need to use styles and themes. Check this out.
You can either build custom (compound) views, look at Building Custom Components.
Or you can use Fragments. Fragments are new in Honeycomb (Android 3.0), but there is a compatibility library that adds fragment support to lower android versions (can be found in your ANDROID_SDK/extras/android folder). Or you can mix both of course.