I want to create a simple login app in android studio in that I want to fetch first time username and password from local database sqlite and after that when we log in then the imei number of device can store in database... then when we try to log in next time it will login through only that imei number
What I have tried:
So This is MainActivity.java file In this File that Insert method Has Some problem
package com.example.newlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.LoginFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputLayout;
public class MainActivity extends AppCompatActivity {
Button login;
DatabaseHandler sqLiteHelper;
String SQLiteDataBaseQueryHolder ;
SQLiteDatabase sqLiteDatabaseObj;
String imeistr;
TextInputLayout userview ;
TextInputLayout passview ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqLiteHelper = new DatabaseHandler(this);
login = (Button) findViewById(R.id.button1);
userview = (TextInputLayout) findViewById(R.id.Username);
passview = (TextInputLayout) findViewById(R.id.Password);
userview.getEditText().addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
passview.getEditText().addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
{
SQLiteDataBaseBuild();
SQLiteTableBuild();
InsertDataIntoSQLiteDatabase();
} }
});
}
public void SQLiteDataBaseBuild(){
sqLiteDatabaseObj = openOrCreateDatabase(DatabaseHandler.DATABASE_NAME, Context.MODE_PRIVATE, null);
}
public void SQLiteTableBuild() {
sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS " + DatabaseHandler.TABLE_NAME + "(" + DatabaseHandler.KEY_User + " PRIMARY KEY , " + DatabaseHandler.KEY_PASS + " STRING);");
}
public void InsertDataIntoSQLiteDatabase(){
if (userview.getEditText().getText().toString().equals("admin") &&
passview.getEditText().getText().toString().equals("admin"))
{
SQLiteDataBaseQueryHolder = "INSERT INTO " + DatabaseHandler.TABLE_NAME + " (username,password) VALUES('" + userview + "', '" + passview + "');";
sqLiteDatabaseObj.execSQL(SQLiteDataBaseQueryHolder);
Toast.makeText(MainActivity.this, "User Login Successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, wv.class);
startActivity(intent);
}
else {
Toast.makeText(MainActivity.this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show();
}
}
}
This is My DatabaseHandler class
package com.example.newlogin;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
static final String DATABASE_NAME = "Galaxy";
public static final String TABLE_NAME = "LOGINUSER";
public static final String KEY_User = "username";
public static final String KEY_PASS = "password";
public static final String KEY_IMEI_NO = "imei_number";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +KEY_User+ "STRING , "+
KEY_PASS +" STRING,"+ KEY_IMEI_NO + "STRING"+ " )";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
This is my Activty_main.xml file
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
="" android:layout_height="match_parent"
="" android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:padding="20dp" android:background="#fed8b1">
<imageview
android:id="@+id/Logo_Image"
="" android:layout_width="420dp" android:layout_height="150dp" android:src="@mipmap/neumann_logo_final">
<textview
android:id="@+id/Logo_Name"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:fontfamily="sans-serif-black" android:text="Welcome" android:textcolor="#B71C1C" android:textsize="40sp">
<linearlayout
android:layout_height="wrap_content"
="" android:layout_width="match_parent" android:layout_margintop="20dp" android:layout_marginbottom="20dp" android:orientation="vertical">
<com.google.android.material.textfield.textinputlayout
android:layout_height="wrap_content"
="" android:layout_width="match_parent" android:id="@+id/Username" android:hint="UserName" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.textinputedittext
android:layout_width="match_parent"
="" android:layout_height="60dp" android:background="#FF5722" android:textappearance="@style/TextAppearance.AppCompat.Large">
<com.google.android.material.textfield.textinputlayout
android:layout_height="wrap_content"
="" android:layout_width="match_parent" android:id="@+id/Password"
="" android:hint="Password" app:passwordtoggleenabled="true" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.textinputedittext
android:layout_width="match_parent"
="" android:layout_height="wrap_content" android:background="#FF5722" android:textappearance="@style/TextAppearance.AppCompat.Large">
<button
android:id="@+id/button1"
="" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#800000" android:paddingbottom="4dp" android:text="Login" android:textappearance="@style/TextAppearance.AppCompat.Large">
so please what is the problem in this code
EDIT: Added from comment:
my apllication run very properly but the LOGINUSER table saves the entry like:
com.google.android.material.textfield.TextInputLayout{16aa1a2 V.ED..... ........ 0,0-1300,229 #7f080011 app:id/Username aid=1073741828}
Now what change can i do to store table values as admin