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 7969371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:13:36+00:00 2026-06-04T07:13:36+00:00

I have two Fragment s in my Activity : fragment A with button X

  • 0

I have two Fragments in my Activity: fragment A with button X and fragment B with button Y.

How can I change button X‘s background image when I click button B? Is it possible?

  • 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-06-04T07:13:37+00:00Added an answer on June 4, 2026 at 7:13 am

    From the documentation,

    Because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.

    That being said, what you want to do is create event callbacks to the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary. This is the recommended way to share events between two separate Fragments–that is, sharing the event through the activity.

    Check out the link above… it provides a couple nice examples. If you are still having trouble, let me know and maybe I can be more explicit.


    Edit #1:

    Let’s say you click a button in fragment A and you want this to cause changes to a button in fragment B. Here’s some sample code illustrating the concept:

    The callback interface:

    public interface OnButtonClickedListener {
        public void onButtonClicked(); 
    }
    

    The activity:

    public class SampleActivity extends Activity implements OnButtonClickedListener {
    
        /* Implementation goes here */     
    
        public void onButtonClicked() {
            // This method is called from fragment A, and when it is called,
            //   it will send information to fragment B. Remember to first
            //   check to see if fragment B is non-null.
    
            /* Make call to a method in fragment B that will update its display */
        }
    } 
    

    Fragment A:

    public class FragmentA extends Fragment {
        OnButtonClickedListener mListener;
    
        /* Implementation goes here */
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mListener = (OnButtonClickedListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement OnButtonClickedListener ");
            }
        }
    
        public void clickButton() {
            // When the button is clicked, notify the activity.
            //   The activity will then pass the information to fragment
            //   B (if it has been created).
    
            mListener.onButtonClicked();
        }
    }
    

    Edit #2:

    Now, you might be wondering, “Why would anyone ever go through all of this trouble? What’s the point of creating a separate activity callback method when you could just have fragment A directly manipulate fragment B?”

    The main reason you want to do this is to ensure that each fragment is designed as a modular and reusable activity component. This is especially important because a modular fragment allows you to change your fragment combinations for different screen sizes. When designing your application to support both tablets and handsets, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset, it might be necessary to separate fragments to provide a single-pane UI when more than one cannot fit within the same activity. Making use of activity callbacks ensures that you will easily be able to reuse your fragments in situations where fragment B is not visible on the screen. For example, if you are on a handheld device and there is not enough room to display fragment B, then you can easily have your activity check to see if fragment B is currently being shown on the screen.

    Sorry if this isn’t clear… I’m finding it difficult to describe :P. Working your way through this tutorial might help… Activity callbacks make your life especially easier as a developer when you are working with interactive multi-pane layouts.

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

Sidebar

Related Questions

I need your help. I have one Activity with two fragments: one fragment with
I have two fragments in an activity where one fragment takes up 70% and
I have an activity made up of two fragments. On the left is a
I have two fragments in an activity: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=horizontal android:id=@+id/mainlayout android:layout_width=fill_parent android:layout_height=fill_parent> <fragment
Suppose I have two fragments: firstFragment , and secondFragment I know I can replace
I have an activity with two fragments: one for showing products in a grid
I have create a sample Fragment application. I have two fragments like list fragment
I have two fragments, one a ListView and one a details view. Each fragment
I have two textures generated using a fragment shader. I want to be able
I have a HTML fragment which contains two anchor tags in various parts of

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.