Android : How to call activity inside running activity? - android

I am thinking to split the activity into two parts :
List view : show the files.
Second part will an activity contain the selected file.
its possible to do that, like frames in HTML ?

I think what you want to be doing is using activity fragments. The guide topic Fragments describes how to do pretty much exactly what you're talking about. To support pre-3.0 Android systems, you'll need to use the compatibility package.

Related

MainActivity is too big. How do I split it into multiple files

I'm building an Android App which has a complex screen with lots of logic.
It currently contains a listview, tabs, search box, and a panel for updating user stuff.
The probem is that the mainactivity code file became very big, although I'm seperating things to different layers, because there's a lot of UI components which affect things in the screen.
I'm trying to seperate it to several files but I don't seem to do it right.
As much as I understood Fragments is not what I need here. Are there any other ways?
Just need some directions please.
I'm asking mostly about the code, not the layout (Although I don't care changing the layout too).
Currently it's 616 lines and the biggest problem is that we are a team and the maintenance became hell...
Create utility class and put your listeners and adapters there. Use main activity only to initialise view instances and setting listeners and adapters.
Although 616 is not a particularly big file you could use Fragments as they just add another layer of abstraction. They also have a similar lifecycle to activities.
This tutorial shows how to add fragments to tabs
http://developer.android.com/training/implementing-navigation/lateral.html
You can create a base class put all the initializations and listerners there.
Likewise for function to be implemented later create a template in base class and override it in main activity. It works !

Converting Existing App to use Tabs

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.

Using multiple inheritence of activity

Is there way to inherit two different activities in android. I have to display the Map on my activity which inherit from some other activity. i want to display a map on that activity but i can't display the map without inheriting MapActivity. Is there any other way to display the map without using MapActivity.
No, android is java based. And java do not support multiple inheritance.
Java supports multiple interfaces.
Maybe using fragments will help. Fragments could simulate multiple activities. However, all "activities" must be available as fragment. I am not sure whether there exist one for maps
java does not support Multiple Inheritance, although you can come up with a clever design that will let you use functionality of multiple objects in your Activity.
read following article
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html
I modified the pattern listed by Mayank to assume that one base Activity doesn't change. I also made some tweak to show how arguments would work, considering activities will need access to base activity. In the following link, assume map activity would be BaseActivityAlpha. Here is my posting: http://www.anotherandroidblog.com/2013/01/03/extending-from-two-activities

How do I make sure TabHost references the correct names from main.xml like -> android:id = "#android:id/tabs"

I'm following the android developer tutorials on tab layouts.
(im very very new to android)
I dont have errors, but when I run it I get an activity not found exception.
The ddms asks if ive added the artists activity to the android manifest file, which I have.
in the tutorial - http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
it says :
"Notice that the TabWidget and the FrameLayout elements have the IDs tabs and tabcontent, respectively. These names must be used so that the TabHost can retrieve references to each of them. It expects exactly these names"
Where should I specify those names exactly. I know the id's need to be linked, but I dont know where to do it.
I would actually recommend that you stop following this sample code as it is using an old of way of doing what you want with tabs.
I would instead recommend that you start reading about ViewPager. I would start by reading http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html
You'll want to get the Android Support/Compatibility Library plus the Support Demos and look at the source code inside. In particular, you'll want to pay close attention to http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html
With FragmentTabsPager, you'll be using the latest techniques and have the best of both worlds: tabs as well as horizontal swiping. Google recommends using Fragments going forward for UIs like this. You can read more about Fragments at http://developer.android.com/guide/topics/fundamentals/fragments.html
See: http://developer.android.com/reference/android/widget/TabHost.TabSpec.html
The example given uses the third option given, an Intent that launches an Activity. You want #1, I think. So instead of
.setContent(intent)
Use:
.setContent(R.layout.exact_name)

How to show activities inside of a running activity?

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:

Categories

Resources