Hi Folks !
Scratching your head developing a android login module without sql lite database ? Here is exact tutorial to create login system where you can validate username and password.
No database nothing. You need to specify username and password in your java file.
If you are in GTU then you must check this program
GTU MCA SLMC Program 2
- To understand Activity, Intent .
- a. Create sample application with login module.(Check username and password)
- b. On successful login, go to next screen. And on failing login, alert user using Toast.
- c. Also pass username to next screen
Lets get started.
Design a Two Screens | First to Let user enter username and passoword and Second to show welcome message.
- Take 2 editText and give a unique id.
- Put a button for onclick event.
Here is the code of activity_main.xml
<RelativeLayout 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:background="@drawable/balloon_bg"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/linearlayout_bg" android:padding="10dp" > <Button android:id="@+id/btnSignUp" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_margin="4dp" android:text="Sign Up" android:background="@drawable/button_default_bg" style="@style/DefaultButtonText" /> <Button android:id="@+id/btnSingIn" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_margin="4dp" android:text="Sign In" style="@style/DefaultButtonText" android:background="@drawable/button_default_bg" /> </LinearLayout> </RelativeLayout>
Here is your next screen to show your welcome message.
second.xml
You just need to take one textview to show the message to the user.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"> <TextView android:text="Welcome" android:textSize="20dip" android:id="@+id/showmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center"></TextView> </LinearLayout>
Now actual coding part starts in this java file which is given below
package kamlesh.login; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Login extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button btn_save = (Button)findViewById(R.id.button1); btn_save.setOnClickListener(new OnClickListener(){ public void onClick(View v){ final TextView username =(TextView)findViewById(R.id.editText1); final TextView password =(TextView)findViewById(R.id.editText2); String uname = username.getText().toString(); String pass = password.getText().toString(); if(uname.equals("kamlesh") && pass.equals("kamlesh")) startActivity(new Intent(Login.this,LoginSuccess.class).putExtra("usr",(CharSequence)uname)); else Toast.makeText(Login.this,"Invalid UserName or Password", Toast.LENGTH_LONG).show(); } }); } }
LoginSuccess.java to show Successfull login message.
This is layout we are just fetching the username from main acitvity with “usr” variable.
package kamlesh.login; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class LoginSuccess extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); Intent in = getIntent(); if (in.getCharSequenceExtra("usr") != null) { final TextView setmsg = (TextView)findViewById(R.id.showmsg); setmsg.setText("Welcome n "+in.getCharSequenceExtra("usr")); } } }
Strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, LoginSystem!</string> <string name="app_name">Login System</string> <string name="username">kamlesh</string> <string name="password">kamlesh</string> <string name="btn_txt">Save</string> </resources>
These are the only files you need to build your login system that checks your username and password.
If you face any errors feel free to comment.