How to implement more into this code so that it returns a message telling the user to 'select at least one checkbox' if he/ she does not select any?
public void verificaCheckBox() {
Listcheck.clear();
if (cbPapel.isChecked())
Listcheck.add(cbPapel.getText().toString());
if (cbPlastico.isChecked())
Listcheck.add(cbPlastico.getText().toString());
if (cbMetal.isChecked())
Listcheck.add(cbMetal.getText().toString());
if (cbVidro.isChecked())
Listcheck.add(cbVidro.getText().toString());
cbSelecionado = (Listcheck.toString());
}
public String verificaCheckBox(){
Listcheck.clear();
if (cbPapel.isChecked())
Listcheck.add(cbPapel.getText().toString());
if (cbPlastico.isChecked())
Listcheck.add(cbPlastico.getText().toString());
if (cbMetal.isChecked())
Listcheck.add(cbMetal.getText().toString());
if (cbVidro.isChecked())
Listcheck.add(cbVidro.getText().toString());
cbSelecionado = (Listcheck.toString());
return Listcheck.isEmpty() ? "Message goes here" : "";
}
Then wherever calls it can check to see if the return is an empty string or the message. If you just want to show the user a message you could do this:
public void verificaCheckBox(){
Listcheck.clear();
if (cbPapel.isChecked())
Listcheck.add(cbPapel.getText().toString());
if (cbPlastico.isChecked())
Listcheck.add(cbPlastico.getText().toString());
if (cbMetal.isChecked())
Listcheck.add(cbMetal.getText().toString());
if (cbVidro.isChecked())
Listcheck.add(cbVidro.getText().toString());
cbSelecionado = (Listcheck.toString());
if(Listcheck.isEmpty()) {
Toast.makeText(applicationContext, "Your message", Toast.LENGTH_SHORT).show();
}
}
Try using an Alert Dialog to accomplish that.
I believe that the following link gives more relevant details on how to do so:
How to add message box with ok button
To put it for you in simpler steps:
Add an else-statement at the end of your code.
Use the Alert Dialog to notify the user of the changes needed to be done.
I am not sure that I have understood your question, but if you want to send message to the user you can use a Toast
For example:
if (Listcheck.size() == 0)
Toast.makeText(this, "select at least one checkbox", Toast.LENGTH_SHORT).show();
Related
I'm trying to login using Parse for android.
If I enter the correct username and password, I log in successfully.
But when I use a wrong password or username, I always get error 101: object not found.
Here's the code (Notice "username" and "password" are EditText):
private void doLogin() {
if (!validate()) { // IF VALIDATION FAILS, DO NOTHING
return;
} // ELSE...
String name = email.getText().toString();
String pass = password.getText().toString();
ParseUser.logInInBackground(name, pass, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
goToMainActivity(user.getUsername());
} else {
handleParseError(e);
}
}
});
}
Thanks for your help.
Update: Parse does not have means to check if there was an incorrect login field. Hence they use the general 101: Object Not Found error to catch it. Reference: https://parse.com/docs/android/api/com/parse/ParseException.html
Previous stackoverflow link: Parse : invalid username, password
If you want your app to respond to an incorrect login, just replace the line handleParseError(e); with code to handle it.
For example, if you want a message box to show up, place that code there. If you do not want to do anything, comment out that line. Not sure what else you are looking for...
I would suggest replacing it with a Toast message
I know that System.out.println() does not work on Android.
So I need another way to print out some text.
Please help me.
I'm using the Root Tools library
class superuser {
public static Command c ;{
if (RootTools.isRootAvailable()) {
System.out.print("Root found!!");
}
else{ System.out.print(("NO ROOT!"));
}
}
}
Outputing text in android
There are many ways, but usually for testing and debugging processes we use log. The log is not visible to user but you can see it in DDMS.
From what i understand you want to create a dialog or display a textview to users so they know if root is available or not.
1.Logging(for testing and debugging processes)
we defined TAG in below code because it would be easy to make changes later and our code is more organised
private static final String TAG = MyActivity.class.getName();
Log.v(TAG , "here is the line i want to output in logcat");
here v in log.v stands for verbose.You may use i for info, e for error etc.
2.Displaying text to user via a TextView
First lets import the textview. Let the id of the textview you imported may be "resultTextView"
TextView resultText = (TextView) findViewById(R.id.resultTextView);
now applying your logic and setting its text...
if (RootTools.isRootAvailable()) {
resultText.setText("Root found!!");
}
else{ resultText.setText(("NO ROOT!"));
}
3.Creating a dialog
Dialogs are the pop out messages we get.
I would recommend creating a function that takes String message and a String Title as a parameter and creates a dialog using dialog.builder something like this rather than a dialog fragment(which is available in the below link) - http://developer.android.com/reference/android/app/AlertDialog.Builder.html
public void alertDialog(String message,String title){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
// set title
alertDialogBuilder.setTitle(title);
// set dialog message
alertDialogBuilder
.setMessage(message)
.setPositiveButton("OK", null) //we write null cause we don't want
//to perform any action after ok is clicked, we just want the message to disappear
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
Now you can call the method with title and text you want :)
if(condition){
Log.d("message","The root found");
}
else{
Log.d("message","The root not found");
}
Have you tried using logcat ? http://developer.android.com/reference/android/util/Log.html
use it like so: Log.v("myAwesomeApp", "my developer comment");
then use the log panel in your IDE to read it
Using logcat is the usual way in Android, and the simplest.
Log.d("message_id","message content");
If you want another way to display log, you can try
https://github.com/orhanobut/logger
I have a Swing GUI where I am restricting the user registration so that the username and the password cannot be the same. I am using JoptionPane for the task with the following code:
public void actionPerformed(ActionEvent e) {
String username = tuser.getText();
String password1 = pass1.getText();
String password2 = pass2.getText();
String workclass = wclass.getText();
Connection conn = null;
try {
if(username.equalsIgnoreCase(password1)) {
JOptionPane.showMessageDialog(null,
"Username and Password Cannot be the same. Click OK to Continue",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
...
The problem is that I had to use System.exit(0); without it, the next code was getting executed. Even after the JOptionPane poped up, the registration was succeeding. I do not need the system to exit, but I need the user to be kept on the registration page after the validation. What is the best way to do this? Is there other convenient ways of doing this rather than using the JOptionPane?
Replace
System.exit(0);
with
return;
if you do not want the rest of the method to be performed
You need to place your code within endless loop, and break it upon successful result. Something like:
while(true)
{
// get input from user
if(vlaidInput) break;
}
place that next code into else part may be it works
if(username.equalsIgnoreCase(password1))
{
JOptionPane.showMessageDialog(null, "Username and Password Cannot be the same. Click OK to Continue","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
//place that next code
// username and password not equals, then it will execute the code
}
First of all, it is best if the UI and business logic (in this case, validation) are separated. Have them separate sort of suggest a better way of handling interaction on its own. Thus, it makes sense to create a separate class UserValidation with method boolean isValid(). Something like this:
public class UserValidation {
private final String name;
private final String passwd;
private final String passwdRpt;
public UserValidation(final String name, final String passwd, final String passwdRpt) {
this.name = name;
this.passwd = passwd;
this.passwdRpt = passwdRpt;
}
public boolean isValid() {
// do your validation logic and return true if successful, or false otherwise
}
}
Then the action code would look like this:
public void actionPerformed(ActionEvent e) {
if (new UserValidation(tuser.getText(), pass1.getText(), pass2.getText()).isValid()) {
// do everything needed is validation passes, which should include closing of the frame of dialog used for entering credentials.
}
// else update the UI with appropriate error message -- the dialog would not close by itself and would keep prompting user for a valid entry
}
The suggested approach gives you a way to easily unit test the validation logic and use it in different situations. Please also note that if the logic in method isValid() is heavy than it should be executed by a SwingWorker. The invocation of SwingWorker is the responsibility of the action (i.e. UI) logic.
I'm looking for a way to show a second list in a preference, after a user has selected a choice in a listpreference
For example : the user selects the option "Send sms to" from a list, then a second list appears, and the user can choose a contact.
At the moment, i'm trying to put a onSharedPreferenceChanged method from my preference activity, and show an alert Dialog containing the contacts after a selection, but i think there is another way... But i havent found it yet on the Internet...
Does anyone know how it is possible ?
Thank's
In your PreferenceActivity put a method like below that listens for when that specific key is clicked.
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
//Make sure the item changed was the list_preference
if(key.equals("list_preference")) {
String value = sharedPreferences.getString(key, "Nothing");
if(value.equals("Send_sms")) {
//launch AlertDialog with list or launch new preference
}
}
}
I created a login application. But when I input a wrong password, the app won't give me any notification that the password was incorrect and it's because I didn't even put any codes for it. I don't know what to put in order to achieve it. I'm a newbie here. Please help.
This is my code:
public void onClick(View v)
{
EditText passwordEditText = (EditText) findViewById(R.id.currentPass);
SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
String password = prefs.getString("password","");
if("".equals(password))
{
Editor edit = prefs.edit();
edit.putString("password",passwordEditText.getText().toString());
edit.commit();
StartMain();
}
else
{
if(passwordEditText.getText().toString().equals(password))
{
StartMain();
}
}
}
You probably want an else condition on your inner if statement:
if(passwordEditText.getText().toString().equals(password)) //checking if they put the right password?
{
StartMain(); //I assume this is starting the application
}
else
{
//Tell them the password was wrong.
}
The simplest think would be to show a Toast notification. You could do that in the else branch of the second (nested) if in your code above.
Hello schadi:
i think you problem is this:EditText catch the click event as first as you click it that you should want to input some password.so,your code should always running at '"".equals(password)' statement.
you may can use TextWatcher or something else to do your work.
Chinese guy,English is poor,i hope you can understand what i am saying :)
I suggest using a toast to notify the user. Like this:
else
{
if(passwordEditText.getText().toString().equals(password))
{
StartMain();
}
} else {
Toast.makeText(v.getContext(), "Wrong password!", Toast.LENGTH_LONG).show();
}