4

I have two EditTexts and one CheckBox and a Button in my layout in the above order. After entering the values to the EditText, the user has to accept terms and conditions by clicking the Checkbox. I need to remove focus from EditTexts after the checkbox is clicked. Right now the focus is always on the second EditText. How can this be achieved? Please help. Layout:

  <EditText
      android:id="@+id/pm_et_customer_id"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:hint="@string/customer_id_hint"
      android:fontFamily="sans-serif-light"
      android:gravity="center"
      android:singleLine="true"
      android:layout_centerHorizontal="true"
      android:inputType="phone"
      android:textAppearance="?android:attr/textAppearanceMedium"/>

  <RelativeLayout
      android:id="@+id/pm_rl_mob_no_widget"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="6dp"
      android:layout_below="@id/pm_et_customer_id"
      android:gravity="center_horizontal">

    <EditText
        android:id="@+id/pm_et_dial_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/pm_et_msisdn"
        android:layout_marginLeft="5dp"
        android:ems="3"
        android:fontFamily="sans-serif-thin"
        android:gravity="center"
        android:text="@string/plus_nine_one"
        android:textAppearance="?android:attr/textAppearanceMedium"
    />

    <EditText
        android:id="@+id/pm_et_msisdn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/pm_et_dial_code"
        android:ems="9"
        android:fontFamily="sans-serif-light"
        android:gravity="center_horizontal"
        android:hint="@string/msisdn_hint"
        android:inputType="phone"
        android:maxLength="14"/>
  </RelativeLayout>

  <Button
      android:id="@+id/pm_bt_proceed"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/lay_t_c"
      android:layout_centerHorizontal="true"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
      android:layout_marginTop="8dp"
      android:fontFamily="sans-serif-light"
      android:text="@string/bt_label_proceed"/>

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/pm_rl_mob_no_widget"
      android:id="@+id/lay_t_c"
      android:layout_marginTop="10dp"
      android:gravity="center"
      android:orientation="horizontal">
    <CheckBox
        android:id="@+id/pm_check_t_and_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-thin"
        android:textSize="14sp"
        android:text="@string/pm_label_accept"/>

    <TextView
        android:id="@+id/pm_tv_t_and_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="4dp"
        android:layout_marginEnd="4dp"
        android:paddingLeft="4dp"
        android:paddingStart="4dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/pm_label_t_and_c"
        android:textSize="14sp"/>
  </LinearLayout>

</RelativeLayout>
Alexi V.
  • 143
  • 1
  • 2
  • 10
Nabeel K
  • 5,938
  • 11
  • 38
  • 68

4 Answers4

2

Using onCheckedChangeListener of Check Box you can do this. Just apply this on your Activity's onCreate()

chb1 = (CheckBox) findViewById(R.id.pm_check_t_and_c);
        chb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (chb1.isChecked()) {
                    edt1.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(edt1.getWindowToken(), 0);
                } else {
                    edt1.requestFocus();
                }

            }
        });
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
1
checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editText.setFocusable(false);
        }
    });

Add above OnClickListener to check box.

1
checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editText.setFocusable(false);
        }
    });

After removing focus, please do one more..

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56
-3
editText.setEnabled(false);

Use it on Checkbox listner it worked for me

Haroon
  • 497
  • 4
  • 13