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

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 !

Related

Is there an efficient way to perform this project?

I am learning Android Studio and I have recently come across listview.
I am making a project which has 6 buttons and each of those 6 buttons will open an activity where there are 2 additional buttons and if you click on 1 of those 2 activities you will reach the required page.The required pages have different text content.
I am approaching this problem by creating a new xml file for every activity however this leads to creation of many pages and I just wanted to know if there is any method which will reduce the number of files created for this project
If your two activity have similar look than you can use same layout,
else you have to create different layouts for all your activity.
If you have Activities that looks alike you can use the same layout file for both activities, and only change the behavior in the Java side.
And if you have activities thats acts alike you can also use the same activity and change the behavior depending on some extras.

How to create identical activities in Android?

Im developing an application with quite a number of activities.
The activities are content wise identical, containing 2 buttons and 2 textfields.
The problem I have is that I want every activity to look like every other in placement, since their content will be different.
Im using the Eclipse IDE
How do I approach this problem?
You should share one layout with different activities. Create a layout (xml file) and then use it for all the activities that you want to look identical. Just change setContentView(R.layout.yourlayout); in onCreate().

Android GUI XML vs Code

I have a short question according to creating GUIs in android. What way is better xml or coding?
I always read that xml is way better but imagine you have a scrollview.
Inside the scrollview is a relativelayout. Inside that there shall be several rows with an ImageView next to a TextView next to a RadioButton. The number of rows can vary.
Is it really better to make lets say 50 Views in the xml or a loop in code where these views are created?
Each has its pros and cons. Just to name a few:
XML
pros -> fast GUI development, keep code clean
cons -> static
Dynamic (code)
pros -> able to react to runtime conditions
cons -> more code, which means poorer maintainability and potentially buggier
If you need to add components dynamically, only way is go with code (or) mixed approach (define layout in XML and add components in code). If your components are static XML may be best.
Dynamic content is, of course, added dynamically. So your example would require some java code. You should only add the dynamic part programmatically though, so you'd still use an xml document for the static parts (it's very unusual for a layout to be completely dynamic).
If you have a fixed number of views then yes, I'd write 50 of them in xml rather than with a loop. I guess you're wondering about code duplication and, as far as I know, you'll get some when using xml.
(One way to minimize code duplication within xmls' is with the usage of styles and themes)
I agree with the above. XML is a better approach to this even when you require dynamic updates you can still use XML bits and pieces to render the content. your code will be based on XML elements but XML files will be independent. hence if you break a funcitonality in the code you know that its your business logic thats broken not the UI part, which will make it easier to develop and find problems easily.
Why you do not use a ListView instead of a ScrollView.
It will be simplier to implement and performances must be better with it.
Create a XML file with a ListView and in your activity implements your own adapter to instanciate the rows.
You can find a lot of tutorials on internet talking about that, I'm sure you will find what you need !
Good luck.

Android : How to call activity inside running activity?

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.

Navbar Class in all activities

Ok, I am doing something. I have a navigation bar that contains all the buttons for my activities. I have tried the method of Extending the other activities to the "navbar" class.
Here is what I have done so far:
I have extended all the classes to navbar (Except those that will need multiple inheritance).
used an tag in every XML layout.
What I need:
I need classes to handle multiple inheritance
All my classes even those that do not need multiple inheritance, to extend my navbar class.
If multiple inheritance is not possible, I do not mind hardcoding those classes, but multiple inheritance would be very nice :)
thanks in advance.
Java does not support multiple inheritance, sorry.
Rather than roll your own "navbar", you might consider using the action bar on Honeycomb, perhaps then using ActionBarSherlock to support pre-Honeycomb devices from the same code base.
You could do what I did for my app.
Create a "base" activity that all other activities extend.
Fill your menu bar and set up all of the listeners with Intents in it, then allow each activity to handle everything else.
The other option would be to create a XML element with whatever you want in it (if it needs to be more complex than a menu for some reason). You should still use the "base" activity idea for this.
Hope this helps.

Categories

Resources