Edittext automatically gets focus when moving between pages in the pager view

I have an activity that contains two fragments inside the presentation pager. I used the same layout to inflate these fragments. The layout has two editing texts located inside the linear layout, which is inside the relative layout. The problem is that when I switch from fragment A to fragment B, it first edits the text with focus in fragment A, and when I return from fragment B to fragment A, instead of the first editing text that has focus, the second editing text gets focus. How to solve it. I provide layouts and source code below. I do not return code inside fragment classes.

Activity:

public class LoginActivity extends BaseActivity {
    public static final String selectedTabPosition = "selectedTabPosition";
    //Tab tag name
    private static String TAB_1_TAG = "Email";
    private static String TAB_2_TAG = "Mobile";
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        TAB_1_TAG = getResources().getString(R.string.tab_email);
        TAB_2_TAG = getResources().getString(R.string.tab_mobile);

        //Initialise views
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);

        //set tab with view pager
        setupViewPager(mViewPager);
        mTabLayout.setupWithViewPager(mViewPager);
        setupTabIcons();

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                dismissKeyboard(mViewPager);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * Adding custom view to tab
     */
    private void setupTabIcons() {
        LinearLayout tabOne = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
        TextView tvIconOne = (TextView) tabOne.findViewById(R.id.tv_tab_title);
        tvIconOne.setText(TAB_1_TAG);
        mTabLayout.getTabAt(0).setCustomView(tabOne);
        setTypeface(tvIconOne, CustomFonts.Prime_regular);
        mTabLayout.getTabAt(0).getCustomView().setSelected(true);

        LinearLayout tabTwo = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
        TextView tvIconTwo = (TextView) tabTwo.findViewById(R.id.tv_tab_title);
        tvIconTwo.setText(TAB_2_TAG);
        setTypeface(tvIconTwo, CustomFonts.Prime_regular);
        mTabLayout.getTabAt(1).setCustomView(tabTwo);
    }

    /**
     * Adding fragments to ViewPager
     *
     * @param viewPager The view pager
     */
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        LoginFragment loginFragmentEmail = new LoginFragment();
        Bundle emailBundle = new Bundle();
        emailBundle.putInt(selectedTabPosition, 0);
        loginFragmentEmail.setArguments(emailBundle);
        LoginFragment loginFragmentMobile = new LoginFragment();
        Bundle phoneBundle = new Bundle();
        phoneBundle.putInt(selectedTabPosition, 1);
        loginFragmentMobile.setArguments(phoneBundle);
        adapter.addFrag(loginFragmentEmail, TAB_1_TAG);
        adapter.addFrag(loginFragmentMobile, TAB_2_TAG);
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Fragment:

    public class LoginFragment extends BaseFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_login, container, false);
                return rootView;

}

activity_login.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_title"
        android:layout_width="200dp"
        android:layout_height="45dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/custom_tab_layout_height"
        android:layout_below="@+id/iv_title"
        android:layout_marginTop="@dimen/spacing_10"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/app_color_dark"
        app:tabMode="fixed"/>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/divider_height_small"
        android:layout_below="@+id/tabs"
        android:background="@color/gray_medium"/>

    <com.helper.CustomNonSwipeableViewpager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/view"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</RelativeLayout>

fragment_login.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white"
                android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spacing_50"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="@dimen/spacing_48"
            />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="@dimen/spacing_48"
            android:layout_marginTop="@dimen/spacing_15"/>

    </LinearLayout>

</RelativeLayout>

AndroidManifest.xml:

 <activity
    android:name=".activities.LoginActivity"
    android:screenOrientation="portrait"
    />

AndrodManifest,

android: windowSoftInputMode = stateHidden and android: windowSoftInputMode = adjustPan

CustomNonSwipeableViewpager.java:

    public class CustomNonSwipeableViewpager extends ViewPager {

    public CustomNonSwipeableViewpager(Context context) {
        super(context);
    }

    public CustomNonSwipeableViewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }
}

ScreenShots: FragmentB:

Before going to FragmentB.

B: In fragment B.

A B:

enter image description here

+4
3

requestFocus editText, editText .

fragment_login.xml ,

<EditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="@dimen/spacing_48">

        <requestFocus />
</EditText>
0

EditText :

EditText et_email_view = (EditText) rootView.findViewById(R.id.et_email);
et_email_view.setFocusable(false);
0

, viewPager editText. translationZ editText (translationZ="2") viewPage (translationZ="1"), .

0
source

All Articles