Question

Getting prob in following code.Can anybody suggest how to write if/else if I want to show...

Getting prob in following code.Can anybody suggest how to write if/else if I want to show hint as username/Paswword required if null and password or username did not match if entered wrong and go to next activity if its right.??



public class MainActivity extends AppCompatActivity {
    EditText userName, pwd;
    DatabaseHelper helper = new DatabaseHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

public void onClickOk(View v) {

    if (v.getId() == R.id.LogIn) {
        final EditText User = (EditText) findViewById(R.id.userName);
        String t1 = User.getText().toString();
        final EditText pawd = (EditText) findViewById(R.id.pwd);
        String t2 = pawd.getText().toString();
        String password = helper.searchpass(t1);
if (t2.equals(password)) {
        Intent lo = new Intent(MainActivity.this, Timer.class);
        startActivity(lo);


    if (t2!=password){
    Toast.makeText(MainActivity.this, "Pass does'nt match", Toast.LENGTH_LONG).show();
}}
              //if(t1==null && t2==null) {
               else
{
                User.setHint("Username Required");
                pawd.setHint("Password Required");

            }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, since you have provided very little details, I can provide limited help. But I assure you this solution will solve your problem. The main problems in your code are some misplaced curly braces and some wrong conditional checks. I assume that you put your code to call the onClickOk() inside xml document. And I suppose the DatabaseHelper class is working fine.

The code will fetch texts from username and password fields, checks if the password is correct, and if it is correct, it will move to the next activity, else it will first check if the fields are empty (not to be confused with null, both are different), if yes, display the hint messages in proper fields, else display the password wrong message via Toast.

Here is the corrected code. Output not included as I don’t have all the required inputs.

public class MainActivity extends AppCompatActivity {

                EditText userName, pwd;

                DatabaseHelper helper = new DatabaseHelper(this);

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                }

                public void onClickOk(View v) {

                                if (v.getId() == R.id.LogIn) {

                                                final EditText User = (EditText) findViewById(R.id.userName);

                                                String t1 = User.getText().toString();

                                                final EditText pawd = (EditText) findViewById(R.id.pwd);

                                                String t2 = pawd.getText().toString();

                                                String password = helper.searchpass(t1);

                                                if (t2.equals(password)) {

                                                                /**

                                                                * Right password, moving to the next activity

                                                                */

                                                                Intent lo = new Intent(MainActivity.this, Timer.class);

                                                                startActivity(lo);

                                                } else {

                                                                /**

                                                                * Wrong or empty username/password

                                                                */

                                                                if (!t2.equals("")) {

                                                                                /**

                                                                                * Password is not empty, but wrong

                                                                                */

                                                                                Toast.makeText(MainActivity.this, "Pass does'nt match",

                                                                                                                Toast.LENGTH_LONG).show();

                                                                }

                                                                if (t1.equals("")) {

                                                                                /**

                                                                                * user name is empty

                                                                                */

                                                                                User.setHint("Username Required");

                                                                }

                                                                if (t2.equals("")) {

                                                                                /**

                                                                                * Password is empty

                                                                                */

                                                                                pawd.setHint("Password Required");

                                                                }

                                                }

                                }

                }

}

Add a comment
Know the answer?
Add Answer to:
Getting prob in following code.Can anybody suggest how to write if/else if I want to show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this java code allows to proceed to next activity even if no data is enetered. add...

    this java code allows to proceed to next activity even if no data is enetered. add a constraint which displays a message to fill all fields. 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.Toast; public class SignupActivity extends AppCompatActivity { EditText editTextName,editTextEmail,editTextPhone,editTextPswd,editTextCPswd,editTextAge,editTextFatherName; Button btnMoveToLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signup); editTextName=findViewById(R.id.editTextName); editTextFatherName=findViewById(R.id.editTextFatherName); editTextAge=findViewById(R.id.editTextAge); editTextPhone=findViewById(R.id.editTextPhoneNumber); editTextPswd=findViewById(R.id.editTextPswd); editTextCPswd=findViewById(R.id.editTextCPswd); editTextEmail=findViewById(R.id.editTextEmail); btnMoveToLogin=findViewById(R.id.buttonMoveToLogIn); btnMoveToLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("Name",editTextEmail.getText().toString()); if(editTextEmail.getText().toString()=="" &&...

  • this customer java code allows to proceed to next activity even if no data is enetered....

    this customer java code allows to proceed to next activity even if no data is enetered. add a constraint which displays a message to fill all fields. import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.SpinnerAdapter; import java.util.ArrayList; import java.util.List; public class CustomerActivity extends AppCompatActivity { EditText editTextName, editTextEmail,editTextPhone, editTextMenu; Button btnBook; Spinner s; String packageName; SpinnerAdapter spinnerAdapter; List<String> packages=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_customer);...

  • Edit question this login java code allows to proceed to next activity even if no data...

    Edit question this login java code allows to proceed to next activity even if no data is enetered. add a constraint which displays a message to fill all fields. package com.example.Divahalls; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class LoginActivity extends AppCompatActivity { Button btnMoveToDashboard; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); btnMoveToDashboard=findViewById(R.id.buttonMoveToDashboard); btnMoveToDashboard.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(LoginActivity.this,CustomerActivity.class); startActivity(intent); } }); } }

  • Adroid Studio Error View.setOnClickListener Error: Class 'anonymous class derived from OnClickListener ' must either be declared...

    Adroid Studio Error View.setOnClickListener Error: Class 'anonymous class derived from OnClickListener ' must either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener ' @Override Error: Method does not override method from its superclass package com.example.gregorybacon.assignment_1; import android.content. Intent; import android. support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view. View; import android.widget.Button; public class MainScreen extends AppCompatActivity f @0verride protected void onCreate(Bundle savedInstanceState) ( super.onCreate(savedInstanceState); setContentView(R. layout.activity main_screen); Button button_unlock-(Button) findViewById(R.id.button_unlock); button_unlock.setOnClickListener(new View.OnClickListener) L aoverride public void OnClick(View view) startActivity(new Intent( packageContext:...

  • Why is my app crashing? Following is an app that takes notes from users and shows...

    Why is my app crashing? Following is an app that takes notes from users and shows it's encrypted string. It also decrypt and encrypted strings. It saves the note in a file and also loads it back. Please help me identify what I am doing wrong? activity_krypto_note.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".KryptoNoteActivity"> <EditText android:id="@+id/key" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:hint="Cryptographic Key" android:inputType="number" app:layout_constraintEnd_toStartOf="@+id/encrypt" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/encrypt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:onClick="enryptClicked" android:text="ENCRYPT" android:textSize="9sp"...

  • I should show tables with info after clicking on the buttons in Android Studio. I still...

    I should show tables with info after clicking on the buttons in Android Studio. I still can not get what I should write inside OnClick function and how to use containers for printing tables. Please help me! import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.text.Layout; import android.view.LayoutInflater; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { LinearLayout container; ListView ListView1; ListView ListView2; ListView ListView3; @Override protected void onCreate(Bundle savedInstanceState)...

  • Need help to edit the code below. Am using it play a lottery game in Android...

    Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. Here is my activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...

  • Need help to edit the code below. Am using it play a lottery game in Android...

    Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. Here is my activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...

  • Need a help with this app. when I run this app, it is stopped. Here a...

    Need a help with this app. when I run this app, it is stopped. Here a source code for Tip calculator application. activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFF" android:id="@+id/tableLayout" android:stretchColumns="1,2,3" android:padding="5dp"> <!-- tableRow0 --> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow0"> <TextView android:id="@+id/billTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/billTotal" android:textColor="#000" android:gravity="right" android:paddingRight="5dp"> </TextView> <EditText android:layout_width="wrap_content" android:id="@+id/billEditText" android:layout_height="wrap_content" android:layout_span="3" android:inputType="numberDecimal" android:layout_weight="1"> </EditText> </TableRow> <!-- tableRow1 --> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow1"> <TextView android:id="@+id/tenTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10%" android:textColor="#000" android:layout_column="1"...

  • Need help to build a fingerprint app that we have sslserversocket to tell a file use finger print...

    need help to build a fingerprint app that we have sslserversocket to tell a file use finger print before open the file what have been done below Here is my mainactivity.java and fingerprintHandler.java below <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:padding="20dp" android:text="Fingerprint Authentication" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/paraMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Place your finger on the fingerprint scanner to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT