Safcode Institute by Sir Safder





  • Login Form in Android

    package com.example.newdbcon;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    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 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 java.util.HashMap;
    import java.util.Map;
    
    public class MainActivity2 extends AppCompatActivity {
    
        EditText jemail, jpass;
        Button btn, btn2;
        String semail, spassword;
        String url = "https://safcode.000webhostapp.com/login.php";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            jemail = (EditText) findViewById(R.id.email);
            jpass = (EditText) findViewById(R.id.password);
            btn = (Button) findViewById(R.id.button);
            btn2 = (Button) findViewById(R.id.button2);
    
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity2.this, MainActivity.class);
                    startActivity(i);
                }
            });
    
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    semail = jemail.getText().toString().trim();
                    spassword = jpass.getText().toString().trim();
    
                    StringRequest rq = new StringRequest(Request.Method.POST, url, new Response.Listener() {
                        @Override
                        public void onResponse(String response) {
                            if(response.equals("Login successfully"))
                            {
                                Intent i = new Intent(MainActivity2.this, MainActivity.class);
                                startActivity(i);
                            }
                            else {
                                Toast.makeText(MainActivity2.this, response, Toast.LENGTH_SHORT).show();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity2.this, error.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }){
                        @Override
                        protected Map getParams() throws AuthFailureError {
                            //return super.getParams();
                            Map para = new HashMap();
                            para.put("email", semail);
                            para.put("password", spassword);
                            return para;
                        }
                    };
    
                    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity2.this);
                    requestQueue.add(rq);
    
    
                }
            });
    
        }
    }



  • You Can Also Watch Our Tutorial