Shri
Shri

Reputation: 159

Edittext disappears when i change screen orientation?

Thanks in advance.

This is my sample code. Whenever i try to run it and change the screen orientation both the edit text disappears and i have to restart the application. please help me out

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ed1=(EditText)findViewById(R.id.ed1);
    ed2=(EditText)findViewById(R.id.ed2);

    ed1.addTextChangedListener(new TextWatcher()                          
    {
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
        }

        public void afterTextChanged(Editable arg0) {    
            ed2.setVisibility(View.INVISIBLE);
        }
    }); 

    ed2.addTextChangedListener(new TextWatcher()            
    {
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }    

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
        }

        public void afterTextChanged(Editable arg0) {   
            ed1.setVisibility(View.INVISIBLE);
        }
    });
}

This is my main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
    android:id="@+id/table"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="25dip"
    android:stretchColumns="0,1" >

    <TableRow>

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:text="Edit Text 1"
            android:textSize="15dip" />

        <EditText
            android:id="@+id/ed1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginTop="20dip"
            android:maxLength="10"
            android:numeric="integer|decimal" />
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/or_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="OR"
            android:textSize="20dip" />
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:text="Edit Text2"
            android:textSize="15dip" />

        <EditText
            android:id="@+id/ed2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginTop="20dip"
            android:maxLength="10"
            android:numeric="integer|decimal" />
    </TableRow>
</TableLayout>

Upvotes: 1

Views: 3387

Answers (3)

Arif Nadeem
Arif Nadeem

Reputation: 8604

Every time you rotate your device your activity restarts, that is why you do not see your EditTexts.

Add this line of XML to your activity in Manifest file

android:configChanges="orientation|keyboardHidden"

Also check this out to know more about Runtime changes to your applications.

Upvotes: 2

Taras Feschuk
Taras Feschuk

Reputation: 699

Hi, try to add these lines to the end of the 'onCreate' method

ed1.setVisibility(View.VISIBLE);
ed2.setVisibility(View.VISIBLE);

Updated:

Try this, maybe it will help you.

public class MyActivity extends Activity {
    [...]
    private final boolean isEd1FirstChange = true;
    private final boolean isEd2FirstChange = true;
    [...]

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        isEd1FirstChange = true;
        isEd2FirstChange = true;

        ed1=(EditText)findViewById(R.id.ed1);
        ed2=(EditText)findViewById(R.id.ed2);

        ed1.addTextChangedListener(new TextWatcher()                          
        {
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void afterTextChanged(Editable arg0) {    
                if (isEd1FirstChange) {
                    isEd1FirstChange = false;
                    return;
                }

                ed2.setVisibility(View.INVISIBLE);
            }
        }); 

        ed2.addTextChangedListener(new TextWatcher()                          
        {
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void afterTextChanged(Editable arg0) {    
                if (isEd2FirstChange) {
                    isEd2FirstChange = false;
                    return;
                }

                ed1.setVisibility(View.INVISIBLE);
            }
        });

}

Upvotes: 0

Behl&#252;l
Behl&#252;l

Reputation: 3452

it seems that void afterTextChanged(Editable) method is called on your TextWatchers. In any other method, for example onResume() maybe, do you set text of your EditTexts programmatically? This might be the cause for it. If this is the case, just make them visible again in the place that you restore your previous state.

Upvotes: 0

Related Questions