Solved

Android user login page

Posted on 2012-03-11
12
449 Views
Last Modified: 2012-06-27
Hello there,

i am developing a simple login UI,were user will login and go to the main page. the problem i have is that i get exception when i try to return the user back to the login page when they enter wrong credential.it is working fine when the user enters the correct credential.i have attached the files.please help.
Login.java
MainMenu.java
0
Comment
Question by:zolf
  • 7
  • 4
12 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 37708382
sorry for the dumb question, but why don't you check for credentials validity in Login class?
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37708510
Hi  zolf,
if(username.equals("test") && password.equals("secret"))
            {
                  setContentView(R.layout.mainmenu);

            //Radio button is available in the mainmenu.xml. So move this piece of code inside this if block
                  RadioButton order = (RadioButton) findViewById(R.id.radio_order);
                  RadioButton cusInfo = (RadioButton) findViewById(R.id.radio_customer);

                  order.setOnClickListener(new OnClickListener()
                  {
      
                        @Override
                        public void onClick(View v)
                        {
                              Intent intent = new Intent(MainMenu.this, Order.class);
                              startActivity(intent);
                        }
                  });

                  cusInfo.setOnClickListener(new OnClickListener()
                  {
      
                        @Override
                        public void onClick(View v)
                        {
                              // TODO Auto-generated method stub
                        
                        }
                  });
            }
            else
            {
                  Log.d("ZAJ","Inside ELSE Loop");
                  Intent intent = new Intent(MainMenu.this, Login.class);
                  startActivity(intent);
                  
                  //setContentView(R.layout.main);
            }




This will work if you enter wrong username & password it will launch the Loginscreen again.
But i guess it is not a correct way to implement this login screen.


1) In Login.java

LOGIN_RESULT = 100;
....
startActivityForResult(intent,LOGIN_RESULT);
.....

override the onActivityResult(int reqCode, int resCode, Intent data){
        if(LOGIN_RESULT == reqCode){
              finish();
        } else {
           username.setText("");  
           password.setText("");  
        }
}


2) In MainMenu.java


in ocCreate(){
.......

        if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
            && username.equals("test") && username.equals("secret")
            ){
            setResult(RESULT_OK);
              finish();
        } else {
      
            setResult(RESULT_CANCEL);
            finish();
            return;
        }
      
      setContentView(R.layout.mainmenu);
.......
}
0
 

Author Comment

by:zolf
ID: 37713600
santhanasamy

thanks for your comments and feedbacks.
Can you please give a bried description to the implementation which you suggested. i will appreciate it.

cheers
Zolf
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37718357
Hi  zolf,

In your implementation you are launching the Login activity every time if the username/ password is wrong. So every time it will create a new activity instance if the user enter wrong username/ password ,,,,,,

So i Called the "MainMenu" for result(onActivityResult(...)).  It will return the result to the "Login" activity.
If the result is success it will finish the "Login activity".Other wise it will just clear the texts that we entered previously in the username & password field...

In MainMenu activity if the username&password is correct we will send the result "RESULT_OK" as result to the Login activity and immediately we are setting the view "R.layout.mainmenu"

Other wise we will send the result "RESULT_CANCEL" as result to the Login activity  and immediately we are finishing  the MainMenu activity...
0
 

Author Comment

by:zolf
ID: 37718555
thanks for your feedbacks.it was very helpful for a beginner like me.
one question, can you please tell me now that in my MainMenu.java the if/else has changed to

if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
            && username.equals("test") && username.equals("secret")
            ){
            setResult(RESULT_OK);
              finish();
        } else {
     
            setResult(RESULT_CANCEL);
            finish();
            return;
        }

where will the code inside of my previous if statement go.
0
 

Author Comment

by:zolf
ID: 37718575
Hi santhanasamy,

can you please look at my code and tell me if i am doing it correct. i am still not clear on some part of the code. see my previuos comment
Login.java
MainMenu.java
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:zolf
ID: 37718666
in the OnActivityResult,shouldnt it be responseCode instead of the reqCode which you mentioned in your code.please clarify
0
 

Author Comment

by:zolf
ID: 37718795
i have made some improvement to the code. but now when i clcik on the radio buttons nothing happens. what am i doing wrong.please see my code.
one question, in the Login class the onActivityResult method,in the if do i have to use setContentVIew or I have to use Intent to go to the MainMenu once the lgin is successful.??
Login.java
MainMenu.java
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37719171
Hi  zolf,

Please check this code....
It is perfectly working to me....Ehen i select the radio button it is launching the order activity...
Login.java
MainMenu.java
0
 

Author Comment

by:zolf
ID: 37731915
Hi santhanasamy

thanks for your feedback. why do we have to call finish() in here.i mean what does it do.

@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
            if (LOGIN_RESULT == requestCode)
            {
                  if (resultCode == RESULT_OK)
                  {
                         finish();


also i want to know the difference between calling setContentView and Intent when i want to switch to another activity.
0
 
LVL 8

Accepted Solution

by:
santhanasamy earned 500 total points
ID: 37732194
Hi  zolf,

If you didn't call the finish ,Login screen will be in the back ground.
If the user enter correct username and password ,it will take to the MainMenu screen.
The default implementation of the backkey will launch the previous activity.So If you you press the back key  from the MainMenu activity (without calling finish ()) , it will launch the LoginActivity.

Intent & setContentView both are not inter relatedwith each other.

setContentView  -- >  Will give the User interface (Views) to an Activity through the layout resource xml.

Intent  --> Android component communicate with each other through the asynchronous messages called Intents.
Geneally Intent object is a passive data structure will hold the abstract information of the operation need to be performed....

All the 3 android components (Activity, Service,  BroadcastReceivers) are activated through this messages called intent.

To know more about Intent refer following links..
http://www.vogella.de/articles/AndroidIntent/article.html
http://developer.android.com/guide/topics/intents/intents-filters.html
0
 

Author Closing Comment

by:zolf
ID: 37734208
cheers mate
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
Introduction Everyone is using a smartphone these days. Contacts, messages, applications, documents, bookmarks, pictures, videos and other stuff which you created and are present only on your device come under the umbrella called “Personal Data”.…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now