0

I am creating an app that requires a user to login before enter the main app. When he hits the "login" button, it should check to make sure that the email/username and password are correct before allowing the user into the app or else give them an error. I have been having a hard time trying to figure out how to do this. Here is my main_activity pages which show the main login screen.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:orientation="vertical">

 <!--  Email Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="@string/Email"/>

                <EditText
                    android:id="@+id/Email_enter"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dip"
                    android:layout_marginTop="5dip"
                    android:hint="@string/Email_enter"
                    android:singleLine="true" 
                    android:inputType="textEmailAddress"/>

                <!--  Password Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="@string/edit_message2"/>

      <EditText
          android:id="@+id/Password_enter"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:hint="@string/Password_enter"
          android:inputType="textPassword"
          android:singleLine="true" />

      <!-- Login button -->

      <Button android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="@string/Login"
            android:onClick="sendMessage"/>

      <Button
          android:id="@+id/query"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Query"
          android:onClick="View arg0"/>

Please ignore the "query" button for now. Here is the java file:

    package com.example.awesomefilebuilder;


    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    public class MainActivity extends Base_Activity {

    public final static String EXTRA_MESSAGE = "com.example.awesomefilebuilder.MESSAGE";
    public final String TAG = "MainActivity";
    public final String edit_message ="Username";
    public static final String DATABASE_NAME = "data";
    public static final String TABLE_NAME = "comments_table";
    public static final String C_ID = "_id";
    public static final String NAME = "name";
    public static final String COMMENT = "comment";
    public static final String EMAIL = "email";
    public static final String TIME = "time";
    public static final String PASSWORD = "password";
    public static final int VERSION = 1;

    View view;
    SQLiteDatabase db;
    DbHelper dbhelper;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        DbHelper dbhelper = new DbHelper(getApplicationContext());
        db = dbhelper.getWritableDatabase();
        view = inflater.inflate(R. layout.activity_main, container,false);


            ContentValues cv = new ContentValues();


        cv.put(DbHelper.NAME, NAME);
        cv.put(DbHelper.COMMENT, COMMENT);
        cv.put(DbHelper.EMAIL, EMAIL);
        cv.put(DbHelper.PASSWORD, PASSWORD);
        db.insert(DbHelper.TABLE_NAME, null, cv);


    Button query = (Button) view.findViewById(R.id.query);
    query.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            String [] columns = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.EMAIL};

            Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
            cursor.moveToFirst();

                while(cursor.moveToNext())
                {
                    String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
                    String comment = cursor.getString(cursor.getColumnIndex(DbHelper.COMMENT));
                    String password = cursor.getString(cursor.getColumnIndex(DbHelper.PASSWORD));
                    String email = cursor.getString(cursor.getColumnIndex(DbHelper.EMAIL));

                    Toast.makeText(getApplicationContext(), "Name = "+ name +"/nComment= "+ comment,Toast.LENGTH_SHORT).show();
                }

                cursor.close();




        }

    });
        return view;


    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate" );

}

@Override
public void onDestroy() {
    super.onDestroy();  // Always call the superclass

    // Stop method tracing that the activity started during onCreate()
    android.os.Debug.stopMethodTracing();
}


/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, HomePageMain.class);
    EditText editText = (EditText) findViewById(R.id.Email_enter);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    Log.i(TAG, "sendMessage" );
    }

};

When the user hits the Login button, I want the app to check the data before allowing them in or giving an error. How do I do this here? After that, if the data does check out correctly, I want the app to send the user to the next main page called HomePageMain.class.

If any other pages are needed, please let me know. Thanks in advance! I really apperciate it.

John Conde
  • 217,595
  • 99
  • 455
  • 496
user2712795
  • 65
  • 1
  • 3
  • 11

1 Answers1

0

Its a broad question. What you might need to do is you would have to detect when user is running the app for the first time. On first time login you can ask the user to enter a username and Password. Now the simplest ( but might not be the best) way is to save it in SharedPreferences. Then when user come to the app next time, you can check it from the SharedPreference and if login is correct then continue, else show an error.

Inside your sendMessage function you can first check if anything is stored in sharepreference with a particular key that you've used for username and password. If its not saved then user is coming for first time. Then strat a new Intent where you can save it. If there is some saved things, then you can extract thme from shared preference and compare the values against the entered values.

Now its not a good idea to save password in SharedPreference as on rooted devices those can be accessed. You can use hashing/encrypting on the password.

See remember password in android app

SharedPreferences for login in android and

Best option to store username and password in android app

for the code sniplets and other logic.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • After looking around as well I was wondering if this site would be a good idea to use as a template:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ What do you think? – user2712795 Aug 26 '13 at 20:02
  • Ooo that's seems to be a pretty good template if you want to involve server side as well. I'll bookmark it as well. Do comment later and tell me how it goes. :) – Shobhit Puri Aug 26 '13 at 20:05
  • So I got all of that in and when I run it via my mom's android I get the error that it has stopped unexpectedly. Do you think you can take a quick look at what might be wrong? I have no errors in eclipse and only a few warnings saying certain variables aren't used. I'll post a link to it once I get the question up... – user2712795 Aug 26 '13 at 22:50
  • Sure. Just post a new question and comment here with a link. Lets keep both questions separate so that others may also contribute to solving your problem. Reconnect the device to eclipse. Error in the LogCat would be there if it has crashed. – Shobhit Puri Aug 26 '13 at 23:36
  • http://stackoverflow.com/questions/18454460/the-application-has-stopped-unexpectedly heres the link. I agree. So far what everyone has said hasn't worked so this is quite interesting O.O – user2712795 Aug 26 '13 at 23:39
  • Ok the problem was that the manifest was wrong. It works quite well now :) the link to the registration works and everything. That site also has how to build the actual database as well if you want to look into it for future reference :D – user2712795 Aug 26 '13 at 23:46