Handling Chat typing or Stop typing event (Android) - java

I am handling typing events if user enter text in editext typing function sends requests to server and use removed text i want to stoptyping function do work but its not working.
How to check if user have removed text after entering text
mEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
new Thread(new Runnable() {
#Override
public void run() {
try {
session.typing();
} catch (OmegleException e) {
e.printStackTrace();
}catch(NullPointerException e){
e.printStackTrace();
}
}
}).start();
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(final CharSequence s, final int start,
int before, int count) {
new Thread(new Runnable() {
#Override
public void run() {
try {
if(start==-1){
session.stopTyping();
}
} catch (OmegleException e) {
e.printStackTrace();
}catch(NullPointerException e){
e.printStackTrace();
}
}
}).start();
}
});

I changed to following :
mEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
lengthBeforeType = mEditText.getText().toString().length();
}
public void onTextChanged(final CharSequence s, final int start,
int before, final int count) {
lengthAfterType = mEditText.getText().toString().length();
new Thread(new Runnable() {
#Override
public void run() {
try {
if((lengthAfterType-lengthBeforeType)<0){
session.stopTyping();
}else{
session.typing();
}
} catch (OmegleException e) {
e.printStackTrace();
}
}
}).start();

int lenghtAfterType;
int lenghtBeforeType;
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
lenghtBeforeType = yourEdittext.getText.toString.Lenght;
}
public void afterTextChanged(Editable s) {
lenghtAfterType = yourEdittext.getText.toString.Lenght;
if((lenghtAfterType-lenghtBeforeType)<0){
// user while deleted text
}else{
// user while typing
}
}

Related

Suggest text for edittext look like search of chrome

I have edited the text and list of results obtained from the search. Now I want to set first result for edittext, it looks like below
this is code i use to get result search:
bindingNV.etSearch.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.toString().length()>0){
new SearchFlowWord(new NotifyDataChangeListener() {
#Override
public void dataChange(List<String> mList) {
if (mList.size()>0) {
resultSearch = mList.get(0);
}else {
resultSearch ="";
}
}
}, s);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
How i can set edittext like image

Firebase: How to keep an Android user logged in using firebase otp authentication?

I'm using Firebase SimpleLogin to enable otp authentication. Creation of users and subsequent login is all working fine. However, whenever I leave the app completely all registration process start from registration process i want to stay logged in on dashboard.
this is flash activity
public class SplashActivity extends AppCompatActivity {
private static int SPLASHTIMEOUT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent homeIntent = new Intent(SplashActivity.this,register.class);
startActivity(homeIntent);
finish();
}
},SPLASHTIMEOUT);
}
}
this is rgister activty
public class register extends AppCompatActivity {
EditText enternumber;
Button getotpbutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
enternumber = findViewById(R.id.input_mobile_number);
getotpbutton = findViewById(R.id.buttongetotp);
final ProgressBar progressBar = findViewById(R.id.progressbar_sending_otp);
getotpbutton.setOnClickListener(v -> {
if (!enternumber.getText().toString().trim().isEmpty()) {
if ((enternumber.getText().toString().trim()).length() == 10) {
progressBar.setVisibility(View.VISIBLE);
getotpbutton.setVisibility(View.INVISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91" + enternumber.getText().toString(),
60,
TimeUnit.SECONDS,
register.this,
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
progressBar.setVisibility(View.GONE);
getotpbutton.setVisibility(View.VISIBLE);
}
#Override
public void onVerificationFailed(#NonNull FirebaseException e) {
progressBar.setVisibility(View.GONE);
getotpbutton.setVisibility(View.VISIBLE);
Toast.makeText(register.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeSent(#NonNull String backendotp, #NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
progressBar.setVisibility(View.GONE);
getotpbutton.setVisibility(View.VISIBLE);
Intent intent = new Intent(getApplicationContext(), otp.class);
intent.putExtra("mobile", enternumber.getText().toString());
intent.putExtra("backendotp",backendotp);
startActivity(intent);
}
}
);
} else {
Toast.makeText(register.this, "Please enter correct number", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(register.this, "Enter Mobile number", Toast.LENGTH_SHORT).show();
}
});
}
}
this is otp activity
public class otp extends AppCompatActivity {
EditText inputnumber1, inputnumber2, inputnumber3, inputnumber4, inputnumber5, inputnumber6;
String getotpbackend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
final Button verifybuttonclick = findViewById(R.id.buttongetotp);
inputnumber1 = findViewById(R.id.inputotp1);
inputnumber2 = findViewById(R.id.inputotp2);
inputnumber3 = findViewById(R.id.inputotp3);
inputnumber4 = findViewById(R.id.inputotp4);
inputnumber5 = findViewById(R.id.inputotp5);
inputnumber6 = findViewById(R.id.inputotp6);
TextView textView = findViewById(R.id.textmobileshownumber);
textView.setText(String.format(
"+91-%s", getIntent().getStringExtra("mobile")
));
getotpbackend = getIntent().getStringExtra("backendotp");
final ProgressBar progressBarverifyotp = findViewById(R.id.progressbar_verify_otp);
verifybuttonclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!inputnumber1.getText().toString().trim().isEmpty() && !inputnumber2.getText().toString().trim().isEmpty() && !inputnumber3.getText().toString().trim().isEmpty() && !inputnumber4.getText().toString().trim().isEmpty()) {
String entercodeotp = inputnumber1.getText().toString() +
inputnumber2.getText().toString() +
inputnumber3.getText().toString() +
inputnumber4.getText().toString() +
inputnumber5.getText().toString() +
inputnumber6.getText().toString();
if (getotpbackend != null) {
progressBarverifyotp.setVisibility(View.VISIBLE);
verifybuttonclick.setVisibility(View.INVISIBLE);
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
getotpbackend, entercodeotp
);
FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBarverifyotp.setVisibility(View.GONE);
verifybuttonclick.setVisibility(View.VISIBLE);
if (task.isSuccessful()) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
Toast.makeText(otp.this, "Enter the correct Otp", Toast.LENGTH_SHORT).show();
}
}
});
}
else {
Toast.makeText(otp.this, "Please check internet connection", Toast.LENGTH_SHORT).show();
}
Toast.makeText(otp.this, "otp verify", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(otp.this, "please enter all number", Toast.LENGTH_SHORT).show();
}
}
});
numberotpmove();
TextView resendlabel = findViewById(R.id.textresendotp);
resendlabel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91" + getIntent().getStringExtra("mobile"),
60,
TimeUnit.SECONDS,
otp.this,
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
}
#Override
public void onVerificationFailed(#NonNull FirebaseException e) {
Toast.makeText(otp.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeSent(#NonNull String newbackendotp, #NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
getotpbackend = newbackendotp;
Toast.makeText(otp.this, "Otp sended Succussfully", Toast.LENGTH_SHORT).show();
}
}
);
}
});
}
private void numberotpmove() {
inputnumber1.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.toString().trim().isEmpty()) {
inputnumber2.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber2.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.toString().trim().isEmpty()) {
inputnumber3.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber3.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.toString().trim().isEmpty()) {
inputnumber4.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber4.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.toString().trim().isEmpty()) {
inputnumber5.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputnumber5.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.toString().trim().isEmpty()) {
inputnumber6.requestFocus();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}

Writing inside EditText

I have two EdiText and when I write a number in the first one I would need the second to be set to 162 - first one. If it is necessary to re-type a second number, the component should recalculate the first number.
If I write something in the second the first must behave exactly like the second.
Below there is my code but it does not work:
inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);
View.OnClickListener inputScoreListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
int inputScoreWeInteger = Integer.parseInt(inputScoreWe.getText().toString());
int inputScoreYouInteger = Integer.parseInt(inputScoreYou.getText().toString());
if (inputScoreWeInteger > 0) {
inputScoreYouInteger = 162 - inputScoreWeInteger;
} else if (inputScoreYouInteger > 0) {
inputScoreWeInteger = 162 - inputScoreYouInteger;
}
String s1 = inputScoreWeInteger + "";
String s2 = inputScoreYouInteger + "";
inputScoreWe.setText(s1);
inputScoreYou.setText(s2);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
};
inputScoreWe.setOnClickListener(inputScoreListener);
inputScoreYou.setOnClickListener(inputScoreListener);
Use text change listener to trigger for everytime you change value
inputScoreWe.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){
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString())+"");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
replace your code with code as below
inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);
inputScoreWe.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) {
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString()) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputScoreYou.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) {
inputScoreWe.setText(162 - Integer.parseInt(inputScoreWe.getText().toString()) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
try this
brandET = findViewById(R.id.addCar_brand);
modelET = findViewById(R.id.addCar_model);
brandET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
brandChange = hasFocus;
}
});
modelET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
modelChange = hasFocus;
}
});
brandET.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 (brandChange && count > 0) {
int dataFromBrand = Integer.parseInt(s.toString());
modelET.setText((162 - dataFromBrand) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
modelET.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 (modelChange && count > 0) {
int dataFromModel = Integer.parseInt(s.toString());
brandET.setText((162 - dataFromModel) + "");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
here brandET and modelET is your two edit text...and...brandChange and modelChange are two global boolean data

How to add delay in Google Places API request?

I want to reduce request to Google Places API by adding delay for every user input but i am not sure how to implement this. This is what i usually do to add delay to my EditText waiting for user's input:
editTextEmail.addTextChangedListener(new TextWatcher() {
final Handler handler = new Handler();
Runnable runnable;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
isEmailValid = false;
textInputLayoutEmail.setError(null);
enableDisableButton();
handler.removeCallbacks(runnable);
}
#Override
public void afterTextChanged(Editable s) {
runnable = new Runnable() {
#Override
public void run() {
callGooglePlacesAPI()
}
};
handler.postDelayed(runnable, 1000);
}
});
This is my code to call Google PLaces API:
editTextLocation.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editTextLocation.setShowSoftInputOnFocus(false);
searchLocation();
}
}
});
public void searchLocation(){
try
{
editTextLocation.clearFocus();
//hide soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getActivity());
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
}catch (Exception e){
e.printStackTrace();
}
}
Is there a way to do this? Please help give advice on this. Thank you.

How to enable text when listening with "addTextChangedListener" and "Character.isLetterOrDigit(s.charAt(i))"

I disable a button when the user inserts non LetterOrDigit and enable it when a user inserts LetterOrDigit.
The problem I'm having is that it enables it too even when the user inserts a letter after a non LetterOrDigit. But I want to only enable when he deletes the non LetterOrDigit character.
My Code
name1.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) {
}
#Override
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i++) {
if(name1.getText().toString().length()>16)
{
register.setClickable(false);
mistakeusername.setErrorEnabled(true);
mistakeusername.setError("You cant insert more then 16 characters");
}else {
if (!(Character.isLetterOrDigit(s.charAt(i))||Character.toString(s.charAt(i)).equals("_")))
{
register.setClickable(false);
mistakeusername.setErrorEnabled(true);
mistakeusername.setError("Only allowed Digit, Number or '_'");
}else{
register.setClickable(true);
mistakeusername.setErrorEnabled(false);
mistakeusername.setError("");
}
}
}
}
});
i think your problem is that you should break from for after your
if(!(Character.isLetterOrDigit(s.charAt(i))||Character.toSt‌​ring(s.charAt(i)).eq‌​uals("_"))) is true.
try this:
name1.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) {
}
#Override
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i++) {
if(name1.getText().toString().length()>16)
{
register.setClickable(false);
mistakeusername.setErrorEnabled(true);
mistakeusername.setError("You cant insert more then 16 characters");
}else {
if (!(Character.isLetterOrDigit(s.charAt(i))||Character.toString(s.charAt(i)).equals("_")))
{
register.setClickable(false);
mistakeusername.setErrorEnabled(true);
mistakeusername.setError("Only allowed Digit, Number or '_'");
break;
}else{
register.setClickable(true);
mistakeusername.setErrorEnabled(false);
mistakeusername.setError("");
}
}
}
}
});

Categories

Resources