error in simple log in form in android studio java - java

I am beginner. I have made a simple log in form in android studio. I face a fault in if condition. Please solve this.
If username box is empty then control should go in if(...) but it goes to else part and open next intent
code is here:
package com.android.dumbseraaz;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText uname;
private EditText pwd;
private Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText)findViewById(R.id.txt_userName);
pwd = (EditText)findViewById(R.id.txt_Password);
login = (Button)findViewById(R.id.button);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate(uname.getText().toString(),pwd.getText().toString());
}
});
}
private void validate(String myuname, String Pswd) {
if(myuname == "")
{
uname.setText("user");
}
else if(Pswd == "")
{
pwd.setText("PasswordMe");
}
else {
Intent intent = new Intent(MainActivity.this, app_home.class);
startActivity(intent);
}
}
}

Related

Why does my Database not exist in the Main-Activity, when I return back to MainActivity?

in the Mainclass Im creating a variable called mDatabase. In the Mainclass there is a button called Database. When I click on it, it opens a new Activity. Then it starts the onCreate-Method of the second class databasegrocery.
When the program executes the command:
mAdapter=new GroceryAdapter(this, activity.getAllItems());
(...) it returns back to Main-Activity to call the method getAllItems().
When im returning to this method, mDatabase is null (mDatabase was already declared and initialized, when the app started with the MainActivity), why?
As Im totally new to android programing its possible that Im trying something stupid,
so I appreciate your help.
(I read something about activity-lifecycles and I saw that the MainActivity is in Stop-Status, when the second Activity is active. So the data from MainActivity is not destroyed, right?)
If you need more information, tell me!
Thank you.
package com.example.grocerylist;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class MainActivity extends AppCompatActivity{
public SQLiteDatabase mDatabase;
private EditText mEditTextName;
private EditText mEditTextName2;
private EditText mEditTextName3;
private EditText mEditTextName4;
private EditText mEditTextName5;
private EditText mEditTextName6;
private EditText mEditTextName7;
private EditText mEditTextName8;
private EditText mEditTextName9;
Button buttonIncrease;
Button buttonDecrease;
Button buttonAdd;
Button buttonDatabase;
public databasegrocery database;
Intent IntentDatabase;
private TextView mTextViewAmount;
private int mAmount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GroceryDBHelper dbHelper = new GroceryDBHelper(this);
mDatabase = dbHelper.getWritableDatabase();
mEditTextName = findViewById(R.id.edittext_name);
mTextViewAmount = findViewById(R.id.textview_amount);
mEditTextName2=findViewById(R.id.edittext_name2);
mEditTextName3=findViewById(R.id.edittext_name3);
mEditTextName4=findViewById(R.id.edittext_name4);
mEditTextName5=findViewById(R.id.edittext_name5);
mEditTextName6=findViewById(R.id.edittext_name6);
mEditTextName7=findViewById(R.id.edittext_name7);
mEditTextName8=findViewById(R.id.edittext_name8);
mEditTextName9=findViewById(R.id.edittext_name9);
IntentDatabase=new Intent(MainActivity.this,databasegrocery.class);
buttonIncrease = findViewById(R.id.button_increase);
buttonDecrease = findViewById(R.id.button_decrease);
buttonAdd = findViewById(R.id.button_add);
buttonDatabase=findViewById(R.id.buttonDatabase);
buttonDatabase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(IntentDatabase);
}
});
buttonIncrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
increase();
}
});
buttonDecrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
decrease();
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addItem();
}
});
}
private void increase() {
mAmount++;
mTextViewAmount.setText(String.valueOf(mAmount));
}
private void decrease() {
if (mAmount > 0) {
mAmount--;
mTextViewAmount.setText(String.valueOf(mAmount));
}
}
private void addItem() {
if (mEditTextName.getText().toString().trim().length() == 0 || mAmount == 0) {
return;
}
String name = mEditTextName.getText().toString();
String mkcal1 = mEditTextName2.getText().toString();
String mkcal2=mEditTextName3.getText().toString();
String mFett=mEditTextName4.getText().toString();
String mDavongesaettigtefettsaueren=mEditTextName5.getText().toString();
String mKohlenhydrate=mEditTextName6.getText().toString();
String mZucker=mEditTextName7.getText().toString();
String mEiweiss=mEditTextName8.getText().toString();
String mSalz=mEditTextName9.getText().toString();
//___________________________________________________________________
ContentValues cv = new ContentValues();
cv.put(GroceryContract.GroceryEntry.COLUMN_NAME, name);
cv.put(GroceryContract.GroceryEntry.COLUMN_AMOUNT, mAmount);
//----------------------------------------------------------------------------------------
cv.put(GroceryContract.GroceryEntry.COLUMN_KCAL1, mkcal1);
cv.put(GroceryContract.GroceryEntry.COLUMN_KCAL2, mkcal2);
cv.put(GroceryContract.GroceryEntry.COLUMN_FETT, mFett);
cv.put(GroceryContract.GroceryEntry.COLUMN_DAVONGESAETTIGTEFETTSAEUREN, mDavongesaettigtefettsaueren);
cv.put(GroceryContract.GroceryEntry.COLUMN_Kohlenhydrate, mKohlenhydrate);
cv.put(GroceryContract.GroceryEntry.COLUMN_Zucker, mZucker);
cv.put(GroceryContract.GroceryEntry.COLUMN_EIWEIS, mEiweiss);
cv.put(GroceryContract.GroceryEntry.COLUMN_SALZ, mSalz);
//------------------------------------------------------------------------------------------
mDatabase.insert(GroceryContract.GroceryEntry.TABLE_NAME, null, cv);
databasegrocery.mAdapter.swapCursor(getAllItems());
mEditTextName.getText().clear();
mEditTextName2.getText().clear();
mEditTextName3.getText().clear();
mEditTextName4.getText().clear();
mEditTextName5.getText().clear();
mEditTextName6.getText().clear();
mEditTextName7.getText().clear();
mEditTextName8.getText().clear();
mEditTextName9.getText().clear();
}
public Cursor getAllItems() {
return mDatabase.query(GroceryContract.GroceryEntry.TABLE_NAME, null, null, null, null, null, GroceryContract.GroceryEntry.COLUMN_TIMESTAMP + " ASC");
}
}
package com.example.grocerylist;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class databasegrocery extends AppCompatActivity {
public static GroceryAdapter mAdapter;
public MainActivity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.databasegrocery);
activity=new MainActivity();
RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new GroceryAdapter(this, activity.getAllItems());
recyclerView.setAdapter(mAdapter);
}
}

Firebase Sms verification on phone number does not working manually

I use Firebase Sms verification which sends OTP to the provided number. But the problem is it detects the number automatically if the number is within the phone.But i want to enter any number and when i put the number and i can access to the next activity by manually putting the otp. But in my case the the otp is received by any number(i mean outside of the device) but when i enter the code manually in the edittext, the app crashes.
Activity where the number is given and Otp send to the number
package com.example.bohon_final__001;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
public class LoginActivity extends AppCompatActivity {
private EditText LoginPhone;
private Button LoginConfirmButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginPhone = (EditText) findViewById(R.id.PhoneNumberLogin);
LoginConfirmButton = (Button) findViewById(R.id.Loginbutton);
Button registerbutton=(Button)findViewById(R.id.RegisterButton);
LoginConfirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String number=LoginPhone.getText().toString();
if(number.isEmpty() || number.length()<11)
{
LoginPhone.setError("Valied Number Required");
LoginPhone.requestFocus();
}
else {
String PhoneNumber = "+88" + number;
Intent firstintent = new Intent(LoginActivity.this, CodeConfirm.class);
firstintent.putExtra("PhoneNumber", PhoneNumber);
startActivity(firstintent);
}
}
});
registerbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this,User_Registration.class));
}
});
}
}
Activity that receives the OTP
package com.example.bohon_final__001;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskExecutors;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class CodeConfirm extends AppCompatActivity {
EditText Otpverify;
int s;
private String VerificationCode;
Button confirmbutton,test;
private FirebaseAuth mAuth;
String code,PhoneNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code_confirm);
Otpverify = (EditText) findViewById(R.id.ConfirmCode);
test=(Button)findViewById(R.id.testbutton);
mAuth= FirebaseAuth.getInstance();
PhoneNumber = getIntent().getStringExtra("PhoneNumber");
SendVerificationCode(PhoneNumber);
confirmbutton=(Button)findViewById(R.id.ConfirmButton);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent nactivity=new Intent(CodeConfirm.this,Current_Location.class);
nactivity.putExtra("Phone",PhoneNumber);
startActivity(nactivity);
}
});
confirmbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
code=Otpverify.getText().toString().trim();
if(code.isEmpty() || code.length()<6)
{
Otpverify.setError("Enter the OTP properly");
Otpverify.requestFocus();
}
Verifycode(code);
}
});
}
private void Verifycode(String code) {
PhoneAuthCredential credential=PhoneAuthProvider.getCredential(VerificationCode,code);
Signin(credential);
}
private void Signin(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Intent WorkingSwitch=new Intent(CodeConfirm.this,Current_Location.class);
WorkingSwitch.putExtra("Phone",PhoneNumber);
WorkingSwitch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(WorkingSwitch);
}
}
});
}
private void SendVerificationCode(String Number) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
Number,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallBack
);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
String code=phoneAuthCredential.getSmsCode();
if(code!=null)
{
Verifycode(code);
}
}
#Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(CodeConfirm.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
#Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
VerificationCode=s;
}
};
}
I've seen similar question but the answer is not satishfying.

Perserve value in text field in screen 1 while pressing back from the screen 2 using variables and intents

I have 2 screen on first i have Text field and when i pressed the button it takes me to second screen with data from the Text Field but while pressing the back button from screen 2 to screen 1.
I want that the text field(screen 1) will show the previously added data through variables.
////////////////////////////////////Screen 1 code://///////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
EditText name;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.buttonNext1);
name=(EditText)findViewById(R.id.editTextName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String namevalue= name.getText().toString();
savedInstanceState.putString("MyString", "Welcome back to Android");
Intent intent=new Intent(MainActivity.this,Main3Activity.class);
intent.putExtra("Name",namevalue);
startActivity(intent);
}
});
}
}
////////////////**Screen 2 code:**////////////////////////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(Main3Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
Screen 2
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
String valueOfName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
valueOfName = getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
screen2Done();
}
});
}
public void screen2Done() {
Intent intent=new Intent();
intent.putExtra("RESULT_STRING", valueOfName);
setResult(RESULT_OK, intent);
finish();
}
#Override
public void onBackPressed() {
screen2Done();
}
On screen 1, capture the value in onActivityResult() method, similarly as in screen 2.

remember username and password checkbox

I have this Javascript code for login in my app. it working fine but how can I add a checkbox for remember username and password to this code to my project? Is there some one out there that can help me? Thanks for all help
package no.vfk.vfkhakkespettboka;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login_backup extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Name = (EditText)findViewById(R.id.etName);
Password = (EditText)findViewById(R.id.etPassword);
Info = (TextView)findViewById(R.id.tvInfo);
Login = (Button)findViewById(R.id.btnLogin);
Info.setText("No attempt remains: 6");
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validate(Name.getText().toString(), Password.getText().toString());
}
});
}
private void validate(String userName, String userPassword){
if((userName.equals("test")) && (userPassword.equals("test123"))){
Intent intent = new Intent(Login_backup.this, Home.class);
startActivity(intent);
}else{
counter--;
Info.setText("No attempt remains: " + String.valueOf(counter));
if(counter == 0){
Login.setEnabled(false);
}
}
}
}

Submit button not validating

I am trying to get a submit button to take me to the next page when the input is milk. Here's what I've tried so far
package com.example.ephraimcohen.prestwichlanguageschool;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class activitytwomilk2 extends AppCompatActivity {
private EditText Word;
private TextView TryAgain;
private Button Submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitytwomilk2);
Word = (EditText)findViewById(R.id.etword);
TryAgain = (TextView)findViewById(R.id.tvtryagian);
Submit = (Button) findViewById(R.id.btnsubmit);
TryAgain.setText(" ");
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validate(Word.getText().toString());
}
});
}
private void validate(String userWord){
if((userWord.equals ("milk"))){
Intent intent = new Intent(activitytwomilk2.this, activitytwomilkcorrect.class);
startActivity(intent);
}else{
TryAgain.setText(" " + "Try again");
}
}`
Under that, there is another button which just goes to the home screen. That button is working properly and taking me to the homepage.
Try to use == instead of equals in your condition
if(userWord=="milk"){
Intent intent = new Intent(activitytwomilk2.this, activitytwomilkcorrect.class);
startActivity(intent);
}else{
TryAgain.setText(" " + "Try again");
}
}`

Categories

Resources