-5

How can I do this, I searched on StackOverflow, but solution is for iphone, not android. Here is my chat app: https://play.google.com/store/apps/details?id=com.jimmytrivedi.lapitchat

package com.jimmytrivedi.lapitchat;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
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;
    private FirebaseAnalytics mFirebaseAnalytics;
    private String mVerificationId;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
    private FirebaseAuth mAuth;
    private TextView ErrorView;
    private int Verify = 0;

    @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);
        ErrorView = findViewById(R.id.ErrorView);

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

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

        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        mAuth = FirebaseAuth.getInstance();

        PhoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String text = charSequence.toString();
                if (!text.isEmpty()) {
                    sendVerification.setEnabled(true);
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });


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

                if(Verify == 0) {

                    if (!PhoneNumber.getText().toString().isEmpty()) {
                        number = PhoneNumber.getText().toString();
                        PhoneNumber.setEnabled(false);
                        sendVerification.setEnabled(false);
                        PhoneProgress.setVisibility(View.VISIBLE);


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

                    }
                } else {
                    String VerificationCode = code.getText().toString();
                    sendVerification.setEnabled(false);
                    CodeProgress.setVisibility(View.VISIBLE);
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(
                            mVerificationId,VerificationCode);
                    signInWithPhoneAuthCredential(credential);
                    }
            }
        });

        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                PhoneProgress.setVisibility(View.INVISIBLE);
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {

                ErrorView.setText("There was some error in verification");
                ErrorView.setVisibility(View.VISIBLE);

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    Log.d("wihddiewd", "FirebaseAuthInvalidCredentialsException: " + e);

                } else if (e instanceof FirebaseTooManyRequestsException) {
                    Log.d("wihddiewd", "FirebaseTooManyRequestsException: " + e);
                }

            }

            @Override
            public void onCodeSent(String verificationId,
                                   PhoneAuthProvider.ForceResendingToken token) {

                Log.d("wihddiewd", "onCodeSent:" + verificationId);


                mVerificationId = verificationId;
                mResendToken = token;
                Verify=1;
                PhoneProgress.setVisibility(View.INVISIBLE);
                LockLayout.setVisibility(View.VISIBLE);
                sendVerification.setEnabled(true);
                sendVerification.setText("Verify code");

            }
        };
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            FirebaseUser user = task.getResult().getUser();
                            startActivity(new Intent(PhoneAuthActivity.this, MainActivity.class));
                            finish();
                        } else {
                            ErrorView.setText("There was some error in logging in");
                            ErrorView.setVisibility(View.VISIBLE);
                            Log.w("wihddiewd", "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            }
                        }
                    }
                });
    }

}

I don't know what type of code is necessary and where should I add?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Have you implemented any drop-down? Have you tried to fill it with country codes? – OneCricketeer Jul 21 '18 at 13:08
  • Nope, I have to manually type +91**** I also don't know how to do that. Please check my app, you'll understand chat app: https://play.google.com/store/apps/details?id=com.jimmytrivedi.lapitchat – Palak Singh Jul 21 '18 at 13:24
  • No one here will download a random app from the store, sorry – OneCricketeer Jul 21 '18 at 13:25
  • Okay, please help me by my code. – Palak Singh Jul 21 '18 at 13:25
  • If you'd like assistance, please edit your question to include a [mcve] of the code you've tried. By the way, no searches at all helped you? https://stackoverflow.com/questions/9760341/retrieve-a-list-of-countries-from-the-android-os and http://androiddhina.blogspot.in/2015/08/android-get-list-of-countries-in-spinner.html – OneCricketeer Jul 21 '18 at 13:27
  • @cricket_007 please check the updated question – Palak Singh Jul 21 '18 at 13:30
  • Well, you don't need Firebase at all to get a list of countries, so again, please show a **minimal** attempt at a solution to your problem based on those links I just sent – OneCricketeer Jul 21 '18 at 13:36
  • I've checked, but I don't understand that which code should I cut? Actually where can I put this code and all? and which library? please help – Palak Singh Jul 21 '18 at 13:38
  • I didn't say anything about a library. You can cut all of what you have added to the question. None of it needs to be there to create a super simple example of a country code drop-down list... If you actually want to capture the *phone number country codes*, you can just build a Hashmap in your code – OneCricketeer Jul 21 '18 at 13:41
  • How can I do that? Can you please post the answer as a code, Surely I'll accept it – Palak Singh Jul 21 '18 at 13:48
  • Check this reusable & customizable class https://stackoverflow.com/a/55999491/6667442 – Ketan Ramani May 06 '19 at 05:59

1 Answers1

3

Try this lib: CountryCodePicker

 dependencies {
      implementation 'com.hbb20:ccp:2.2.2'
    }

In XML

<com.hbb20.CountryCodePicker
                    android:id="@+id/country_picker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:gravity="center|center_vertical"
                    android:includeFontPadding="false"
                    android:textColor="@color/text_black"
                    app:ccp_showNameCode="false"
                    app:ccp_textSize="@dimen/small_text_size" />

In Activity

CountryCodePicker country_picker = findViewById(R.id.country_picker);
String country_code = country_picker.getSelectedCountryCode();
prashant17
  • 1,520
  • 3
  • 14
  • 23