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

  • SEARCH
  • Home
  • 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 914333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:42:06+00:00 2026-05-15T17:42:06+00:00

I am trying to turn a couple buttons into a reusable component in Android.

  • 0

I am trying to turn a couple buttons into a reusable component in Android. I have successfully gotten the XML / UI portion working, but I can’t figure out how to make code behind it reusable between activities, short of recoding it everywhere. I am new to Android, so I apologize if this is obvious.

I’ve already reviewed this post several times: Android layout Tricks 3 – Part 1 but it seems to be missing a few files, and I do not have enough experience to rebuild them.

A dumbed down version of my main layout:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView android:id="@+id/webview" 
        android:layout_width="fill_parent"
        android:layout_height="375px" 
        android:layout_alignParentTop="true" />

        <include layout="@layout/navbar"/>

</RelativeLayout>

and then of my “component”:

<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
      <LinearLayout   
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center">

            <ImageButton android:id="@+id/Button1" 
                android:layout_width="71px"
                android:layout_height="wrap_content"
                android:background="@drawable/button1"              
                android:layout_alignParentBottom="true" />


            <ImageButton android:id="@+id/Button2" 
                android:layout_width="75px"
                android:layout_height="wrap_content"
                android:background="@drawable/button1"  
                android:layout_toRightOf = "@+id/Button1"
                android:layout_alignParentBottom="true" />              

        </LinearLayout>
        </merge>

If you have any additional critiques on my XML, I would appreciate that too.

  • 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-15T17:42:06+00:00Added an answer on May 15, 2026 at 5:42 pm

    What I do is make an actual component that contains all of the common code and use it directly. Here is a dumbed down version of the component class:

    public class NavigationBar extends LinearLayout {
        public NavigationBar(Context context) {
            super(context);
            setupView(context);
            hookupButtons(context);
        }
        public NavigationBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            setupView(context);
            hookupButtons(context);
        }
        private void setupView(Context context) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // inflate whatever layout xml has your common xml
            inflater.inflate(R.layout.navigation_bar, this);
        }
    }
    

    In my class hookupButtons does exactly what you think it would do. 🙂

    Then my in all my layout xmls look similar to this:

    <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <com.dragonglobal.dragonplayer.ui.widgets.NavigationBar
            android:id="@+id/nav_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ListView
            android:id="@+id/list_of_playlists"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    In the onCreate of each activity I also have this (that you can adapt to whatever you need):

    NavigationBar navbar = (NavigationBar) findViewById(R.id.nav_bar);
    navbar.setText("Playlists");
    

    Of course you will need to add whatever imports you need.

    EDIT

    Just a clarification: My navigation_bar.xml looks very similar to yours except I don’t have the merge lines in mine.

    EDIT 2

    Here is what hookupButtons looks like. I’m only showing one button but you should get the idea.

    private void hookupButtons(final Context context) {
        ImageButton playlistsBtn = (ImageButton)findViewById(R.id.nav_playlists_btn);
        if (context instanceof PlaylistsActivity) {
            playlistsBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.nav_playlists_active));
        } else {
            playlistsBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(context, PlaylistsActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    context.startActivity(i);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to turn a string into a char list list and I have
im trying to turn this jquery script into a toggle event type, thier is
I am trying to turn my array into an arraylist and I am having
I am trying to turn off my Intel AMT 2.0 Blade through RemoteControlService.wsdl but
I'm trying to turn Etags off on iis 6 since I'm load ballancing multiple
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I'm working with QGraphicsView/Scene for the first time. This is in PyQt, but except
I'm trying to turn the following ResultSet Meetings Covers Date TypeName 1 3 2010-10-14
Trying to setup an SSH server on Windows Server 2003. What are some good

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.