user1545066
user1545066

Reputation:

only change layout color while orientation change android

I have create dialog box which will be displayed first when i start application (which is coded in onCreate method ) and then question and answer will be displayed on textview

so to solve problem of orientation (so dialog box again not displayed when orientation change) i have used Manifest with:

android:configChanges="keyboardHidden|orientation"

I have res/layout-land, res/layout-port

but to change only COLOR of background

I have used onConfigurationChanged in my Activity (it get's called on rotation).

so now when orientation change the dialog box will not appear again and background is redrawn but the question and answer which is initialized on onCreate() will not display

so how to maintain

Upvotes: 0

Views: 1023

Answers (4)

Sourav Roy
Sourav Roy

Reputation: 323

Did you use onSaveInstanceState to save the data and onRestoreInstanceState for retriving the data? If you have done this then you can display the same dialog box in onRestoreInstanceState.

Well I have a question for you. How did you kept the background color unchanged when changing the orientations? And what to do if the color is unknown or a random color?

Upvotes: 0

Khan
Khan

Reputation: 7605

In OnCreate add for your main layout as

 LinearLayout lv=(LinearLayout)findViewById(R.id.mainlayout);
  lv.setBackgroundColor(android.R.color.black);  // for default potrait or landscape view and after that add this in that activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
  Configuration c = getResources().getConfiguration();

  if(c.orientation == Configuration.ORIENTATION_PORTRAIT ) {
    // portrait
     lv.setBackgroundColor(android.R.color.black);
  } else if(c.orientation == Configuration.ORIENTATION_LANDSCAPE ){
    // landscape
    lv.setBackgroundColor(android.R.color.white);
  }
}

and also add this in menifest in your activity defined

 android:configChanges="keyboardHidden|orientation"

Upvotes: 0

Nermeen
Nermeen

Reputation: 15973

Using android:configChanges="keyboardHidden|orientation" means that your activity handles a configuration change itself, and the system will not change the layout.. take a lokk at http://developer.android.com/guide/topics/resources/runtime-changes.html

To reset the text view, in case of not using android:configChanges="keyboardHidden|orientation":

@Override
public Object onRetainNonConfigurationInstance() {
    final MyDataObject data = collectMyLoadedData();
     //here get the text from the text view, and any other info
    return data;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
    if (data != null) {
       //get the text and set it in the text view
    }

}

Upvotes: 1

Victor
Victor

Reputation: 193

When you change the orientation of the devide, android continue the execution without calling onCreate again, but if you put the code in onResume, this code will executed again.

Upvotes: 0

Related Questions