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.
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 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.
I am not sure if this is the right way to do it. I have a ChatActivity using FrameLayout in frame.xml. This activity needs to be reused across 5 activities. Is there anyway to do a code reuse? This acitivty runs independently of other activities.
For example, in activity A, which uses main.xml, I want ChatActivity and frame.xml to be included. What is the best way to achieve this besides merging the ChatActivity and frame.xml into activity A and main.xml? Merging the activities would mean that I have to copy and paste codes 5 times into different activities. I am not sure if this is the right way...
You cannot "include" an Activity to another Activity. Since your Activity has a basic functionality that all your other Activities use, you could have all your Activities extend this basic Activity.
The best way though for you would be to use Fragments and the compatibility library.
Regarding layouts, you can have reusable ones and import them to your current layout using include.
Hope this helps!
Use Fragments. you can create one Fragment and use the same in all activities. Fragments have their own view. Look at the following link for fragment.
http://developer.android.com/guide/topics/fundamentals/fragments.html
you can use fragments before 3.0 by including compatibility library.
http://android-developers.blogspot.com/2011/03/fragments-for-all.html
I android you can reuse xml files by using the include tag like
<include layout="#layout/okcancelbar_button" android:id="#+id/okcancelbar_ok"/>
To share functionalites of an activity with other activities, create a base activity with common functionalites and make the other activities extend from it.
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().
I want to create a component that will appear as a navigation menu for an Android application. Basically, the custom component is a rectangular "Div" (to use HTML terms) that contains six buttons. Each button provides a link to another part of the application. I want to use this on every "page", so I want to make it easy to maintain.
What is the recommended class to extend for creating custom components like this? (I've seen the "Widget" class, but not sure If this should only be used for widget that appear outside the app (like Google search))
And
Is the process as simple as creating the custom "Widget" class with it's own XML layout and then adding it to each Activity class?
The class to extend is View, the Widget class is for widgets in the homescreen. This is a nice doc to read: Building Custom Components, I suggest to look at the Compound Controls section, that seems suitable for your problem.
Is the process as simple as creating the custom "Widget" class with it's own XML layout and then adding it to each Activity class?
Yes, once you have written your own view, you just have to add it to your layouts in the XML (just like you do with the android views), something like this:
<com.your.package.YourNiceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>