0

I don't know why I confuse with simple if/ else condition? In my app, phone verification part, Bydefault I set sendVerification button disable, and when I enter text then it should enable. but it is not enabling? What is wrong with my condition? even if I try with else part, same problem!

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PhoneAuthActivity">

    <include
        android:id="@+id/PhoneToolbar"
        layout="@layout/app_bar">
    </include>

    <LinearLayout
        android:id="@+id/DialLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/PhoneToolbar"
        android:orientation="horizontal"
        android:weightSum="10">

        <ImageView
            android:id="@+id/dial"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:src="@drawable/dial"
            android:layout_weight="1"/>

        <EditText
            android:id="@+id/PhoneNumber"
            android:layout_width="270dp"
            android:layout_height="wrap_content"
            android:hint="Phone Number"
            android:layout_weight="8"
            android:ems="10"
            android:inputType="phone"/>

        <ProgressBar
            android:id="@+id/PhoneProgress"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LockLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/DialLayout"
        android:orientation="horizontal"
        android:weightSum="10">

        <ImageView
            android:id="@+id/lock"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:src="@drawable/lock"
            android:layout_weight="1"/>

        <EditText
            android:id="@+id/code"
            android:layout_width="270dp"
            android:layout_height="wrap_content"
            android:hint="Verification Code"
            android:layout_weight="8"/>

        <ProgressBar
            android:id="@+id/CodeProgress"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </LinearLayout>

    <TextView
        android:id="@+id/VerificationText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="91dp"
        android:text="A verification code will be sent to your phone number" />

    <Button
        android:id="@+id/sendVerification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="23dp"
        android:backgroundTint="#FF0000"
        android:text="Send Verification" />

</RelativeLayout>

And this is Activity

package com.jimmytrivedi.lapitchat;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.google.firebase.FirebaseException;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class PhoneAuthActivity extends AppCompatActivity {

    private LinearLayout DialLayout, LockLayout;
    private EditText PhoneNumber, code;
    private ProgressBar PhoneProgress, CodeProgress;
    private Button sendVerification;
    private Toolbar PhoneToolbar;
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private String number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_auth);

        DialLayout = findViewById(R.id.DialLayout);
        LockLayout = findViewById(R.id.LockLayout);
        PhoneNumber = findViewById(R.id.PhoneNumber);
        code = findViewById(R.id.code);
        PhoneProgress = findViewById(R.id.PhoneProgress);
        CodeProgress = findViewById(R.id.CodeProgress);
        sendVerification = findViewById(R.id.sendVerification);
        PhoneToolbar = findViewById(R.id.PhoneToolbar);

        PhoneProgress.setVisibility(View.INVISIBLE);
        CodeProgress.setVisibility(View.INVISIBLE);
        sendVerification.setEnabled(false);

        setSupportActionBar(PhoneToolbar);
        getSupportActionBar().setTitle("Welcome to Phone Verification");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        number = PhoneNumber.getText().toString();
        if (!number.isEmpty()) {
            sendVerification.setEnabled(true);
        }

        sendVerification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                PhoneNumber.setEnabled(false);
                PhoneProgress.setVisibility(View.VISIBLE);


                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        number,
                        60,
                        TimeUnit.SECONDS,
                        PhoneAuthActivity.this,
                        mCallbacks
                );
            }
        });


        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

            }

            @Override
            public void onVerificationFailed(FirebaseException e) {

            }
        };
    }
}
  • 1
    You are currently setting the property "enabled" to false at creation time. I think you are missing an event listener on the input where you can type your data (phonenumber), which will enable your button based on your condition. – mrtna Jul 18 '18 at 14:56
  • I'm not getting OTP after applied TextWatcher. – Palak Singh Jul 19 '18 at 05:08

3 Answers3

1
number = PhoneNumber.getText().toString();
if (!number.isEmpty()) {
    sendVerification.setEnabled(true);
}

This is all executed in onCreate before you get the chance to start typing. Once you start typing this code is not executed again. It is only executed once, when the activity is being created.

Solution: set up a TextWatcher on the phone number editText and enable/disable sendVerification in the TextWatcher#afterTextChanged method. See here for instructions on how to do it.

Tim
  • 41,901
  • 18
  • 127
  • 145
0

You need a TextWatcher to do what you wish to do, so add a TextWatcher to your EditText/TextView like below:

PhoneNumber.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length()>0) {
                        sendVerification.setEnabled(true);
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

The reason your code wasn't working is because the onCreate() method does not get called after you start typing, meaning the condition for enabling your view is checked only once so any changes are not checked again.

0

In your code

number = PhoneNumber.getText().toString();
    if (!number.isEmpty()) {
        sendVerification.setEnabled(true);
    }

is executed only once at onCreate. You'll have to add a text change listener and enable/disable the button accordingly like,

PhoneNumber.addTextChangedListener(new TextWatcher() {
 @Override
  public void afterTextChanged(Editable s) {}

 @Override
  public void beforeTextChanged(CharSequence s, int start,
   int count, int after) {
 }

 @Override
  public void onTextChanged(CharSequence s, int start,
  int before, int count) {
  if(s.length() > 0)
  {
    sendVerification.setEnabled(true);
  }

} });

Madhan Varadhodiyil
  • 2,086
  • 1
  • 14
  • 20