Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist a View across Activities in Android

Tags:

android

I'm working on an Android application and I'd like to maintain a top-bar of sorts in most of my Activities, as per the Twitter and Facebook applications. How is this done? I'd like to keep it there at all times, as it'll provide functionality core to the whole application.

like image 301
Naftuli Kay Avatar asked Dec 13 '22 15:12

Naftuli Kay


1 Answers

Break the title bar out into a separate layout, and use the include xml tag. I do that in a few of my apps. Each of your activities can inherit from a Base Activity that contains events for the included layout, e.g. if the title bar has buttons.

Example pseudocode below.

title.xml

<LinearLayout>
<TextView text="Some text"/><Button text="Some Button" onCLick="buttonClick"/>
</LinearLayout>

activity layouts for each layout

<RelativeLayout>
    <include  layout="@layout/title" />
</RelativeLayout>

BaseActivity

public class BaseActivity extends Activity {
     public void buttonClick(View v) {
          // do something interesting.
     }
}

public class OtherActivity extends BaseActivity {}
like image 124
Robby Pond Avatar answered Dec 15 '22 11:12

Robby Pond