Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Master Layout Templating

Hi am am working on android.

I have a layout which was used in every activity.

I mean I have a layout which has footer and header.

On each activity, header and footer same and has same actions.

As you see,

I want to use a general layout for header and footer.

I mean in a activity, I will put the content area layout to general layout.

I find someting but not enough.

How can I do this?

Is there a dummy docuument for do this?.

sorry for bad english.

like image 683
user999822 Avatar asked Mar 22 '12 10:03

user999822


3 Answers

What you are talking about is a new Android design pattern called Fragments. Since 3.0 fragments are small activity like views that can be combined to form a screen.

So you would create a Header and Footer fragment and then include these across all activities that require them.

The other pattern you might want to look at is the Action bar pattern, which is used to place a bar at the top of screens with common content and functions, similar to your header.

Also another way would be using xml files to define your header and footer then instantiate these as views in code and add them programmatically to your content views xml definition. The problem with this is that the code behind the header and footer would need to be replicated in each controller. Your best bet is using Fragments, I'll put some useful links below:

http://developer.android.com/guide/topics/ui/actionbar.html

http://developer.android.com/guide/topics/fundamentals/fragments.html

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

https://stackoverflow.com/questions/5710573/need-a-fragments-example

like image 99
Michael Allen Avatar answered Nov 06 '22 04:11

Michael Allen


You could use includes for the header & footer or add them dynamically from a base class, but I think a better approach is to use a single Activity to host the app, and then use Fragments for your screen content.

http://android-developers.blogspot.co.uk/2011/02/android-30-fragments-api.html

like image 42
Ollie C Avatar answered Nov 06 '22 02:11

Ollie C


I have nothing against fragments, and Yes, they're the way to go, but for the beginner android developer, you can do achieve what you're trying to do with <include>s and base activities.

This article explains nicely the use of <include>s, but to sum it up, you can have a layout xml file that you can "include" to another layout, instead of rewriting the same stuff over and over.

For the functionality of the headers and footers (assuming they do something when clicked), you can create a base activity that you can extend instead of the normal android Activity. Define the logic for the header and footer clicks in this base activity, such as with this sample code:

public class MyBaseActivity extends Activity {
...
public void onHeaderClick(View view) {
// when header is clicked, do this.
}
public void onFooterClick(View view) {
// when footer is clicked, do this.

In your layout (the one you have as a separate xml), add an onClick attribute to your header/footer, assigning the name of the method in the base activity.

such as

android:onClick="onHeaderClick"

Then it's just a matter of extending MyBaseActivity for all your activities that have headers and footers.

like image 1
josephus Avatar answered Nov 06 '22 02:11

josephus