1

I want to deselect the TextInputLayout when the user presses the back button or cancels the option menu edit item. thus, as you see in pictures, the TextInputLayout doesn't deselect after the edit failed or canceled. I tried different methods and approaches but no one can help.

pic - 1 pic - 2 pic - 3

in addition, I called the

mFragmentProfileBinding.fragmentProfileNameTextInputLayout.clearFocus();

but i also didn't work.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools">

   <data>

       <variable
           name="viewModel"
           type="ir.basamadazmanovin.heartrate.ui.main.profile.ProfileViewModel" />

   </data>


   <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
       android:id="@+id/activity_login_swipe_refresh_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:refreshing="@{viewModel.isLoading}">

       <androidx.core.widget.NestedScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent">


           <androidx.constraintlayout.widget.ConstraintLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="@color/md_grey_200">

               <androidx.cardview.widget.CardView
                   android:id="@+id/fragment_profile_pro_cardView"
                   android:layout_width="0dp"
                   android:layout_height="0dp"
                   android:layout_marginStart="8dp"
                   android:layout_marginEnd="8dp"
                   app:layout_constraintBottom_toTopOf="@+id/guideline18"
                   app:layout_constraintEnd_toStartOf="@+id/guideline13"
                   app:layout_constraintStart_toStartOf="@+id/guideline12"
                   app:layout_constraintTop_toTopOf="@+id/guideline17">

                   <androidx.constraintlayout.widget.ConstraintLayout
                       android:layout_width="match_parent"
                       android:layout_height="match_parent">

                       <TextView
                           android:id="@+id/fragment_profile_account_status_textView"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="8dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           android:text="Free Account"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toStartOf="@+id/guideline21"
                           app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toTopOf="parent" />

                       <androidx.constraintlayout.widget.Guideline
                           android:id="@+id/guideline21"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:orientation="vertical"
                           app:layout_constraintGuide_percent="0.5" />

                       <mehdi.sakout.fancybuttons.FancyButton
                           android:id="@+id/fragment_profile_premium_btn"
                           android:layout_width="124dp"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginEnd="8dp"
                           android:onClick="@{()-> viewModel.onPremiumClick()}"
                           android:paddingStart="16dp"
                           android:paddingTop="8dp"
                           android:paddingEnd="16dp"
                           android:paddingBottom="8dp"
                           app:fb_borderColor="#FFFFFF"
                           app:fb_borderWidth="1dp"
                           app:fb_defaultColor="@color/button_color"
                           app:fb_focusColor="@color/button_color"
                           app:fb_radius="@dimen/button_radius"
                           app:fb_text="Premium"
                           app:fb_textColor="#FFFFFF"
                           app:fb_textSize="@dimen/button_textSize"
                           app:layout_constraintBottom_toBottomOf="@+id/fragment_profile_account_status_textView"
                           app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintStart_toStartOf="@+id/guideline21"
                           app:layout_constraintTop_toTopOf="@+id/fragment_profile_account_status_textView" />


                   </androidx.constraintlayout.widget.ConstraintLayout>
               </androidx.cardview.widget.CardView>

               <View
                   android:id="@+id/fragment_profile_view"
                   android:layout_width="0dp"
                   android:layout_height="0dp"
                   android:background="@color/material_blue400"
                   android:visibility="visible"
                   app:layout_constraintBottom_toTopOf="@+id/guideline19"
                   app:layout_constraintEnd_toEndOf="parent"
                   app:layout_constraintHorizontal_bias="1.0"
                   app:layout_constraintStart_toStartOf="parent"
                   app:layout_constraintTop_toTopOf="parent"
                   app:layout_constraintVertical_bias="0.675" />

               <de.hdodenhof.circleimageview.CircleImageView
                   android:id="@+id/fragment_profile_profile_ImageView"
                   android:layout_width="124dp"
                   android:layout_height="124dp"
                   android:layout_marginTop="8dp"
                   android:layout_marginEnd="8dp"
                   android:layout_marginBottom="8dp"
                   android:src="@mipmap/ic_launcher_round"
                   app:civ_border_color="@color/material_white"
                   app:civ_border_width="2dp"
                   app:layout_constraintBottom_toTopOf="@+id/guideline17"
                   app:layout_constraintEnd_toStartOf="@+id/guideline20"
                   app:layout_constraintStart_toStartOf="@+id/guideline20"
                   app:layout_constraintTop_toTopOf="@+id/guideline14"
                   app:layout_constraintVertical_bias="0.358" />

               <TextView
                   android:id="@+id/fragment_profile_profile_textview"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="8dp"
                   android:layout_marginBottom="8dp"
                   android:text="@{viewModel.profileModelObservableField.name}"
                   android:textColor="@color/material_white"
                   app:layout_constraintBottom_toTopOf="@+id/guideline17"
                   app:layout_constraintEnd_toEndOf="@+id/fragment_profile_profile_ImageView"
                   app:layout_constraintStart_toStartOf="@+id/fragment_profile_profile_ImageView"
                   app:layout_constraintTop_toBottomOf="@+id/fragment_profile_profile_ImageView"
                   tools:text="oy" />

               <androidx.cardview.widget.CardView
                   android:layout_width="0dp"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="8dp"
                   android:layout_marginBottom="8dp"
                   app:layout_constraintBottom_toBottomOf="parent"
                   app:layout_constraintEnd_toEndOf="@+id/fragment_profile_pro_cardView"
                   app:layout_constraintStart_toStartOf="@+id/fragment_profile_pro_cardView"
                   app:layout_constraintTop_toTopOf="@+id/guideline18">

                   <androidx.constraintlayout.widget.ConstraintLayout
                       android:layout_width="match_parent"
                       android:layout_height="match_parent">

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_name_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="8dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           android:focusable="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toTopOf="parent"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_name_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="First Name"
                               tools:visibility="visible" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_lastname_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="4dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="@+id/fragment_profile_name_textInputLayout"
                           app:layout_constraintStart_toStartOf="@+id/fragment_profile_name_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_name_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_lastname_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Last Name" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_year_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="4dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toStartOf="@+id/fragment_profile_month_textInputLayout"
                           app:layout_constraintHorizontal_bias="0.5"
                           app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_lastname_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_year_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Year" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_month_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginTop="4dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toStartOf="@+id/fragment_profile_day_textInputLayout"
                           app:layout_constraintHorizontal_bias="0.5"
                           app:layout_constraintStart_toEndOf="@+id/fragment_profile_year_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_lastname_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_month_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Month" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_day_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="4dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintHorizontal_bias="0.5"
                           app:layout_constraintStart_toEndOf="@+id/fragment_profile_month_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_lastname_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_day_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Day" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_description_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginTop="4dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="@+id/fragment_profile_day_textInputLayout"
                           app:layout_constraintStart_toStartOf="@+id/fragment_profile_year_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_month_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_description_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="128dp"
                               android:gravity="top"
                               android:hint="Describe Your Problems Here"
                               android:inputType="textMultiLine" />
                       </com.google.android.material.textfield.TextInputLayout>

                   </androidx.constraintlayout.widget.ConstraintLayout>
               </androidx.cardview.widget.CardView>

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline12"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   app:layout_constraintGuide_begin="8dp" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline13"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   app:layout_constraintGuide_end="8dp" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline14"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_begin="8dp" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline16"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.5" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline17"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.3" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline18"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.4" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline19"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.35" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline20"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   app:layout_constraintGuide_percent="0.5" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline22"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.45" />

           </androidx.constraintlayout.widget.ConstraintLayout>

       </androidx.core.widget.NestedScrollView>
   </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</layout>

public class ProfileFragment
       extends BaseFragment<FragmentProfileBinding,ProfileViewModel> implements ProfileNavigator {

   @Inject
   ViewModelProviderFactory factory;
   private ActionMode mActionMode;
   private FragmentProfileBinding mFragmentProfileBinding;
   private ProfileViewModel mProfileViewModel;


   public ProfileFragment() {
       // Required empty public constructor
   }



   public static ProfileFragment newInstance() {
       ProfileFragment fragment = new ProfileFragment();
       Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
       fragment.setArguments(args);
       return fragment;
   }

   @Override
   public int getBindingVariable() {
       return BR.viewModel;
   }

   @Override
   public int getLayoutId() {
       return R.layout.fragment_profile;
   }

   @Override
   public ProfileViewModel getViewModel() {
       mProfileViewModel= ViewModelProviders.of(this, factory).get(ProfileViewModel.class);
       return mProfileViewModel;
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mProfileViewModel.setNavigator(this);

   }



   @Override
   public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
       super.onViewCreated(view, savedInstanceState);
       mFragmentProfileBinding = getViewDataBinding();

       subscribeToProfile();
       deActiveEditTexts();
   }

   private void subscribeToProfile(){
       mProfileViewModel.getProfileModelMutableLiveData().observe(this,
               profileModel ->
                       mProfileViewModel.setProfileModelObservableField(profileModel));

   }



   @Override
   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.fragment_profile_menu, menu);
       super.onCreateOptionsMenu(menu, inflater);
   }


   @Override
   public boolean onOptionsItemSelected(@NonNull MenuItem item) {
       switch (item.getItemId()) {

           case R.id.fragment_profile_menu_edit:
               mActionMode = getBaseActivity().startSupportActionMode(new ProfileFragmentActionMode(this));
               activeEditTexts();
               return true;

           default:
               break;
       }

       return false;
   }

   private void setUp(){

   }
   @Override
   public void editProfileInfo() {
       deActiveEditTexts();
       setNullToActionMode();
       Toast.makeText(getActivity(),"oy",Toast.LENGTH_SHORT).show();

   }

   @Override
   public void cancelProfileInfo() {
       deActiveEditTexts();
       setNullToActionMode();
   }

   private void activeEditTexts(){
       mFragmentProfileBinding.fragmentProfileNameEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileLastnameEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileYearEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileMonthEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileDayEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileDescriptionEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileNameEditText.requestFocus();

   }
   private void deActiveEditTexts(){

       mFragmentProfileBinding.fragmentProfileNameTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileLastnameTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileYearTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileMonthTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileDayTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileDescriptionTextInputLayout.clearFocus();


       mFragmentProfileBinding.fragmentProfileNameEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileLastnameEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileYearEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileMonthEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileDayEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileDescriptionEditText.setEnabled(false);
   }

   //Set action mode null after use
   public void setNullToActionMode() {

       if (mActionMode != null){
           mActionMode.finish();
           mActionMode = null;
       }

   }


   @Override
   public void onBackPressed() {
//        deActiveEditTexts();
//        cancelProfileInfo();
   }

   @Override
   public void handleError(Throwable throwable) {

   }



}
Omid Erfanmanesh
  • 547
  • 1
  • 7
  • 29
  • Correct me If I am wrong. According to you question you want when layout open all edit text show deselect. right? – BlackBlind Apr 27 '19 at 09:54
  • Imagine the user want to edit his profile and press the edit from options menu. Here the edittext enabled. If the user cancels by press back button although the edittext disabled, the inputtextlayout still red – Omid Erfanmanesh Apr 27 '19 at 11:17

4 Answers4

1

Try to add a LinearLayout before TextInputLayout to your XML:

<LinearLayout
    android:id="@+id/thief_layout"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:clickable="true"
    android:layout_width="0px"
    android:layout_height="0px" />

Then add this code to your onBackPressed() Fragment:

editText.clearFocus();
thiefLayout.requestFocus();
Aref
  • 746
  • 10
  • 27
1

Try this

editText.setFocusableInTouchMode(false);

editText.setFocusable(false);

editText.setFocusableInTouchMode(true);

editText.setFocusable(true);
Android
  • 1,420
  • 4
  • 13
  • 23
1

Ok now you want to disable edit text

editTextName.setFocusable(false);
editTextName.setEnabled(false);

or when you want to enable edit text on user click

editTextName.setFocusable(true);
editTextName.setFocusableInTouchMode(true);
editTextName.setEnabled(true);
editTextName.requestFocus();

Hopefully it will help you.

BlackBlind
  • 772
  • 9
  • 26
0

You should use interface to use that you can not register in fragment directly example thread is here

Vignesh
  • 355
  • 1
  • 4
  • 17
  • It's no matter with onbackpress, the problem is the inputtextlayout is still red otter onbackpress called even though I called cancelfocus() – Omid Erfanmanesh Apr 27 '19 at 11:19