How to create identical activities in Android? - 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().

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.

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 !

New to android, have a question about layouts in general

I'm making a new application and its basically filled with information about Warcraft.
I have similar apps on my phone that have similar information and when I looked inside their .apk they only had like 10 layouts.
The app that I am making already has 5 layouts and it seems like I will need about 50-60 layouts.
So now my question is it normal to have that many layouts? Or do I have to learn to make one general layout and keep reusing it? For example, like if I need to display information about a topic for instance the classes in warcraft which are 10 different classes with 2-3 different guide pages on average for each class, would I need to make a different layout for each page or is their a better way of doing it?
I would really appreciate any input/suggestions.
What I recommend is having one layout for every type of screen (basically one per Activity) and use Java to fill in all of the info. Use getResources().getString(int id) and pass something from R.string. That means you need to keep all of your information in a strings.xml file in your values folder (located in /res/values). List all of your views in the layout XML file and then find them by ID and set their values.
It's best to keep all of your string resources in a separate XML file and not hard code them into the layout (otherwise it's a pain to replace every instance of a word you realized you misspelled or something). If you don't know how to write XML, that's okay since there are tools in Eclipse, but I HIGHLY recommend learning it.
You can re-use layouts without any problems. In fact for maintaining the code it is a very good idea to do so. No one wants to maintain 50+ layouts and associated code.

Can i have same activity using different xml file

I'm creating an android app that will have multiple pages with the same layout, but the only thing that changes will be a string that is displayed on the top (using setText). Can I use a different xml file in the same activity class.., or does Android not allow that?
No problem with that. You can use in the same activity as many XML layouts as you want. Simply switch between them using setContentView()

how to add number of button in run time in android

I am beginner in android development and my problem is that ı want to add some buttons in runtime. I mean, number of button will be change according to flow of program so i need to create different number of buttons at different situations. In code section i can handle it by using array but what about layout file? how can ı set the layout file according to flow of program. I hope ı could explain my problem. Thank you very much.
The xml files in res/layout are static descriptions of layouts. You can create different ones to be used in different contexts (different activities, dialogs, etc). You can actually even replace one layout with another one in the same activity. What you cannot do is to modify the xml files during runtime.
If your UI depends on runtime variables, then you will have to act accordingly. If it's just the number of buttons that will change, you can either
Add new buttons using addView(button);
Add a ListView to your xml file and use an ArrayList and an ArrayAdapter to determine how many buttons you will need.

Categories

Resources