I have been thinking of two ways I can create the layouts for my activities and wonder which is the better way to do it? Optimizing resources and speed up the application.
My activities have a constant menu tab control bar at the bottom of the page. The center will be used to house the contents respective content selected (content has no similarity, all of them uses different layouts)
Method One:
Use one activity and house all fragments required (which contains the contents) and do a .replace to swap fragments whenever needed. (However, do I instantiate all fragments at the onCreate on at xml? Or is creating it on runtime, whenever required more advisable?)
Method Two:
Use multiple activities, each to house only a specific content. Use an intent to send to the appropriate activity when required. This will require the duplicate of the constant menu tab control bar as stated before.
Can someone please advise me the better method and how should I implement it would be more appropriate?
Related
I'm trying to make an app and I have made a blueprint for a specific activity, but I don't know how to implement it. The layout contains few buttons at the top of the activity, and each button features some information, which is displayed inside the view. The view which needs to be updated is present under the buttons. I don't want the activity to be changed, instead it should update the contents of the View, which is different for each category/button.
By doing some research I have realised that the "Tab Layout" can be used to achieve my requirements, but I don't want the tabs and I need some stylish buttons as a replacement.
I know I'm not the best at describing, so I have looked upon Dribble and found one design which is 100% similar to blueprint.
I want to achieve this using XML and Java using Android Studio! Every suggestion will be a great support foy my app.
Thanks a lot.
As far as I know, you could achieve that by using fragments (which is the same concept you would have used on TabLayout). I don't really know how much knowleadge you have on Android, but if you know what a Fragment is, it should be easy for you to recreate the idea.
You have 3 buttons for 3 different fragments, so you must design every fragment by separate and change it depending the button you click.
After a lot of time spent I can't find the solution. I want a button or any clickable view should stay visible for all activities and it should be only for one specific app not like chat heads. I am basically making a library so that's why I can't use base activity.I have attached the image as well for a better explanation. How can I achieve this any suggestions? thankyou...
I
It sounds like you need a ViewOverlay. This is the API documentation.
ViewOverlay is usually tied to a single view, but if you wrap it in a fragment, you should be able to attach it to each view in your application. This should create the effect of an of an application scoped overlay.
There may be a more elegant way of doing this, but I am not aware of it.
EDIT: You can also wrap your layouts inside a frame layout(s) along with a seperate nested view (the view that you want to keep on top of the stack).
Frame layout creates a 'stack' of inner views. If you use this approach, you can programmatically ensure that there are exactly two views present and visible as children of your frame layout at all times. one will be the layout tied to your current activity. The other will be the view that you want to be overlayed.
I know that the term 'programmatically ensure' is vague. This is because there are many ways to make this happen. It is up to you to decide which way best suits your needs.
Despite its not best UX. I wonder what is the best solution to start several different Activity(Dialog) with different callback implementation. I assume starting each dialog needs to be from static fabric method with context.startactivity(dialog1). Each dialog looks exactly the same besides some title and message but callback for ok and cancel buttons are different. I want to separate implementation of dialog callbacks(ok, cancel) from generic dialog behavior. What if I can't pass actions while starting activity from static method, I don't find Bundle to fit this case.
How about this, create an enum for the dialogs.
Based on the enum you can either have the values for everything be in the enum itself or switch on the enum in your code at the appropriate places.
recommend creating different click listeners for the yes and no buttons.
In those click listeners you can switch(enum) and for each case have the specific business logic. OR create different click listeners and use a factory that will allocate the listeners based on the enum.
either solution works depending on how you want to code it. They both have their own pros and cons.
There is an AlertDialog.Builder class that you may be able to use depending on what your dialogs look like. There is also the dialogfragment class that you can extend to help out with the dialogs.
if you want to show the users several dialogs you will need some sort of queue that you populate with all the dialogs that you want to show and then show them one after another, IMHO, use different views and just replace the view in an activity so you can slide it in or animate it in somehow.
You can make them look like cards and then just change the text inbetween that way switching on the clicklisteners based on the type of the current view will be easy and you can even have the enums provide the views using the R.layout.layout_name as a value in the enum.
I know that is a lot and maybe some of it is unclear, Please ask questions and i will do my best to respond in a timely manner.
I have some problems with the layout and activity and I don't know are they different,are they related?
I think the layout is a place we can add or remove our views and activity is just a place that shows any thing in our layout, is this true?
An activity:
is an instance of Activity, a class in the Android SDK. An activity is responsible for
managing user interaction with a screen of information.
You write subclasses of Activity to implement the functionality that your app requires. A simple application may need only one subclass; a complex application can have many.
A layout:
defines a set of user interface objects and their position on the screen. A layout is made up of definitions written in XML. Each definition is used to create an object that appears on screen, like a button or some text.
A layout deals with the user interface. Its where you set all your views that will be visible on the user interface.
The code behind (.java) sets the layout you created as the content view and manipulates the behavior of the views you have set. For example, sets the text for a text view.
The activity then is the whole thing, the layout and the code behind.
An activity is the java code which attaches actions and puts content to/in a layout. For this the Activity loads the layout.
Briefly,
Activity is the java part of your projects. The program and any kind of algorithms are implemented here. Also layout views come to life in an activity.
Layout is where you organize the views in your page. But without activity, they have no meaning. Because in activity, you have to get these views and use them programmaticaly.
All together, you load views from layout to activity and in activies you implement your whole program.
A layout defines all the appearance of an app and this is of no use without a java program which helps in real functioning of that visual display.
Thus we define what an app does by writing its java code and a special java class called activity decides which layout to use at a particular instant and tells the app how to respond to the user.
I vaguely understand what fragments are used for. However, I don't understand why ADT seems to suggest that we should ALWAYS use them. I am aware that I can simply delete the fragment layout, as well as the code in Activity for the fragment. But is it ACTUALLY recommended to use fragments all the time? What are the benefits of not deleting it?
Is there even a point of an Activity having a single fragment? What is the difference with it having no fragments at all?
Additionally, how do you know whether to create multiple fragments in one Activity, multiple Activities with a single or zero fragments or a combination of Activities and fragments?
For example, if you have a Terms and Conditions button in your Activity, and you want it to open a screen containing a document listing the terms and conditions, should I be starting a new Activity for that? Or should I just move a fragment containing this content to the front?
Using Fragments is how modern apps are developed today.
Some of the benefits are a better performance when switching content (lets say 2 fragments)
because you are not leaving the current Activity.
Also, apps with swipe tabs (for example) are built with an Activity as a container and Fragments as the content itself.
Other benefit is that you can do much more with one activity, distributing work between the fragments. Each Fragment have its own Layout and therefore its own components, so maintaining the code and keeping organized is easier.
Eclipse does this way as suggesting a start point. You can totally ignore this and start from scratch. And that's all.
I suggest you to keep the Fragment to start getting used to it. Even if there is just one.
And, if later you need to work more with that Activity, it will be easier to add new features by creating a new Fragment, without modifying your previous code.
Good luck.