0

i have different layout design for portrait mode and a different looking layout for landscapre mode.. inside 'res' folder, i created a new folder called 'land-layout', where i created the file with the same name as inside 'layout'.

i have the textview (counter), which restores the value, when the orientation changes.. But the problem i am facing is, when the orientation goes from portrait to landscape, the design still continues to be the same as that was in portrait mode.. i wanted to know, how do i handle it ?? i'd be grateful. Thanks.

MainActivity.java :

public class MainActivity extends Activity {

private int sum = 0;
Button add, sub;
TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.textView1);

    add = (Button) findViewById(R.id.button1);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            sum++;
            tv1.setText("Incrementation = " + sum);
        }
    });
    sub = (Button) findViewById(R.id.button2);
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            sum--;
            tv1.setText("Decrementation = " + sum);
        }
    });
}  
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// setContentView(R.layout.activity_main);
Log.v("message", "ORIENTATION_LANDSCAPE");

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.v("message", "ORIENTATION_PORTRAIT");
}

}


@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putInt("sum", sum);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    int x = savedInstanceState.getInt("sum");
}

}

res\layout-land\activity_main.xml(for landscape mode)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/backg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="total is = 0"
    android:layout_gravity="center"
    android:textSize="40sp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button
    android:id="@+id/button1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:text="ADD" />

<Button
    android:id="@+id/button2"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center"
    android:text="Sub" />
</LinearLayout>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

res\layout\activity_main.xml(for portrait mode) :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/backg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:freezesText="true"
    android:text="total is = 0"
    android:textSize="40sp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="ADD" />

<Button
    android:id="@+id/button2"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Sub" />

</LinearLayout>

in manifest i added : android:configChanges="orientation|screenSize" (how do i use onConfigurationChanged() to handle the layout ? )

<activity
        android:name="com.example.apptest.MainActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.apptest.MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
ligi
  • 39,001
  • 44
  • 144
  • 244
rattlehead
  • 57
  • 9
  • 1
    If you want the ui to change then remove config changes, then on orientation change, the landscape ui will be drawn – Rahul Gupta Sep 24 '14 at 11:36
  • when i do that, the TextView, which acts as a counter onClick of a button, is reset to 0 when there is change of orientation. – rattlehead Sep 24 '14 at 11:41
  • @user3755885 : In that case save and access the TextView value as SharedPreferences. – Ed_Fernando Sep 24 '14 at 11:43
  • use onSaveInstanceState method for saving your counter and at onCreate just take the value from saveInstateState. Here's [a link](http://developer.android.com/training/basics/activity-lifecycle/recreating.html) how to do it! – ovluca Sep 24 '14 at 11:46
  • use saveinstance and restoreinstance methods for saving and retrieving activity state – Rahul Gupta Sep 24 '14 at 11:47
  • Appreciate your quick responses.. yes, i have done that exact same thing, and that helped me save and restore the value when the orientation changes.. but the layout design also continues to be the same then.. so how do i bridge, the changing value of textview, with the new layout design, is what i am looking for.. – rattlehead Sep 24 '14 at 12:02

1 Answers1

0

When you write this

android:configChanges="orientation|screenSize"

in the manifest : it means that you will handle orientation change yourself. So write something like this to handle configuration change.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_main);
    ...
}

Note that doing this : you will inflate the new layout from xml. So new Views will be created and the content of previous views will be lost.

For a better user experience, you need to transfer the content from the old view in the new view :

Something like this :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    CharSequence tmp = ((TextView)findViewById(R.myId)).getText();
    setContentView(R.layout.activity_main);
    ((TextView)findViewById(R.myId)).setText(tmp);
    ...
}

BUT Android provides useful hook to write all of this in a better way.

remove this line from the manifest

android:configChanges="orientation|screenSize"

and use :

@Override
protected void onSaveInstanceState(Bundle outState) {
    //here store your data in outState
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState!=null){
       //at this point savedInstanceState contains everything that was stored in outState.
    }
ben75
  • 29,217
  • 10
  • 88
  • 134
  • Hey ! Thanks ! now the desired layout design is acheieved whenever the layout is changed.. but now the bug is, when the counter is incremented, and the orientation is changed from portrait to landscape, the value displayed on that textview is correct, but when click the button and i try to increment or decrement, the counter starts counting from 0 again.. and again when i go back to portrait mode, after incrementing or decrementing, everything is reset back to beginning(0).. Appreciate your response though ! thanks ! – rattlehead Sep 24 '14 at 12:35
  • Welcome on SO. If the answer solved the question, please accept it. If you have another problem, please ask another question. http://stackoverflow.com/tour – ben75 Sep 24 '14 at 12:41
  • sure ! sorry i forgot about it earlier..it kinda solved my problem..i'll look into those bugs, if it still persists, will get back.. thanks though for assisting :) – rattlehead Sep 24 '14 at 15:41