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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:55:29+00:00 2026-05-30T05:55:29+00:00

I have problems with fragment when the screen orientation changes. In my code, I

  • 0

I have problems with fragment when the screen orientation changes.
In my code, I have create different layout xml according to the screen orientation.
For example, I have header_landscape.xml and header_portrait.xml in the layout directory. And each different header has the same fragment in the linear layout. So when I turn my device, I have the error “duplicate id …” that corresponding to my fragment.

The differences between layout is the content.When I am in “landscape” I display more informations than I am in “portrait”.

On create of my activity :

 setContentView(R.layout.main_landscape); 

                //header
                date=(TextView)findViewById(R.id.headerLandscapeDate);
                routeSens=(TextView) findViewById(R.id.headerLandscapeRouteSens);
                pkHeader=(TextView) findViewById(R.id.headerLandscapePk);

                //Récupération de la listview créée dans le fichier main.xml
                maListViewPerso = (ListView) findViewById(R.id.ListeChoix);      

                //On attribut à notre listView l'adapter que l'on vient de créer
                maListViewPerso.setAdapter(chargeMenu());

$ The code of the modification of layout when the orientation screen changes.

 public void onConfigurationChanged(Configuration newConfig)
 {
        super.onConfigurationChanged(newConfig);


        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE && isLoaded && oldScreenStateOrientation!=newConfig.orientation)
        {

            setContentView(R.layout.main_landscape);

            //header
            date=(TextView)findViewById(R.id.headerLandscapeDate);
            routeSens=(TextView) findViewById(R.id.headerLandscapeRouteSens);
            pkHeader=(TextView) findViewById(R.id.headerLandscapePk);

            maListViewPerso = (ListView) findViewById(R.id.ListeChoix);  
            //On attribut à notre listView l'adapter que l'on vient de créer
            maListViewPerso.setAdapter(chargeMenu());
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT && isLoaded && oldScreenStateOrientation!=newConfig.orientation)
        {

            setContentView(R.layout.main_portrait);


            routeSens=(TextView) findViewById(R.id.headerPortraitRouteSens);
            pkHeader=(TextView) findViewById(R.id.headerPortraitPk);

            maListViewPerso = (ListView) findViewById(R.id.ListeChoix);  
            //On attribut à notre listView l'adapter que l'on vient de créer
            maListViewPerso.setAdapter(chargeMenu());
        }
      }

If anyone have a solution.
I developp with the api8 (compatibility library fragment).

Sorry for my english.

Thanks

  • 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-30T05:55:31+00:00Added an answer on May 30, 2026 at 5:55 am

    You should let android do all that configuration switching for you! Don’t do anything in “onConfigurationChanged”, remove all configuration changes from your manifest even!

    Put your portrait layout in
    layout/ folder

    and put your landscape layout in
    layout-land/ folder

    Make sure they are both named: main.xml (or anything, as long as it is the same)

    Then in your activity.onCreate, do something like this:

        // Will automatically select the correct layout
        setContentView(R.layout.main); 
    
        //header
        // If this is in portrait, date will be null. Check for that later
        date=(TextView)findViewById(R.id.headerDate);
        routeSens=(TextView) findViewById(R.id.headerRouteSens);
        pkHeader=(TextView) findViewById(R.id.headerPk);
    
        //Récupération de la listview créée dans le fichier main.xml
        maListViewPerso = (ListView) findViewById(R.id.ListeChoix);      
    
        //On attribut à notre listView l'adapter que l'on vient de créer
        maListViewPerso.setAdapter(chargeMenu());
    

    This way, you can let android worry about which configuration to use. If you only want to display the landscape view on large screens, you could put it in a folder such as layout-w1024dp. This way you can have multiple layouts very easily.

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

Sidebar

Related Questions

I have problems getting some of my views aligned in a relative layout that
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
I have a fragment interface with tabs along the bottom which open different fragments
For some reasons I can't use xml layout files. But I need to create
I have application which uses fragments. Depending on screen size and orientation I'm displaying
I have problems with bringing a windows mobile 6 form to the front. I
I have problems binding both a telerik RadGrid and a plain vanilla ASP.NET GridView
I have problems with how I should structure my product listing pages, products pages,
I have problems with dealing with widows within a multicols environment, that is, I
I have problems to get rspec running properly to test validates_inclusion_of my migration looks

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.