I have an actionbar which has a title and few buttons. I want to display this actionbar for all activities. So, instead of including layout code of this actionbar for all screens, how can I use it efficiently ?
Create a BaseActivity(which extends Activity), where you can write this code just once. And then all other activities of your code must extend this BaseActivity. Also in your xml layout, create a separate layout file for this Actionbar, and then reuse this file using the include tag.
Hope this helps.
Related
I'm developing an app that has the same action bar for all Activities. The only thing that changes is the menu, which can be easily built dinamically.
At the moment, I have a xml file for each toolbar of each Activity, but there are plenty of code repetition thoughtout the code. How should I deal with it?
I've thought of making a single xml file and including it in the Activities layout. However, there are plenty of stuff that are made programatically, such as definition of the button, title and menu. These methods are located in the Activities onCreate. How is it done to reutilize this code?
Im not sure If I understand you correctly, but maybe make class which extends after Activity (or if you use AppCompatActivity etc.) for example class MyCustomActivity extends AppCompatActivity implement there your actionbar, and then in every new Activity class where you need that ActionBar just extend MyCustomActivity. In this class you can ofcourse implement some public method to set menu in subclasses etc.
I am trying to do inheritance between activities in my application.
I want to create "general activity" style for my all application.
For example, this "general activity" includes toolbar and statusbar. I want to show both in each activity in my program.
I wrote the BaseActivity layout like that:
<include layout="#layout/titlebar" />
<!-- Some control that can contain layout -->
<include layout="#layout/statusbar" />
And MainActivity that extends BaseActivity.
I want to insert in code the layout of the MainActivity to the specific space on BaseActivity.
Is it possible to do that? If yes, how can I set the layout of my inheritance activity in specific spase on this BaseActivity?
There is any control that can contain layout?
This can be possible from java side (Note that I didn't tried yet), here is an example Reusing layout XML and the code behind.
This is can also be done via layouts.
You have to make a separate layout (xml) for you toolbar or status bar, and then you have to include in required layout.
Re-using Layouts will be helpful link and Android Layout Trick #2: Include to Reuse
Sure, its possible.
You'll do it like you do any Java inheritance.
You'll want to create a BaseActivity which will extend Activity.
Your other Activities, will extend BaseActivity.
Not always, but generally, you'll want your BaseActivity to be abstract class. You don't want to create instances of it.
So, you should call setContentView from the EXTENDING Activity. That way each Activity will have its own layout.
I actually want to apply a custom title bar (kind of like the Action Bar), but since i'm developing for Froyo, I actually need to have it in a xml file. The trivial way to do this is to just write the code to create the title bar in each of the activities.
I did search around for some efficient ways and found this on SO
Similar Problem
There are 2 solutions proposed over there, but I am having problems in implementing them.
To include the layout of your title bar in the layout of all the Activities. My question is say I have a sample layout as follows, where should the include tag go and do I need to use Theme.NoTitleBar theme for the application in order to get it working?
To subclass Activity and then derive all of my Activities from that. Should the custom Title bar creation method be defined in the onCreate method of the subclassed Activity. Because if I do this, the custom title bar does appear, but it appears blank. No buttons,etc.. are present on it.
Thanks in advance for any help you provide
The tag belongs inside the top level view container (linear layout/relative layout). Yes use NoTitleBar so you don't have the Android provided title bar in your app window.
There is obviously an issue with your code there, with out any code in your post I cannot help you.
I am writing an app in which I have mutiple screens which I want to adhere to a certain theme.
For example: all screens should have the same background, all screens should have a bar on the top with the same set of actions (not necessarily dependent on the activity currently being show) and all screens should share the same fonts.
Is there a way in android to create a parent layout which all other layouts extend and therefore if I need to change some elements (say the background color) I won't have to do it on each individual screen but rather only in the parent screen.
I guess what I'm asking is, is there a sort of layout inheritance in android?
Thanks,
e.
There is no layout inheritance but you can create this bar layout in a separate file and use include to include it in the layouts that you want to have the bar.
Some of the other stuff could also be done with styles. Create a style for different elements and then add the style to the element in the layouts.
To do inheritance you can create a layout that you can include in other layouts. Then in a BaseActivity you can modify views, add event listeners, etc for the included layout. Just make sure in your onCreate to call setContentView() before you call super.onCreate().
Hallo,
Any idea where I can find a simple tabs example?
The Android Developers Hello Views Tab Layout example only details how to use separate Activities for each Tab.
I would like to use tabs to extend a screen of settings without scrolling, i.e. there are too many options for a single screen, so I'd like to split them over two tabs, but still handle them in the same Activity.
Each tab can refer to a separate layout file if that is how it needs to be done,
TIA,
-Frink
if your tabs don't need to be dynamic you can just load the views in the xml file. Just like in this example http://developer.android.com/guide/tutorials/views/hello-tabwidget.html
In your tabhost instead of passing an intent you can pass a viewID