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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:56:36+00:00 2026-05-28T19:56:36+00:00

I have 2 buttons to switch between 2 layouts in the same activity: clicking

  • 0

I have 2 buttons to switch between 2 layouts in the same activity: clicking button1 on layout1, it goes to layout 2 (using setContentView). On layout2, clicking button2, it goes back to layout1. Then button1 is no longer responding OnClickListener. I looked into “Input Events” but still couldn’t figure it out. What happened and how to fix it?

Thanks in advance!

Button submitBtn;
Button backBtn;
submitBtn = (Button)findViewById(R.id.button1); //on layout1
backButn = (Button)findViewById(R.id.button2);  //on layout2
submitBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          setContentView(R.layout.layout2);
                    }
    });
backBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          setContentView(R.layout.layout1);
                   }
    });
  • 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-28T19:56:36+00:00Added an answer on May 28, 2026 at 7:56 pm

    You should re-assign listeners when you switch layout, cause when you’re calling setContentView old view is destroyed, and new components are created.

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

Sidebar

Related Questions

In order to switch between views in my iOS app, I have been using
I have three views that I switch between using a tab bar, and the
I'm using a CardLayout to switch between full-screen panels that have several nodes. To
My app has 5 different relative layouts that it can switch between by using
I have an app with two screens, and buttons that switch between them. One
I want to have buttons that kind of act like tabs - they switch
I have two buttons with the same ID: <button type=submit onclick=//do something id=theID>button 1</button>
I am using a UINavigationController to switch between views. What I would like is
I have an element and multiple styles, how do I switch between the styles
I have seen the post for How to switch views by buttons on iPhone?

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.