Solved

Android user login page

Posted on 2012-03-11
12
454 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:Santhana
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 8

Expert Comment

by:Santhana
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
 

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:Santhana
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:
Santhana 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android Box Xibo software 3 122
messenger app crashes 7 54
IT Company 5 78
Android Sat Nav Apps No Voice Instructions 2 43
First off, some of you reading this are already thinking there is an error in the title. It really should be Kindle Fire versus OTHER Android tablets. That's because the Amazon Kindle Fire IS an Android tablet. Well, it is and it's not. It's running…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

786 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