1

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

nick
  • 121
  • 1
  • 2
  • 10

2 Answers2

3
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.

Gaurav Gupta
  • 4,228
  • 1
  • 31
  • 59
  • i follow this way like what you did. Yes the layout is changed but the data still empty. please advice – nick Sep 20 '13 at 06:57
  • @nick If you are using some D.S like arraylist/any thing else, you can reuse that, since data will be there and you can repopulate the views with that data. In my case i had collection of Business Objects and that were preserved on orientation changed.So, i reuse that collection and repopulated my view. If you find some better approach please do share. – Gaurav Gupta Sep 20 '13 at 08:28
  • if i wants to change only the layout but without repopulate the whole set of data is it posible? – nick Sep 20 '13 at 08:31
  • Use `inflater.inflate(...)` to change the layout. You may have to remove all the views using `((ViewGroup) getView()).removeAllViews();` and then add the returned View that is returned by `inflate(...)`, some thing like `((ViewGroup)getView()).addView(view);` – Gaurav Gupta Sep 20 '13 at 08:39
  • yes i try this layout is changed if portrait get from layout folder if landscape get from layout-land folder. But no data loaded. empty layout – nick Sep 20 '13 at 08:57
  • It is expected behavior, because you have inflated the layout. It is still a new layout. No you have to feed data into these layout. – Gaurav Gupta Sep 20 '13 at 09:03
  • because my layout having listview that scroll to certain position so if a new layout then reload evrything and i need to fix back to the original position after rotation. no others solution beside reload the data? – nick Sep 20 '13 at 09:13
  • AFAIK this is only possible way out. but if you want to retain the position of listview as well then http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview may help you. – Gaurav Gupta Sep 20 '13 at 09:31
  • simply inflating my layout did the job :) – Jibяaᴎ Khaᴎ Jan 23 '15 at 17:18
0

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.

Pradip
  • 2,993
  • 3
  • 20
  • 27
  • thanks all it work for me with this link http://stackoverflow.com/questions/17116602/using-onconfigurationchanged-in-a-fragment – nick Sep 12 '13 at 06:55