1

I create two xml files, one in activity_main other is activity_main_land. Both have 2 EditText fields. Both have ids and in both XML ids are same. When onConfigchange() method is called I change XML file. Now when I rotate device EditText lost the data. Can you Please help me to get rid this problem.

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Toast.makeText(MainActivity.this, "simple", Toast.LENGTH_SHORT).show();
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
        Toast.makeText(MainActivity.this, "Landscape", Toast.LENGTH_SHORT).show();

        setContentView(R.layout.activity_main_land);
    }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(MainActivity.this, "portreate", Toast.LENGTH_SHORT).show();

        setContentView(R.layout.activity_main);
    }

I also tried reference of:

How to retain EditText data on orientation change?

Community
  • 1
  • 1
Shivang
  • 935
  • 8
  • 18

2 Answers2

5
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
...
...
String userName, password;
if(savedInstanceState!=null)
{
    userName = savedInstanceState.getString("user_name");
    password= savedInstanceState.getString("password");
}

if(userName != null)
    userNameEdtTxt.setText(userName);
if(password != null)
    passEdtTxt.setText(password);
}
....

@Override
protected void onSaveInstanceState (Bundle outState)
{
    outState.putString("user_name", userNameEdtTxt.getText().toString());
    outState.putString("password",  passEdtTxt.getText().toString());
}
johnrao07
  • 6,690
  • 4
  • 32
  • 55
1

Try this its working fine for me.

In your both xml file keep edittexts id same,

public class MainActivity extends Activity {


    EditText userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userName = (EditText)findViewById(R.id.edtUserName);
    }

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

        String storedUsername = userName.getText().toString();

        // landscape mode
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.activity_main);
            userName = (EditText) findViewById(R.id.edtUserName);
        }else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            setContentView(R.layout.activity_main1);
            userName = (EditText) findViewById(R.id.edtUserName);
        }
        // set username
        userName.setText(storedUsername);
    }

}
  • Its working but, In case of many Edit text ( more than 10 ) then it will increase the length of code. Is there any way to optimize it. – Shivang Nov 26 '15 at 07:07
  • You can use Hashmap, HashMap loginDetails = new HashMap(); loginDetails.put("username",userName.getText().toString()); loginDetails.put("password", password.getText().toString()); loginDetails.put("confirmPassword",confirmPassword.getText().toString()); –  Nov 30 '15 at 04:12