Skip to content Skip to sidebar Skip to footer

Android - Ontextchanged() Called On When Phone Orientation Is Changed

I tried to implement search using EditText. whenever a text is typed in the EditText request is sent with the typed text in onTextChanged() method. When I change the orientation of

Solution 1:

I've got this problem just. So I moved addTextChangedListener to the post method of EditText in the onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ...

    EditTextmSearchQuery= findViewById(R.id.search_query);
    mSearchQuery.post(newRunnable() {
            @Overridepublicvoidrun() {
                mSearchQuery.addTextChangedListener(newTextWatcher() {
                    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
                        //Some stuff
                    }

                    @OverridepublicvoidafterTextChanged(Editable s) {
                    }
                });
            }
        });
}

Solution 2:

You need to override onConfigurationChanged method to get callback whenever orientation is getting changed.

@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // landscape
    } elseif (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        // portrait
    }
}

Add below line in manifest

android:configChanges= "orientation"

Now based on the callback you can do whatever you wanted to do.

Post a Comment for "Android - Ontextchanged() Called On When Phone Orientation Is Changed"