Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:23:55+00:00 2026-05-25T16:23:55+00:00

I have an application with a common Header in all layouts. I want that

  • 0

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, my “Main” for instance.

What is the best way to do that?

I know that I can define the onClick(View v) for every activity, but maybe there is a better way to do that. Even making every activity be some (via heritage) other that has the onClick(View v) defined sounds bad.

header.xml

<RelativeLayout ...>
    <RelativeLayout android:id="@+id/relativeLayout1" ...>
        <ImageView android:id="@+id/logo_cats"></ImageView>
        <ImageView android:id="@+id/btn_home" ...></ImageView>
    </RelativeLayout>
</RelativeLayout>

every layout

...
<include layout="@layout/header" android:id="@+id/header"
        android:layout_height="wrap_content" android:layout_width="fill_parent" />
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:23:56+00:00Added an answer on May 25, 2026 at 4:23 pm

    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:

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView android:id="@+id/logo" .../>
        <TextView android:id="@+id/label" .../>
        <Button android:id="@+id/login" .../>
    </merge>
    

    activity_with_header.xml:

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

    Header.java:

    public class Header extends RelativeLayout {
    public static final String TAG = Header.class.getSimpleName();
    
    protected ImageView logo;
    private TextView label;
    private Button loginButton;
    
    public Header(Context context) {
        super(context);
    }
    
    public Header(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public Header(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public void initHeader() {
            inflateHeader();
    }
    
    private void inflateHeader() {
        LayoutInflater inflater = (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:

    public class ActivityWithHeader extends Activity {
    private View mCreate;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_with_header);
    
        Header header = (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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application that uses a library which resides in TOMCAT_HOME/common/lib. This
In a typical business application it is quite common to have forms that are
In my CodeIgniter application have a common header and footer view, which I include
I have a common header and common footer in all the view controllers of
I have a webcrawler application. It successfully crawled most common and simple sites. Now
I have a Client/Server application, where the Client and Server have some common tables
I have two applications in two solutions in VS2008 that share a common dll,
I have a project where there are multiple applications that have some common configuration
I have 2 Java EE applications which need to share some common user related
I have application that makes different queries with different results so the caching in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.