Android studio crashes after i press button, with 0 errors - android

When i press login button app doesnt close but puts me in desktop mode on phone than i go again on the app and try to "login" and than it crashes
package com.example.coronaapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText username = findViewById(R.id.username);
final EditText password = findViewById(R.id.password);
Button login = findViewById(R.id.login);
Button register = findViewById(R.id.register_btn);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// čia bus vykdomas kodas, kai paspaudžiamas mygtukas
// Kuriamas User klases objektas
// public User(String username, String password)
User user = new User(username.getText().toString(), password.getText().toString());
Toast.makeText(LoginActivity.this, "prisijungimo vardas:" +
user.getUsername() + "\n" + "slaptazodis:" +
user.getPassword(), Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,
username.getText().toString(),
Toast.LENGTH_SHORT).show();
username.setError(null);
if (Validation.isValidUsername(username.getText().toString())) {
Intent goToSearchActivity = new Intent(LoginActivity.this, SearchActivity.class);
startActivity(goToSearchActivity);
password.setError(null);
}
if (Validation.isValidUsername(password.getText().toString())) {
Intent goToSearchActivity = new Intent(LoginActivity.this, SearchActivity.class);
startActivity(goToSearchActivity);
} else {
username.setError("Error Wrong username");
username.requestFocus();
password.setError("Wrong Password");
password.requestFocus();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent goToRegisterActivity = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(goToRegisterActivity);
}
});
}
}
im new to java, i tried many ways that i found on stackoverflow but it just doesnt work. Even if i delete half the code and try to go with only validation after click still crashes

Related

Android studio button onclick redirect condition when user is logged in and not logged in

I am new to coding, please I need help on this specific function I will be so grateful if anyone could help.
I am working on an android studio project, and on my activity (supposed Activity A) I want to place a button which when clicked it will open Supposed Activity B.
I have been able to setup onclick function and intent to open Activity B on button click.
But now what I want is if the user is not logged in then it should take the user to LoginActivity when that button is clicked, but when user is logged in it should take user direct to Activity B.
Please how can I achieve this on my Activity? Thanks in anticipation of a solution.
Below is my LoginActivity Code
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.sckoolboy.premiumapp.R;
public class Login extends AppCompatActivity {
EditText mEmail,mPassword;
Button mCreateBtn,mLoginBtn;
TextView forgotTextLink;
ProgressBar progressBar;
FirebaseAuth fAuth;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(Login.this, Main1.class));
finish();
}
setContentView(R.layout.activity_login);
mEmail = findViewById(R.id.Email);
mPassword = findViewById(R.id.password);
progressBar = findViewById(R.id.progressBar);
fAuth = FirebaseAuth.getInstance();
mLoginBtn = findViewById(R.id.loginBtn);
mCreateBtn = findViewById(R.id.createText);
forgotTextLink = findViewById(R.id.forgotPassword);
mLoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
mEmail.setError("Email is Required.");
return;
}
if(TextUtils.isEmpty(password)){
mPassword.setError("PremiumKey is Required.");
return;
}
if(password.length() < 6){
mPassword.setError("PremiumKey not valid");
return;
}
progressBar.setVisibility(View.VISIBLE);
// authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(Login.this, "Premium Successfully Unlocked", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Main1.class));
}else {
Toast.makeText(Login.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
mCreateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Register.class));
}
});
forgotTextLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText resetMail = new EditText(v.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext());
passwordResetDialog.setTitle("Reset Password ?");
passwordResetDialog.setMessage("Enter Your Email To Received Reset Link.");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// extract the email and send reset link
String mail = resetMail.getText().toString();
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Login.this, "Error ! Reset Link is Not Sent" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// close the dialog
}
});
passwordResetDialog.create().show();
}
});
}
}
using firebase authentication, how can i go about it from here
You can use an if block to switch between starting different activities, depending on whether the user was logged in or not.
For example, assuming loggedIn is the boolean variable storing whether the user was logged in, and button is the button,
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(v -> {
if (loggedIn) {
// Start ActivityB, user has logged in
Intent intent = new Intent(/* context here */, Main1.class);
startActivity(intent);
} else {
// Redirect to LoginActivity, user hasn't logged in
Intent intent = new Intent(/* context here */, Login.class);
startActivity(intent);
}
});
and the XML design for the new activity:
<!-- Root layout above..... -->
<Button
android:text="Log in"
android:id="#+id/button"/>
<!--Other elements below -->
You can get the login condition from Firebase's API, (reference made to https://stackoverflow.com/a/44585789/12204281), the method call should be:
FirebaseAuth.getInstance().getCurrentUser(), and this method returns null when user is not logged in.
So you can check if that value is null using equals operator and store it in loggedIn:
boolean loggedIn = FirebaseAuth.getInstance().getCurrentUser() != null;

Logging out of an application

I am trying to implement the logout method in my android application
and write the below code. Main Activity is a login activity and ChoosingProcActivity is an activity which contains logout button.
when I press logout button it moves me to Main Activity but when I open application next time it directly moves me to ChoosingProcActivity.
in addition to that when I log in successfully and then go back or press back it show Main Activity (login). how can I avoid this?
are shared preferences wrong?
Main Activity code
package com.example.lenovo.tactic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
EditText email_input,password_input;
TextView reset;
Button btnLogin;
Boolean isLogedBefore = false;
Vibrator v;
String organizer_ID;
SharedPreferences test_name;
final String loginURL = "http://tactickevent.com/phpApp/loginApp.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email_input = findViewById(R.id.etUserName);
password_input = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
test_name = getSharedPreferences("NAME", Context.MODE_PRIVATE);
test_name.getString("email", "");
test_name.getString("organizer_ID", "");
// if(isLogedBefore == true){
boolean is = test_name.getBoolean("isLoged", false);
if (is==true) {
Intent intent = new Intent(MainActivity.this, ChoosingProcActivity.class);
startActivity(intent);
// }
}
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validateUserData();
}
});
}
private void validateUserData() {
//first getting the values
final String email = email_input.getText().toString();
final String password = password_input.getText().toString();
//checking if email is empty
if (TextUtils.isEmpty(email)) {
email_input.setError("أدخل البريد الالكتروني من فضلك");
email_input.requestFocus();
// Vibrate for 100 milliseconds
v.vibrate(100);
btnLogin.setEnabled(true);
return;
}
//checking if password is empty
if (TextUtils.isEmpty(password)) {
password_input.setError("أدخل كلمة السر من فضلك");
password_input.requestFocus();
//Vibrate for 100 milliseconds
v.vibrate(100);
btnLogin.setEnabled(true);
return;
}
//validating email
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
email_input.setError("أدخل بريد الكتروني صحيح");
email_input.requestFocus();
//Vibrate for 100 milliseconds
v.vibrate(100);
btnLogin.setEnabled(true);
return;
}
//Login User if everything is fine
//first getting the values
//String emaill = email_input.getText().toString();
// String passwordd = password_input.getText().toString();
loginUser(email,password);
}
private void loginUser(final String email,final String password) {
RequestQueue queue = Volley.newRequestQueue(this);
//Call our volley library
StringRequest stringRequest = new StringRequest(Request.Method.POST,loginURL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
// Toast.makeText(getApplicationContext(),response.toString(), Toast.LENGTH_SHORT).show();
JSONObject obj = new JSONObject(response);
if (obj.getInt("value")== 1) {
organizer_ID = obj.getString("organizer_ID");
//storing the user in shared preferences
//SharedPref.getInstance(getApplicationContext()).storeID(organizer_ID);
//starting the ChoosingProcActivity
SharedPreferences.Editor editor = test_name.edit();
editor.putBoolean("isLoged",true);
editor.putString("email", email);
editor.putString("organizer_ID", organizer_ID);
//apply
editor.commit();
Toast.makeText(getApplicationContext(), "تم تسجيل الدخول بنجاح", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), ChoosingProcActivity.class));
//finish();
// startActivity(new Intent(getApplicationContext(), ChoosingProcActivity.class));
} else{
Toast.makeText(getApplicationContext(),"هناك خطأ في كلمة السر أو البريد الالكتروني ", Toast.LENGTH_SHORT).show();
//getting user name
//Toast.makeText(getApplicationContext(), obj.getString("messagee"), Toast.LENGTH_SHORT).show();
//storing the user in shared preferences
//SharedPref.getInstance(getApplicationContext()).storeUserName(organizer_ID);
//starting the profile activity
//finish();
//startActivity(new Intent(getApplicationContext(), ChoosingProcActivity.class));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Connection Error"+error, Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
//email key mean the value that will send to php in $_POST["email"];
//password key mean the value that will send to php in $_POST["password"];
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
queue.add(stringRequest);
/*
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, email, password);
try {
JSONObject jsonObject = new JSONObject(result1);
SharedPreferences.Editor editor = test_name.edit();
editor.putBoolean("isLoged",true);
editor.putString("email", email);
editor.putString("organizer_ID", jsonObject.getString("organizer_ID"));
//apply
editor.commit();
*/
}
}
code of second activity
package com.example.lenovo.tactic;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class ChoosingProcActivity extends AppCompatActivity {
Button eventBTN, subeventBTN;
SharedPreferences test_name;
String emailToPass,organizer_ID;
SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choosing_proc);
eventBTN= (Button)findViewById(R.id.BtnEvent);
subeventBTN= (Button)findViewById(R.id.BtnSubEvent);
test_name = getSharedPreferences("NAME", Context.MODE_PRIVATE);
emailToPass= test_name.getString("email", "");
organizer_ID= test_name.getString("organizer_ID", "");
eventBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ChoosingProcActivity.this, EventProcActivity.class);
startActivity(intent);
}
});
subeventBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ChoosingProcActivity.this, SubeventProcActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId() ==R.id.logout)
{
preferences =getSharedPreferences("Name",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
finish();
}
return true;
}
}
You can finish your activity every time the test is true so that it can no longer be accessible from the stack.
boolean is = test_name.getBoolean("isLoged", false);
if (is) {
Intent intent = new Intent(MainActivity.this, ChoosingProcActivity.class);
startActivity(intent);
finish();
}
I am assuming that your MainActivity is the first one to get launched when opening the app.
In your MainActivity add this:
#Override
protected void onStart() {
super.onStart();
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
Intent intent = new Intent(this, ChoosingProcActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
What it does during startup of your app it will query if there is someone already logged in if yes it will go to the ChoosingProcActivity. I am assuming that in your SharedPrefManager implementation you have a code to know if there is a logged in user.
In your login method uncomment the finish() method
startActivity(new Intent(getApplicationContext(), ChoosingProcActivity.class));
//finish();
After successfull login, if you press back button you will not see the MainActivity.
In your ChoosingProcActivity it will look like this:
#Override
protected void onStart() {
super.onStart();
if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
Basically, it will ask if there is currently logged in user if not, it will go back to the MainActivity.
EDIT In Logout you may use this as basis from your code,
logoutComponent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPrefManager.getInstance(getActivity()).clear();
Intent intent = new Intent(ChooseProcActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});

Issue running a simple login app

I was learning to make a simple login app from youtube, however, when I build the code, although the app runs quite fine but it never assigns the value of 3 to the attempt counter later in the code, only the layout is visible and hence login doesn't work. Can you please help me? If you need any other file, write it in comment, I'll upload it later. Thanks.
package com.shubham.splashscreenemulation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class UserLogin extends AppCompatActivity {
private static EditText username;
private static EditText password;
private static TextView attempts_left;
private static Button login_btn;
int attempt_counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
}
public void LoginButton(){
username = (EditText)findViewById(R.id.editText_user);
password = (EditText)findViewById(R.id.editText_password);
attempts_left = (TextView)findViewById(R.id.textView_attempt_count);
login_btn = (Button)findViewById(R.id.button_login);
attempts_left.setText(Integer.toString(attempt_counter));
login_btn.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev"))
{
Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen");
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts_left.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0)
{
login_btn.setEnabled(false);
}
}
}
}
);
}
}
You don't call the function LoginButton() in your Activity.
Here some tips : function name starts with lowercase letter like loginButton ( with camelCase ), classes names starts with uppercase letter like AppCompatActivity. This tip is for more readability for the code. Why do you declare Views as static in that function?
package com.shubham.splashscreenemulation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class UserLogin extends AppCompatActivity {
private static EditText username;
private static EditText password;
private static TextView attempts_left;
private static Button login_btn;
int attempt_counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
LoginButton();
}
public void LoginButton(){
username = (EditText)findViewById(R.id.editText_user);
password = (EditText)findViewById(R.id.editText_password);
attempts_left = (TextView)findViewById(R.id.textView_attempt_count);
login_btn = (Button)findViewById(R.id.button_login);
attempts_left.setText(Integer.toString(attempt_counter));
login_btn.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev"))
{
Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen");
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts_left.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0)
{
login_btn.setEnabled(false);
}
}
}
}
);
}
You can add LoginButton() after setContentView or remove the LoginButton method, move everything inside onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
username = (EditText)findViewById(R.id.editText_user);
password = (EditText)findViewById(R.id.editText_password);
attempts_left = (TextView)findViewById(R.id.textView_attempt_count);
login_btn = (Button)findViewById(R.id.button_login);
attempts_left.setText(Integer.toString(attempt_counter));
login_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev"))
{
Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen");
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show();
attempt_counter--; attempts_left.setText(Integer.toString(attempt_counter));
if(attempt_counter == 0)
{
login_btn.setEnabled(false);
}
}
}
});
}
You can also change your setText to this attempts_left.setText(attempt_counter+"");

Android application - login account

i am working with my project and i find it hard to go to my main activity which is diary after I log-in..The problem is that I can't display a message saying I successfully logged in and after that proceed to creating my diary..what should I do? here's my code..
login.class
package com.gomez.android;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class login extends Activity{
//Declare views
private EditText uname;
private EditText pword;
private Button btnlogin;
private Button btncancel;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set Activity Layout
setContentView(R.layout.login);
//Get EditText and Button References
uname = (EditText)findViewById(R.id.username);
pword = (EditText)findViewById(R.id.password);
btnlogin = (Button)findViewById(R.id.login_enter);
btncancel = (Button)findViewById(R.id.cancel);
//set Click Listener
btnlogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Check Login
final String username = uname.getText().toString();
final String password = pword.getText().toString();
try{
if(username.length()>0&& password.length()>0)
{
dbuser users = new dbuser(login.this);
users.open();
if (users.Login(username, password))
{
Toast.makeText(login.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
Intent i = new Intent(login.this, firstpage.class);
startActivity(i);
}
else{
Toast.makeText(login.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
}
users.close();
}
}catch(Exception e)
{
Toast.makeText(login.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
btncancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//close application
finish();
}
});
}
}
please help me..thanks
You can use Asynctask class
Asynctask
In the onPostExecute method, you can show your notification then start your activity.

reset button in signup page is not working in android

I tried to make a signup page in android where I use a reset button that should clear all fields in the page. Please see the code below and correct it as my code is not working.
Button btnreset = (Button) findViewById(R.id.btnreset);
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
this is an signup page and fields are first name,last name,user id,password.when user click on reset button it clear all the fields who's filled the user.I'm give a complete source code to you please check this:
package com.boyzcorn.android.fyp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class signup extends Activity{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.signup);
Button b = (Button) findViewById(R.id.btnClick2);
Button btnreset = (Button) findViewById(R.id.btnreset);
final EditText eText1 = (EditText)findViewById(R.id.firstname);
final EditText eText2 = (EditText)findViewById(R.id.lastname);
final EditText eText3 = (EditText)findViewById(R.id.userid);
final EditText eText4 = (EditText)findViewById(R.id.password);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
{
if(eText1.getText().toString().equals("") ||eText2.getText().toString().equals("") || eText3.getText().toString().equals("") ||eText4.getText().toString().equals(""))
{
Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
}
else
{
Intent i = new Intent(signup.this,login.class);
startActivity(i);
}
}
}
});
}
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
public void onClick(View arg0) {
}
}
You've only defined the function; you're not calling it. You will need to execute restartActivity(signup.this) from the OnClickListener.
Also, your intent will likely not execute, because its parent has been finished. Perhaps rearranging the lines of code might help, but a better solution would be having the parent activity start it. Try replacing the click listener with:
btnreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Activity act = signup.this;
Intent intent = new Intent(act, act.getClass());
act.startActivity(intent);
act.finish();
}
}

Categories

Resources