Skip to content Skip to sidebar Skip to footer

Common Clickable Header For All Activities In Android

I have an application with a common Header in all layouts. I want that whenever the user clicks at the the ImageView with id btn_home, the app will go back to a specific activity,

Solution 1:

You can make a custom component out of your header and define 'onClick()' in it. For example, make a new class Header that would extend a RelativeLayout and inflate your header.xml there. Then, instead of <include> tag you would use <com.example.app.Header android:id="@+id/header" .... No code duplication and the header becomes totally reusable.

UPD: Here's some code examples

header.xml:

<mergexmlns:android="http://schemas.android.com/apk/res/android"><ImageViewandroid:id="@+id/logo".../><TextViewandroid:id="@+id/label".../><Buttonandroid:id="@+id/login".../></merge>

activity_with_header.xml:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"...><com.example.app.Headerandroid:id="@+id/header".../><!-- Other views --></RelativeLayout>

Header.java:

publicclassHeaderextendsRelativeLayout {
publicstaticfinalStringTAG= Header.class.getSimpleName();

protected ImageView logo;
private TextView label;
private Button loginButton;

publicHeader(Context context) {
    super(context);
}

publicHeader(Context context, AttributeSet attrs) {
    super(context, attrs);
}

publicHeader(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

publicvoidinitHeader() {
        inflateHeader();
}

privatevoidinflateHeader() {
    LayoutInflaterinflater= (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.header, this);
    logo = (ImageView) findViewById(R.id.logo);
    label = (TextView) findViewById(R.id.label);
    loginButton = (Button) findViewById(R.id.login);
}

ActivityWithHeader.java:

publicclassActivityWithHeaderextendsActivity {
private View mCreate;

protectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_with_header);

    Headerheader= (Header) findViewById(R.id.header);
    header.initHeader();
    // and so on
}
}

In this example, Header.initHeader() can be moved inside Header's constructor, but generally this method provides a nice way to pass on some useful listeners. Hope this will help.

Solution 2:

Extend the Activity class and build a MyActivity class for example. In this MyActivity class you could include the code for the onClick.

Now create a layout that just holds your header. Include that layout in your activity layouts.

Extend all your activities from MyActivity - that's it.

If you need the same behaviour in ListActivities create a MyListActivity as well.

Post a Comment for "Common Clickable Header For All Activities In Android"