Submit button not validating - java

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

Related

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

How to run this page just once after installing the page

I want this code to run just once after installing the app.
Page 1 has a button and after clicking it, the user will be directed to page 2.
Page 2 is the disclaimer page and after checkboxes are marked and agree button is clicked, the user is directed to the home page.
When the user clicks for the second time, this disclaimer page should not open again.
I tried to modify my code for it, but I couldn't because of the button as I'm not sure how to use it.
Page 1
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button btn_Shap;
public void first(){
btn_Shap = (Button)findViewById(R.id.btn_Shap);
btn_Shap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent btn = new Intent(MainActivity.this, disclaimer.class);
startActivity(btn);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first();
}
}
Page 2
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class disclaimer extends AppCompatActivity {
public Button button2;
public Button button1;
public void second(){
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent btn = new Intent(disclaimer.this, home.class);
startActivity(btn);
}
});
}
public void first(){
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent btn = new Intent(disclaimer.this, MainActivity.class);
startActivity(btn);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disclaimer);
second();
first();
}
}
Inside the desclaimer page store a boolean value using shared preference and check it before open the desclaimer page. like this
Inside the Desclaimer page
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
Before open the desclaimer page
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if ( firstRun )
{
//Open desclaimer
}else{
//Do not open desclaimer
}

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

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 :)

Categories

Resources