I am following the Google tutorial for building your first android application. I got to the point where I needed to implement the actionbar actions with the functions openSearch() and openSettings().
I implemented all of this in the MainActivity.java file.
My question is this:
In the example app you can type a message and then send it and it displays it in a second activity. In the second activity, the top action bar changes and does not display my Search icon or perform the action when the settings button is clicked. In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
My other somewhat related curiosity is this. The method openSettings() is called when I click the 3 vertical dots and then settings. These 3 vertical dots show up on every activity, and settings is always in the list. However clicking settings obviously doesn't perform the call to openSettings() when in the DisplayMessageActivity and not MainActivity. How is it that settings and the vertical dots are carried over?
Second to last, how can I add other selections to the drop down list from the options/vertical dots in the action bar? Settings is always there although it responds differently in each activity which was my first question. But I would like to add certain things to the options menu that are on all activities, and some things that are unique to some activities. I assume there must be a better way than repeating switch statements and methods in every Activity.java file.
And finally, what is the best practice to implement an action bar over multiple activities?
Obviously different activities will often have different icons/actions in the action bar, however some things like the 3 vertical dots(options) and settings within that would obviously be acceptable to have in every Activity, while it would be nice to add other things to the options list I don't see why settings should ever change across activities. Yet as I stated before the method is not called in DisplayMessageActivity unless I repeat the code in DisplayMessageActivity.java that I had added to MainActivity.java. I'm confused as to where I can add these so that they are displayed on all activities without repeating code. And I'm confused as to how the actionbar's options/vertical dots are carried over to all activities while others require the repeating of code in each activities' java file that I want them to show up in.
I know this was a bit of a long winded quesiton, I will clarify if necessary. I'm just a bit confused. I was able to make it through the tutorial fine as I have a decent understanding of java. However google's guide isn't written that well and the Android environment is very confusing to a beginner.
I do understand how things work to a degree, I just want to ensure that I'm actually doing it in a way that when my app grows in complexity it won't be a mess of unnecessarily repeated statements and methods.
Thanks in advance for any assistance and tips.
In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
That is certainly one solution, but as you obviously know, it's not a very good one. There are at least two alternative solutions:
Create a MenuActivity class which implements all the logic for common menu items and then extend this class from all of your activities, rather than extending the standard Activity class.
Use fragments to implement your UI. Fragments are similar to activities in that they create UI elements from an XML layout. One difference is that they live inside a "host activity". In this particular case, the host activity will provide the common menu functionality and each fragment can customize it further depending on your needs.
How is it that settings and the vertical dots are carried over?
Most likely your DisplayMessageActivity overrides onCreateOptionsMenu() and inflates a menu XML layout which was created by Android Studio (or Eclipse?) when you created the activity class.
Related
I'm trying to create an overlay that is triggered when a button is pressed. This overlay is supposed to allow the user to add their contact and I was wondering how can I use fragments to get this effect like you can see in this mockup.
I am in a dilemna over using fragments is the right choice. My reasoning being that I only need to have it do one task that is adding contacts, and thus I do not need a whole activity.
This is what I have on the main activity right now
I would really appreciate any help on understanding how to make this work.
You can use a DialogFragment.
It behaves like a normal Fragment for the most part. Here is a guide for a basic implementation https://guides.codepath.com/android/using-dialogfragment
They appear automatically in the center of the screen. To get it a bit lower like in your mockup you can change where it is in the window. Here is an answer showing such Position of DialogFragment in Android
I am developing an activity that contains a kind of mix between a menu and a progress bar. Below this, there are views that are "visible" or "gone" depending on the progress in which the user is. I have programmed everything in the same activity, but I have doubts that this is efficient because we are talking about more than 1200 lines of code and most of it is only used in part of the progress.
Do you think creating a structure with fragments would be better? If so, how would you go about changing from one fragment to the next from a button within the fragment?
I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM
I don't know if it's a dumb question because it is one of my first apps.. but if that's the case, please also explain why!
If I create a Tab-Based Activity-Structure, I get nearly what I am asking for but only for Tabs. What I want is generally opening a second or even third activity in one main-activity which contains the main-interface.
Example: I have a Title-Bar and a little icon at the bottom-left corner for some reason during the whole app runtime. Now: How can I control them with the main activity and open at the same time some other activities/views into the existing interface? It should then be shown below the title bar and lying underneath the little icon (the icon is not really important, just fictional). Also it would be nice if I could add some fade in effects to these embedded activities/views. Is that somehow possible?
I currently only know, how to open activities each over another filling the whole screen, except in the case of tabs... maybe I only haven't inspected the tab structure enough.. however, I would be delighted about each answer!
Regards
What you are looking for, are Fragments.
Fragments can be used to fill a part of the screen, while doing something else entirely in a different one.
In your example you can create a main activity that contains two Fragments. One Fragment controls the title bar, the other one controls the main content area.
By replacing the current Fragment in your content area with a different one on the press of a button, you can achieve the behavior you are looking for. At least, that's how I did it in an app of mine containing a main content area and a music player. The music player stays in place while the main content changes.
Sadly I can't provide any example code right now, but here is a tutorialthat should help you get started:
Android User Interface Design: Working With Fragments
I am working on an Android project where a group of buttons needs to show on the bottom of every screen (activity) in the application. The group of buttons are basically a navigation bar. I want to know the best way to do this without creating new buttons for every activity. I have been around programming (C++/C#) for many years but am pretty new to Android and Java so if someone can point me in a general direction, it would be appreciated.
Thanks.
I bet you need to use "include" tag for xml layouts. It's the best when you need to reuse some UI components. See http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ for the examples and description.
To elaborate on Konstantin's answer, after you've used include, you'll need to bind actions to these buttons.
If the buttons should have the same action regardless of the activity they are in, use the include tag to create their layout and then create a parent NavigationActivity (or whatever else you want to call it) class from which all your other activites will inherits.
In the parent NavigationActivity class' onCreate method, you can set up the onClickListener (and other needed stuff) for the buttons.