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.
Related
For example, I have activityMain which contains the main logic of the Application, but it should be opened from several buttons that stored in other activities, and some activities contains multiple buttons too. And they should call activityMain. So i need to pass different data from db depending on which button called activityMain
Help please, there weren't similar topics at all.
If i Understand your question correctly, you can achieve this functionality with android jetpack navigation component. I add the link of documentation below
Get started with the Navigation component
So im wondering how I can change what is displayed on the phone screen without the need for creating a new activity each time I wish to do so.
For example in a simple game im trying to make: there will be a small row of buttons (inventory, stats, save, options, etc). When I press one of these buttons, how can I change the view within the same activity to show the appropriate data without having to create an entirely new task, if possible.
Two possibilities here depending on what you're really trying to accomplish.
1: If this is just another xml layout you want to display I would suggest using fragments.
http://developer.android.com/guide/components/fragments.html
2: If this is graphical ( often true with a game ) you will need to extend SurfaceView and
implement a drawing thread. http://developer.android.com/reference/android/view/SurfaceView.html
( There are multiple examples of how to do this if you google for SurfaceView example ).
It's not a good practice but if it's simple enough you can just use view.SetVisibility(View.Gone) to views you wish to hide and view.SetVisibility(View.Visible) to the views you wish to show.
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.
I have defined my Relative Layout using the drag and drop tool in Eclipse, so all of my buttons are laid out how I wish. My issue is when I set the onClick listener, that calls a method in another class. So to be able to redraw items on screen, I need to access the layout manager so I can add and remove buttons from the screen as well as update textViews. I have done all of this in a demo I made in Java, and I used a JPanel with GridBagConstraints. Now that I am moving to Android, a system I haven't done much development in, I am at the point where I have to learn some new stuff. For example in my demo I made I could do this:
grid.remove(trueButton);
grid.add(falseButton);
grid.remove(textField);
grid.add(backButton);
Essentially I want to be able to do the same sort of thing in my Android app. If you guys need more info I can provide, I wasn't really sure how much would be needed since I am looking at really just where to start. Everything has been declared in the XML since the drag and drop part of Eclipse does that all for me. It is just the Java part that is giving me some issue.
Why not just setVisibility of the buttons you wish to hide/show? Same with the TextViews.
You can set visibility to 'GONE' and it will be as if the view has been removed (taking up no space in the layout and not responding to touch events.).
I need some advice for those who are experienced making Android applications. What I would really like to have, for my application's appearance: at the top, a title-bar which is a ImageView (content is a png), and at the bottom a series of custom buttons which make up a tab-bar like thing. In between the title and the tab-bar is the Content, which may be anything... (most likely buttons)
I have been doing this by making a RelativeLayout which specifies LeftMargin and UpperMargin for x,y coordinates--
Currently all of my activities are inheriting a custom MyActivity class, which rebuilds the title and the tab-bar at the time of onCreate. This seems bad to me!
PART1)
---A solution to Persistent data
Since the "tab-bar" and the title are persistent no matter what screen you're on during this application's run-time, it makes the most sense to store them somewhere... How should I do this? Make a singleton object that the Activity's ask for?
I thought a little about the singleton object, and I'm not even sure what I would store, since the Views that are on displayed during Activity A have activity A as context, and not Activity B.
PART2)
---Animation Aesthetics
I would really like to have the "Content" (the view in the middle between title and tabbar) slide out to the left, and the new content slide in from the right. I.e, I'd like the tab-bar and the title to remain fixed while the "activities" change. is this at all possible? What should I do to achieve it?
one idea I had, was to make all of the program in one activity! I would create an animation for the Custom View in the middle, and I would override the "back" button to navigate correctly to the previous Custom View. Is that a horrible idea?
Anyone have any advice?
Read http://developer.android.com/design. Most of the design principles can be applied to apps that run on legacy releases; it's not just limited to Honeycomb and Ice Cream Sandwich. Do consider the Action Bar and Dashboard design patterns.
I don't really recommend using just one Activity -- generally, an Activity should be a separate, encapsulated, pretty well defined chunk of functionality that can execute independently of other Activities.
To avoid duplication of your UI, consider reusing XML layouts.
To avoid duplication of your logic, consider using Fragments. You should be able to mix and match them in your activities.
To achieve the animation you describe, consider implementing a ViewPager.
Using the ActionBarCompat sample app and Android Support Library, you can enjoy modern goodies like Action Bar, fragments, tabs, and horizontal sliding transitions on devices running Android all the way back to Donut (1.6).