nick
nick

Reputation: 131

orientation change getting the same layout in fragment

i'm doing orientation change from portrait to landscape and from landscape to portrait. My code as below

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    //Set titlebar as "my@heaven"
    getSherlockActivity().getSupportActionBar()
    .setBackgroundDrawable(getResources().getDrawable(R.drawable.myheaven));

    view = inflater.inflate(R.layout.memorial_listing, container, false);   

    setUpView(savedInstanceState);

    return view;
}

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {


    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

    }

  }

and i also add this to manifest

android:configChanges="orientation|keyboardHidden|screenSize"

and the same layout i put inside layout folder for portrait and layout-land for landscape but when i try to rotate the screen if start from portrait then still remain getting layout from layout folder. The screen rotate but not getting right folder to display the layout. Please help

Upvotes: 1

Views: 1859

Answers (2)

Gaurav Gupta
Gaurav Gupta

Reputation: 4691

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

    // Get a layout inflater (inflater from getActivity() or getSupportActivity() works as well)
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(// new orientation is potrait){
        // load potrait layout
    }else{
       // load landscape layout
    }
}

This method receives callback when orientation is change. So inflate layout on receiving callback.

P.S: both the layout should have different names. Do not put landscape layout in layout-land folder. When i faced this issue, i had to pull my layout from layout-land and i put it in layout folder. Although this is not good practice, but in my case it worked that way only.

Upvotes: 3

Pradip
Pradip

Reputation: 3177

Below works for me,

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

    Log.e("On Config Change","LANDSCAPE Mode");
}else{

    Log.e("On Config Change","PORTRAIT Mode");
}

Can you please check with this.

Upvotes: 0

Related Questions