I have list of passwords (strings) in a database.
In my activity i put EditText (the user will write there his/her password) and a button.
How i can implement onClick for the button that will check if the password are in the system (in the list)?
Define a method called onClick() in your activity - this will get called when the button is clicked (as you specified in your XML). You can then retrieve the EditText text, and check it against the passwords in your DB.
public void onClick(View v) {
EditText myEditText = (EditText) findViewById(R.id.password);
CharSequence enteredPassword = myEditText.getText();
// TODO check if input matches a string in the DB
}
Implement OnClickListener to the control you want the user to click on:
Button mLoginButton = (Button) findViewById(R.id.login);
mLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Login();
}
});
Create the listener event "Login":
private void attemptLogin() {
//check login is valid
}
Add this to the button
btn.addActionListener( this );
and then do this
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == btn )
{
}
}
You can do like this:
Button loginButton = (Button) findViewById(R.id.login);
loginButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String pwd = password.getText().toString();
checkLogin(pwd)
}
})
private void checkLogin(String password) {
//db is your db instance.this is dummy query.You may have a user name editText as well
Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE TRIM(password) = '"+password.trim()+"'", null);
//check if cursor returns data
if (!(c .moveToFirst()) || c .getCount() ==0){
//cursor is empty
}
else
{
`//cursor is not empty`
}
}
Related
I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1.
but when i run my app and press the button, the app crashes...
i do not have much experience at programming and i do not know what im doing wrong. and i have only tried this code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked== true){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
if (isChecked == false) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
}
});
}
}
so this should behave as i described earlier but it doesn't.
Your app crashes because you are trying to set an int to a textview.setText()and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.
You should set text as following:
text.setText(String.valueOf(text_int));
You’re nesting listeners but that logic doesn’t work sequentially. You should declare your listeners separately. I suggest you create a boolean that holds the state of the switch and one button listener. Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mySwitch.isChecked(){
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
} else {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
}
}
});
You don't need a listener for the Switch, but only 1 listener for the Button:
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = 0;
try {
text_int = Integer.parseInt(text_string);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mySwitch.isChecked())
text_int++;
else
text_int--;
text.setText("" + text_int);
}
});
Every time you click the Button, in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.
So I basically want to know how would I allow the user to proceed to the next page when they hit pay if either txtCode or StreetCode is filled, only one of these two needs to be filled in order to pass to the next page but how would i get that to work as currently they both must filled to pass to the next page.
btnPay = (Button)findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText)findViewById(R.id.txtReg);
txtCode = (EditText)findViewById(R.id.txtCode);
txtStreetName = (EditText)findViewById(R.id.txtStreetName);
dlCostTime = (Spinner)findViewById(R.id.dlCostTime);
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
Replace this code
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
with this
if( txtReg.getText().toString().trim().equals("")){
txtReg.setError("required!");
}else{
if((txtCode.getText().toString().trim().equals("") && !txtStreetName.getText().toString().trim().equals("")) ||
(!txtCode.getText().toString().trim().equals("") && txtStreetName.getText().toString().trim().equals(""))){
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}else{
//show message enter either code or street name not both
}
Replace this code
btnPay = (Button) findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText) findViewById(R.id.txtReg);
txtCode = (EditText) findViewById(R.id.txtCode);
txtStreetName = (EditText) findViewById(R.id.txtStreetName);
dlCostTime = (Spinner) findViewById(R.id.dlCostTime);
if (txtCode.getText().toString().trim().length() > 0 || txtStreetName.getText().toString().trim().length() > 0) {
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
} else if (txtStreetName.getText().toString().trim().length() == 0) {
txtStreetName.setError("required!");
} else if (txtReg.getText().toString().trim().length() == 0) {
txtReg.setError("required!");
} else if (txtCode.getText().toString().trim().length() == 0) {
txtCode.setError("required!");
}
}
}
I am trying to make an app for a restaurant. I have set up a local database and have a prepared statement to save a users username and password to enable them to log into the system, but it does not seem to work. The username and passwords are not saving. Below is my code, if anyone has any solutions, i would greatly appreciate it.
public class register extends AppCompatActivity {
EditText username, password;
Button registartion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
private Connection getConnection()
{
String host = "jdbc:mysql://jdbc.fmc.me.uk:3306/db_ben";
String u = "user_ben";
String p = "******";
try
{
Connection con = DriverManager.getConnection(host, u, p);
return con;
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private boolean registerUser(){
//Get username
EditText regUsernameBox = (EditText) findViewById(R.id.RegUsername);
String regUsernameStr = regUsernameBox.getText().toString();
//Get password
EditText regPasswordBox = (EditText) findViewById(R.id.RegPassword);
String regPasswordStr = regPasswordBox.getText().toString();
System.out.println(regUsernameStr);
System.out.println(regPasswordStr);
int i = 0;
Connection con = getConnection();
try {
PreparedStatement pst = con.prepareStatement("insert into users values(?,?)");
pst.setString(1, regUsernameStr);
pst.setString(2,regPasswordStr);
i = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
if(i>0)
return true;
return false;
}
public void buttonClick() {
final Button registerUsers = (Button) findViewById(R.id.bRegister);
registerUsers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registerUser();
}
});
}
}
None of your methods are being called. Your oncreate sets the view for the activity and then does nothing. Hence, you get no errors as there are technically none. You have to set the button click instructions for the button in the create. Your onCreate should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final Button registerUsers = (Button) findViewById(R.id.bRegister);
registerUsers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registerUser();
}
});
}
Now here the button is assigned a variable and then the clicklistener is set to it onCreate. Now when you click the button it would do something.
iQuote the android references http://developer.android.com/intl/es/reference/android/app/Activity.html#onCreate(android.os.Bundle)
i also think that you should use asynctask to do database work to keep your app responsive
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
so it must look like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
EditText regUsernameBox = (EditText) findViewById(R.id.RegUsername);
EditText regPasswordBox = (EditText) findViewById(R.id.RegPassword);
Button registerUsers = (Button) findViewById(R.id.bRegister);
registerUsers.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
registerUser();
}
});
}
I'm trying to produce a password check before submitting my form, but I'm not getting the desired behavior. Basically no matter what the user inputs for either field, it will submit the new password from mConfirmPasswordField. What I want to have happen is that if the passwords do not match, hence if (!(mNewPassword.equals(mConfirmPassword)), then an alert dialog is displayed and nothing more. Based on the code below, this seems like it should be the case but it simply saves the new password to the user either way. What am I doing wrong here?
mNewPasswordField = (EditText)findViewById(R.id.newPassword);
mConfirmPasswordField = (EditText)findViewById(R.id.newPasswordAgain);
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
mNewPasswordField.setText(mNewPassword);
mConfirmPasswordField.setText(mConfirmPassword);
Button mButton = (Button) findViewById(R.id.submitPasswordChanges);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!(mNewPassword.equals(mConfirmPassword))) {
AlertDialog.Builder builder = new AlertDialog.Builder(ChangePasswordActivity.this);
builder.setMessage("Please check that you've entered and confirmed your new password!")
.setTitle("Error:")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
setProgressBarIndeterminateVisibility(true);
//Update user
ParseUser user = ParseUser.getCurrentUser();
user.setPassword(mConfirmPasswordField.getText().toString());
user.saveInBackground(new SaveCallback() {
public void done(com.parse.ParseException e) {
// TODO Auto-generated method stub
LaunchPersonalGalleryIntent();
}
});
}
}
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
This will be executed when the EditText are still empty, so mNewPassword equals mConfirmPassword equals "".
The two String should be retrieved within the onClick:
..
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
if (!(mNewPassword.equals(mConfirmPassword))) {
..
I believe what's wrong is that you are not updating your fields: you should check for new values right when the user clicks the button, like:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mNewPassword = mNewPasswordField.getText().toString();
mConfirmPassword = mConfirmPasswordField.getText().toString();
if (!(mNewPassword.equals(mConfirmPassword))) {
....
} else {
....
}
}
});
I should mention for others passing through, that you will also need to include the following fields, along with the password strings, within the onClick method.
..
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mNewPasswordField = (EditText)findViewById(R.id.newPassword);
mConfirmPasswordField = (EditText)findViewById(R.id.newPasswordAgain);
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
mConfirmPasswordField.setText(mConfirmPassword);
if (!(mNewPassword.equals(mConfirmPassword))) {
..
I have an EditText with a text, and there is a button, what I want the code to make is that when a long click is done on the Button(IButton1) the EditText's will be gone... The code:
private void Delete() {
ImageButton IButton1=(ImageButton)findViewById(R.id.ibdelete);
//Click:
IButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"A long click is required in order to delete the text", Toast.LENGTH_LONG).show();
}
});
//Long Click:
IButton1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
EditText text1 = (EditText) findViewById(R.id.EditText1);
text1.setText("");
Toast.makeText(getApplicationContext(), "The text is gone!", Toast.LENGTH_SHORT).show();
}
});
}
Now, the problemis that it says: "Unreachable statement" and the line EditText text1 = (EditText) findViewById(R.id.EditText1); is underline with red(an error), and I would like to solve this...
Right now the first statement executed in onLongClick() is return false; which will end the method. Any and all code after that in the method can never be reached. The return should be the last thing in your method.
You need to move your return statement to the end of the method like this.
public boolean onLongClick(View v) {
EditText text1 = (EditText) findViewById(R.id.EditText1);
text1.setText("");
Toast.makeText(getApplicationContext(), "The text is gone!", Toast.LENGTH_SHORT).show();
return false;
}