remember username and password checkbox - java

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);
}
}
}
}

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);
}
}

error in simple log in form in android studio 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);
}
}
}

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");
}
}`

Animation not showing after restarting Android Activity

I'll do my best to explain my issue without a video
I have a login activity where upon successful login, the EditText and Button fields fade out and a "logging in" TextView fades in (using Facebook Shimmer). This works great!! However, upon successfully login we are greeted by a blank activity (still fine). Now, I overwrote the back button so that when the back button is pressed, the user is forced to login again. My problem arises with the user hits the login button at this time. The Edit Text and Button elements fade out nicely but the "logging in" TextView never fades in.
Below are some picture examples. I will also post the source code for LoginActivity.class and if you want to download the project it is available at: git#github.com:fbgrecojr/Android-Application-Login-Activity-Template.git
If you download the project, username: testuser and password: testpass will work.
Images:
Initial Login (working)
Login Attempt after pressing the back button and then logging in again (which I overwrote to restart the intent)
LoginActivity.class
package com.projects.fbgrecojr.logintemplate.UI;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.facebook.shimmer.ShimmerFrameLayout;
import com.projects.fbgrecojr.logintemplate.HTTPManager.HttpManager;
import com.projects.fbgrecojr.logintemplate.HTTPManager.RequestPackage;
import com.projects.fbgrecojr.logintemplate.Parser.JSONParser;
import com.projects.fbgrecojr.logintemplate.R;
import com.projects.fbgrecojr.logintemplate.Session.Session;
import com.projects.fbgrecojr.logintemplate.Structures.User;
import com.projects.fbgrecojr.logintemplate.Utility.UTILITY;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private EditText userName, password;
private Button login;
private RelativeLayout image;
private LinearLayout button, belowPic;
private Animation fadeInImage, fadeInButton, bottomUp, fadeOut;
private TextInputLayout inputLayoutName,inputLayoutPassword;
private ViewGroup hiddenPanel;
private ShimmerFrameLayout container, loggingIn;
private static final int SECOND = 1000;
private static final int HALF_SECOND = 500;
private static final int QUARTER_SECOND = 250;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//INITIALIZE ANIMATION ITEMS
fadeInImage = new AlphaAnimation(0, 1);
fadeInButton = new AlphaAnimation(0, 1);
fadeOut = new AlphaAnimation(1.0f,0.0f);
bottomUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.bottom_up_animation);
fadeInImage.setInterpolator(new AccelerateInterpolator()); //and this
bottomUp.setInterpolator(new DecelerateInterpolator());
//GET UI ELEMENTS
userName = (EditText) findViewById(R.id.userName);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
image = (RelativeLayout) findViewById(R.id.image);
button = (LinearLayout) findViewById(R.id.button);
container = (ShimmerFrameLayout) findViewById(R.id.shimmer);
belowPic = (LinearLayout) findViewById(R.id.below_picture);
loggingIn = (com.facebook.shimmer.ShimmerFrameLayout) findViewById(R.id.login_shimmer);
hiddenPanel = (ViewGroup)findViewById(R.id.input);
inputLayoutName = (TextInputLayout) findViewById(R.id.text_input_username);
inputLayoutPassword = (TextInputLayout) findViewById(R.id.text_input_password);
//SET UI PROPERTIES
loggingIn.setVisibility(View.INVISIBLE);
userName.setCursorVisible(false);
password.setCursorVisible(false);
password.setHint("Password");
userName.setHint("Username");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
userName.setCursorVisible(true);
password.setCursorVisible(true);
userName.requestFocus();
}
}, LoginActivity.SECOND * 3);
//ANIMATIONS
fadeInImage.setDuration(SECOND * 3);
fadeOut.setStartOffset(SECOND);
fadeOut.setDuration(SECOND);
image.setAnimation(fadeInImage);
fadeInButton.setStartOffset(SECOND + HALF_SECOND + QUARTER_SECOND);
fadeInButton.setDuration(SECOND * 2);
button.setAnimation(fadeInButton);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
container.setDuration(SECOND * 2 + QUARTER_SECOND);
container.setRepeatDelay(QUARTER_SECOND);
container.setIntensity((float) 0.15);
container.setBaseAlpha((float) 0.75);
container.setFadingEdgeLength(3);
container.setDropoff((float) 0.40);
container.startShimmerAnimation();
//ON CLICK LISTENERS
login.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
if(getUserName().getText().toString().equals("") || getUserName().getText().toString().equals(" ")) {
inputLayoutName.setError("enter username");
}else if(getPassword().getText().toString().equals("") || getPassword().getText().toString().equals(" ")){
inputLayoutPassword.setError("enter password");
}else{
//webservice
if (UTILITY.isOnline(getApplicationContext())) {
RequestPackage p = new RequestPackage();
p.setMethod("GET");
p.setUri(UTILITY.UBUNTU_SERVER_URL);
p.setParam("query", "user");
p.setParam("username", getUserName().getText().toString());
new WebserviceCallOne().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, p);
} else {
Toast.makeText(getApplicationContext(), "you are not connected to the internet", Toast.LENGTH_LONG).show();
}
}
break;
}
}
private void animateExit() {
//fade out annimation
belowPic.startAnimation(fadeOut);
belowPic.setVisibility(View.INVISIBLE);
fadeInImage.setStartOffset(SECOND * 2);
fadeInImage.setDuration(HALF_SECOND);
loggingIn.startAnimation(fadeInImage);
loggingIn.setVisibility(View.VISIBLE);
loggingIn.setDuration(SECOND);
loggingIn.startShimmerAnimation();
}
public EditText getPassword() {
return password;
}
public EditText getUserName() {
return userName;
}
private class WebserviceCallOne extends AsyncTask<RequestPackage, String, User> {
#Override
protected User doInBackground(RequestPackage... params) {
String content = HttpManager.getData(params[0]);
return JSONParser.parseUserFeed(content);
}
#Override
protected void onPostExecute(User s) {
Session.setCurrentUser(s);
//if null, error stacktrace will print to the log. This is expected!!
if(Session.getCurrentUser() == null){ //username was incorrect
inputLayoutName.setError("username does not exist");
}else{ //check password
if(getPassword().getText().toString().equals(s.getPassword())){ //passwords match
animateExit();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
},LoginActivity.SECOND * 4);
}else{
inputLayoutPassword.setError("password incorrect");
}
}
}
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Take care of popping the fragment back stack or finishing the activity
* as appropriate.
*/
#Override
public void onBackPressed() {
startActivity(new Intent(this, LoginActivity.class));
}
}
You have to call animateExit(); code in onResume methord
#Override
public void onResume() {
super.onResume();
animateExit();
}
override onResume() in your Activity.
try starting your animation in onresume.
override onPause() in your Activity
try stopping your animation in onPause.
Hope this Helps :)

java.lang.RuntimeException: Performing stop of activity that is not resumed:

I am getting a runtime exception when I am calling NavDrawerActivity from LoginScreen activity.
Error
java.lang.RuntimeException: Performing stop of activity that is not
resumed:
{com.example.owner.loginapp/com.example.owner.loginapp.NavDrawerActivity}
How to fix this error?
Here is my loginScreen code
package com.example.owner.loginapp;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.ImageView;
import android.widget.Toast;
import android.content.SharedPreferences;
public class LoginScreen extends AppCompatActivity {
ImageView image;
ImageView user_img;
ImageView pass_img;
Button log_in;
Button reg;
EditText editText_Username;
EditText editText_Pass;
UserSessionManager session;
private SharedPreferences sharedPreferences;
private static final String PREFER_NAME = "Reg";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
session = new UserSessionManager(getApplicationContext());
image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.smile);
user_img = (ImageView) findViewById(R.id.user_img);
user_img.setImageResource(R.drawable.user_img);
pass_img = (ImageView) findViewById(R.id.pass_img);
pass_img.setImageResource(R.drawable.img_pass);
log_in = (Button) findViewById(R.id.btn_Login);
reg = (Button) findViewById(R.id.btn_Reg);
editText_Username = (EditText) findViewById(R.id.editText_UserName);
editText_Pass = (EditText) findViewById(R.id.editText_Password);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
image.setImageBitmap(icon);
Toast.makeText(getApplicationContext(),
"User Login Status: " + session.isUserLoggedIn(),
Toast.LENGTH_LONG).show();
sharedPreferences = getSharedPreferences(PREFER_NAME, Context.MODE_PRIVATE);
log_in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = editText_Username.getText().toString();
String password = editText_Pass.getText().toString();
if (username.trim().length() > 0 && password.trim().length() > 0) {
String uName = null;
String uPassword = null;
if (sharedPreferences.contains("Name")) {
uName = sharedPreferences.getString("Name", "");
}
if (sharedPreferences.contains("Password")) {
uPassword = sharedPreferences.getString("Password", "");
}
if (username.equals(uName) && password.equals(uPassword)) {
session.createUserLoginSession(uName, uPassword);
Intent i = new Intent(getApplicationContext(), NavDrawerActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(),
"Username/Password is incorrect",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Please enter username and password",
Toast.LENGTH_LONG).show();
}
}
});
reg.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.register);
Intent i = new Intent(getApplicationContext(), Reg.class);
startActivity(i);
}
});
}
}
I copied your code and tried to instantiate a Reg Activity class that I designed and everything worked fine. It is possible that you are doing something wrong in the 1onResumeoronCreate` of Reg class.
Post that class too for more information.

Categories

Resources