I use .setError function. But it checks the text field only when the save button is clicked, go to SecondActivity and press Back button on device to see it can't be left empty!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_card);
save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText Name2 = (EditText)findViewById(R.id.txtName);
if( Name2.getText().toString().length() == 0 )
Name2.setError( "First name is required!" );
Intent intent = new Intent(NewCard.this, Template.class);
startActivity(intent);
}
});
Any idea on how i can check when button is clicked but not proceeding to SecondActivity if Name2 is blank? And beside length(), i also want to check for numbers, undesired characters etc. if possible. Thanks for assistance.
You just need to create an else for your if, then move your call to startActivity() into that like so:
// Expand this condiditional to perform whatever other validation you want
if( Name2.getText().toString().length() == 0 ) {
Name2.setError( "First name is required!" );
} else {
// Validation passed, show next Activity
Intent intent = new Intent(NewCard.this, Template.class);
startActivity(intent);
}
Related
I am getting an error when I set the counter to subtract and close the application. I get an error "cannot assign value to final variable counter". If the user logins in 3 times with no success quit the application.
final int counter = 3;
//Set the OKButton to accept onClick
OKButton.setOnClickListener(new View.OnClickListener() {
#Override
//once onClick is initalized it takes user to page menu
public void onClick(View v) {
//display text that was inputed for userText and passText
user = userText.getText().toString();
pass = passText.getText().toString();
//create if loop which checks if user and pass equals the credentials
if (user.equals("pshivam") && pass.equals("Bway.857661")) {
//display toast access welcome
String welcome = "Access Granted.";
//Create a Toast to display the welcome string in the MainActivity.
Toast.makeText(MainActivity.this, welcome, Toast.LENGTH_SHORT).show();
setContentView(R.layout.account_main);
}
//create else if loop which checks if user or pass does not equals the credentials
else if (!user.equals("pshivam") || !pass.equals("Bway.857661")){
//displays previous entry
userText.setText(user);
passText.setText(pass);
//allows user to re-enter credentials.
user = userText.getText().toString();
pass = passText.getText().toString();
//display toast access fail
String fail = "Access Denied! Please Try again.";
//Create a Toast to display the fail string in the MainActivity.
Toast.makeText(MainActivity.this, fail, Toast.LENGTH_SHORT).show();
counter--;
if(counter == 0){
finish();
}
}
}
});
}
}
Do something like this :
OKButton.setOnClickListener(new View.OnClickListener() {
int counter = 3;
#Override
//once onClick is initalized it takes user to page menu
public void onClick(View v) {
You can also call a function from inside onClick which will decrement the variable, or use a static field declared in your class
This How to increment a Counter inside an OnClick View Event and How do I use onClickListener to count the number of times a button is pressed? might help.
Edit:
What you are doing in else part doesn't make sense. You are setting text for userText and passText that you just got using getText() from these. Then you are storing these same values to user and pass. But you aren't using these variables anywhere and they get new values when onClick is called again. Why not keep it simple :
else {
//display toast access fail
String fail = "Access Denied! Please Try again.";
//Create a Toast to display the fail string in the MainActivity.
Toast.makeText(MainActivity.this, fail, Toast.LENGTH_SHORT).show();
counter--;
if(counter == 0){
finish();
}
}
I have an android app that has a Login Button, which logs in once validated information with that in the SQLite database, I also have another button that is for Register.
How can I make it so when the user Login is successful the register button is disabled and only becomes clickable when the app is closed and restarted.
Below is the code for my two buttons.
public void onClickButton(View v) {
// Login Button
if (v.getId() == R.id.Blogin) {
EditText a = (EditText) findViewById(R.id.TFusername);
String str = a.getText().toString();
EditText b = (EditText) findViewById(R.id.TFpassword);
String pass = b.getText().toString();
String password = helper.searchPass(str);
if (pass.equals(password)) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
} else {
// Show Toast Message
Toast temp = Toast.makeText(LoginActivity.this, "Username/ Password is incorrect!", Toast.LENGTH_SHORT);
temp.show();
}
}
// Create account Button
if (v.getId() == R.id.Bsignup) {
Intent i = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(i);
}
} // Ends onClickButtonenter code here
If you could give suggestions or point me in the right direction that would be great.
Try like this
public void onClickButton(View v) {
// Login Button
if (v.getId() == R.id.Blogin) {
EditText a = (EditText) findViewById(R.id.TFusername);
String str = a.getText().toString();
EditText b = (EditText) findViewById(R.id.TFpassword);
String pass = b.getText().toString();
String password = helper.searchPass(str);
if (pass.equals(password)) {
register_button.setEnabled(False)
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
} else {
register_button.setEnabled(True)
// Show Toast Message
Toast temp = Toast.makeText(LoginActivity.this, "Username/ Password is incorrect!", Toast.LENGTH_SHORT);
temp.show();
}
}
// Create account Button
if (v.getId() == R.id.Bsignup) {
Intent i = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(i);
}
}
Set the buttons visibility to GONE, so it will be completely removed or to INVISIBLE after you logged in.
b.setVisibility(View.GONE);
If you wont save any state after restarting the app the button will be visible again.
When you inflate the view in your Activity and you create your Button objects. You could test for the successful login and then use
if (someCheckForASuccessfulLogin){
theRegisterButton.setEnabled(false);
}
1. Make one extra field in database
Set one more field in database for checking user is already registered or not. If user register once set this field's value as "false".
2. Check field value by getting it from database
Now at login time first check this field value whether it's "true" or "false".
If value is false then write this line.
yourButtonName.setClickable(false);
if value is "true" then write yourButtonName.setClickable(true);
This will work definitely.
Hi guys i'm really stuck on this and i'd appreciate help .I am inserting into a mysql database and when completed inside the button i am calling finish and it's going back to main activity but the list is not refreshing ?i tried to dothe OnResume and onRestart but the list is not showing at all ?
this is my insert button (main activity is called businessSide.java)
//Add offer when button is clicked and send business id and business email to business side
Button button = (Button) findViewById(R.id.btnOffer);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String serverURL = "http://10.0.2.2/Codes/AddOffer.php";
offerName = editNameText.getText().toString();
offerLocation = editLocationText.getText().toString();
offerDescription = editDescriptionText.getText().toString();
offerType=type;
Intent i = getIntent();
businessId = i.getStringExtra("number");
businessEmail = i.getStringExtra("email");
new SummaryAsyncTask().execute((Void) null);
Toast.makeText(getApplicationContext(), "You have successfully placed a new offer ", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Add_offer.this,BusinessSide.class);
intent.putExtra("number", id);
intent.putExtra("email", businessEmail);
startActivity(intent);
finish();
// onBackPressed();
}
});
}
I am developing an App using Eclipse. I have a page where it has different check boxes. I want the user if checking lest say options A and B and D then Activity 7 will open and if the user checks options A and C then Activity 5 will open.
Thank you
You can do so like this:
Get the IDs of the checkboxes. Then add an OnClickListener to the checkboxes like so:
OnClickListener checkBoxListener;
checkBoxListener = new OnClickListener()
{
#Override
public void onClick(View arg0) {
if (checkboxA.isChecked() && checkboxC.isChecked())
{
Intent i = new Intent(this,Activity5.class)
startActivity(i);
}
else if (checkboxA.isChecked() && checkboxB.isChecked() && checkboxD.isChecked())
{
Intent i = new Intent(this,Activity7.class)
startActivity(i);
}
}
};
checkboxA.setOnClickListener(checkBoxListener);
checkboxB.setOnClickListener(checkBoxListener);
checkboxC.setOnClickListener(checkBoxListener);
checkboxD.setOnClickListener(checkBoxListener);
Please give this a try.
Hi there I am new to Android Programming
I am trying to create an application in which, the user clicks button on the first page
the text color in the buttons change color and the change is reflected in another activity page.
To do this I have
1) one fragment class(BookLockerFragment) which reference to an xml file containing the buttons
2) The parent activity file (TabActivity.java)
3) The activity file to reflect the change ( complainResponse.java)
Here is the code:
LodgeComplaintFragment.java
ArrayList<String>userSelectedOptions = new ArrayList<String>();
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS");
}
Button but = (Button) root.findViewById(R.id.searchButton);
.....
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonListener.onMakeBookingButtonPressed(userSelectedOptions);
}
});
TabMainActivity.java
public void onMakeBookingButtonPressed(ArrayList<String> list) {
// TODO Auto-generated method stub
Intent intent = new Intent(TabMainActivity.this,
complainResponse.class);
intent.putStringArrayListExtra("userSelectOptions",list);
startActivity(intent);
}
complainResponse.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
setContentView(R.layout.complainresponse);
userInput = intent.getStringArrayListExtra("userSelectOptions");
// Creates the window used for the UI
if (userInput != null) {
if (userInput.get(0) != null) {
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(userInput.get(0));
}
}
}
Error occurs at this line:
if (userInput != null) {
//of complainResponse.java
Logcat:
java.lang.IndexOutOfBoundsException
Please help
There's nothing in the ArrayList that you pass to your activity.
I suspect this bit of code isn't being executed -
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS"); <------------ never gets here
}
To verify this, run the application in debug mode, and place a breakpoint at the if statement
userInput.get(0) != null
this is the cause of error in my opinion, list can be initialized but empty.
instead you should use,
if (!userInput.isEmpty())