Wednesday, September 27, 2017

loginRegistration retrofit and MySql Slim Framwork

 

import com.google.gson.annotations.Expose;

/** * Created by Amirul on 26-Sep-17. */
public class LoginModel  {

    @Expose    private String status;
    @Expose    private String login;

    /**     * @return The status     */    public String getStatus() {
        return status;
    }

    /**     * @param status The status     */    public void setStatus(String status) {
        this.status = status;
    }

    /**     * @return The login     */    public String getLogin() {
        return login;
    }

    /**     * @param login The login     */    public void setLogin(String login) {
        this.login = login;
    }
}



package com.edupointbd.amirul.registerlogin.rest_Interface;

import com.edupointbd.amirul.registerlogin.model.LoginModel;

import retrofit.Callback;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.POST;

/** * Created by Amirul on 26-Sep-17. */
public interface RestInterface {
    //chnage your IP here if you working on local    // String url = "http://192.168.43.175/register-login/v1";    //For Hosting give the complete path before index.php    String url = "http://your domain name/register-login/v1";

    @FormUrlEncoded    @POST("/login")
    void Login(@Field("email") String email,
               @Field("pass") String pass, Callback<LoginModel> cb);

    @FormUrlEncoded    @POST("/signup")
    void SignUp(@Field("name") String name, @Field("email") String email,
                @Field("pass") String pass, Callback<LoginModel> pm);

}



---------------------------------------
 

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.edupointbd.amirul.registerlogin.model.LoginModel;
import com.edupointbd.amirul.registerlogin.rest_Interface.RestInterface;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class LoginActivity extends ActionBarActivity {

    EditText email,pass;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        email=(EditText)findViewById(R.id.input_email);
        pass=(EditText)findViewById(R.id.input_password);


        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
        getSupportActionBar().setTitle(Html.fromHtml("<font color='#e12929'>Sign In</font>"));


    }




    // When Button Login clicked    public void Signin(View v){

        //Calling method of field validation        if(CheckFieldValidation()) {

            //progressBar.setVisibility(View.VISIBLE);            setContentView(R.layout.progressbar_layout);
            //making object of RestAdapter            RestAdapter adapter = new RestAdapter.Builder().setEndpoint(RestInterface.url).build();

            //Creating Rest Services            final RestInterface restInterface = adapter.create(RestInterface.class);

            //Calling method to get check login            restInterface.Login(email.getText().toString(), pass.getText().toString(), new Callback<LoginModel>() {
                @Override                public void success(LoginModel model, Response response) {

                    finish();
                    startActivity(getIntent());

                    email.setText("");
                    pass.setText("");


                    if (model.getStatus().equals("1")) {  //login Success
                        Toast.makeText(LoginActivity.this, "Login In SuccessFully", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(LoginActivity.this,AfterLoginActivity.class);
                        startActivity(i);

                    } else if (model.getStatus().equals("0"))  // login failure                    {
                        Toast.makeText(LoginActivity.this, "Invalid UserName/Pass ", Toast.LENGTH_SHORT).show();
                    }


                }

                @Override                public void failure(RetrofitError error) {
                    finish();
                    startActivity(getIntent());
                    String merror = error.getMessage();
                    Toast.makeText(LoginActivity.this, merror, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    //When Button Sign up clicked    public void SignUp(View v){

        Intent i= new Intent(LoginActivity.this,SignupActivity.class);
        startActivity(i);


    }

    //checking field are empty    private boolean CheckFieldValidation(){

        boolean valid=true;
        if(email.getText().toString().equals("")){
            email.setError("Can't be Empty");
            valid=false;
        }else if(pass.getText().toString().equals("")){
            pass.setError("Can't be Empty");
            valid=false;
        }

        return valid;

    }

}




xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#E43F3F"    android:fitsSystemWindows="true"    android:fillViewport="false">

    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingTop="56dp"        android:paddingLeft="24dp"        android:paddingRight="24dp">

        <ImageView android:src="@mipmap/ic_launcher"            android:layout_width="wrap_content"            android:layout_height="100dp"            android:layout_marginBottom="24dp"            android:layout_gravity="center_horizontal" />

        <!-- Email Label -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_email"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="textEmailAddress"                android:singleLine="true"                android:textColor="#ffffff"                android:textColorHint="#ffffff"                android:hint="Email" />
        </LinearLayout>

        <!-- Password Label -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_password"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="textPassword"                android:singleLine="true"                android:textColor="#ffffff"                android:textColorHint="#ffffff"                android:hint="Password"/>
        </LinearLayout>



        <android.support.v7.widget.AppCompatButton            android:id="@+id/btn_login"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:background="#C62828"            android:textColor="#ffffff"            android:textStyle="bold"            android:onClick="Signin"            android:layout_marginTop="24dp"            android:layout_marginBottom="24dp"            android:padding="12dp"            android:text="Login"/>

        <android.support.v7.widget.AppCompatButton            android:id="@+id/btn_signup"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textColor="#C62828"            android:background="#ffffff"            android:textStyle="bold"            android:onClick="SignUp"            android:layout_marginBottom="24dp"            android:padding="12dp"            android:text="Not a member? Click here"/>

    </LinearLayout>
</ScrollView>


===========================================================

package com.edupointbd.amirul.registerlogin;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.edupointbd.amirul.registerlogin.model.LoginModel;
import com.edupointbd.amirul.registerlogin.rest_Interface.RestInterface;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class SignupActivity extends AppCompatActivity {

    EditText email,pass,name;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        email=(EditText)findViewById(R.id.input_email);
        pass=(EditText)findViewById(R.id.input_password);
        name=(EditText)findViewById(R.id.input_name);


        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
        getSupportActionBar().setTitle(Html.fromHtml("<font color='#e12929'>Sign Up</font>"));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }


    public void Signup(View v){


        //calling field validation method        if(CheckFieldValidation()) {

            setContentView(R.layout.progressbar_layout);
            //making object of RestAdapter            RestAdapter adapter = new RestAdapter.Builder().setEndpoint(RestInterface.url).build();

            //Creating Rest Services            final RestInterface restInterface = adapter.create(RestInterface.class);

            //Calling method to signup            restInterface.SignUp(name.getText().toString(), email.getText().toString(),
                    pass.getText().toString(), new Callback<LoginModel>() {
                        @Override                        public void success(LoginModel model, Response response) {

                            finish();
                            startActivity(getIntent());

                            email.setText("");
                            pass.setText("");
                            name.setText("");


                            if (model.getStatus().equals("1")) {  //Signup Success
                                Toast.makeText(SignupActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
                                finish();
                                Intent i = new Intent(SignupActivity.this, LoginActivity.class);
                                startActivity(i);

                            } else if (model.getStatus().equals("0"))  // Signup failure                            {
                                Toast.makeText(SignupActivity.this, "Email Already Registered", Toast.LENGTH_SHORT).show();
                            }


                        }

                        @Override                        public void failure(RetrofitError error) {
                            finish();
                            startActivity(getIntent());
                            String merror = error.getMessage();
                            Toast.makeText(SignupActivity.this, merror, Toast.LENGTH_LONG).show();
                        }
                    });
        }

    }

    //checking field are empty    private boolean CheckFieldValidation(){

        boolean valid=true;
        if(name.getText().toString().equals("")){
            name.setError("Can't be Empty");
            valid=false;
        }else if(email.getText().toString().equals("")){
            email.setError("Can't be Empty");
            valid=false;
        }else if(pass.getText().toString().equals("")){
            pass.setError("Can't be Empty");
            valid=false;
        }

        return valid;

    }


    @Override    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(menuItem);
    }



}

xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#E43F3F"    android:fitsSystemWindows="true">

    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingTop="56dp"        android:paddingLeft="24dp"        android:paddingRight="24dp">

        <ImageView android:src="@drawable/ic_assignment_black_24px"            android:layout_width="100dp"            android:layout_height="100dp"            android:layout_marginBottom="24dp"            android:layout_gravity="center_horizontal" />


        <!-- Name Label -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_name"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="text"                android:singleLine="true"                android:textColor="#ffffff"                android:textColorHint="#ffffff"                android:hint="Name" />
        </LinearLayout>

        <!-- Email Label -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_email"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="textEmailAddress"                android:singleLine="true"                android:textColor="#ffffff"                android:textColorHint="#ffffff"                android:hint="Email" />
        </LinearLayout>

        <!-- Password Label -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_password"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="textPassword"                android:singleLine="true"                android:textColor="#ffffff"                android:textColorHint="#ffffff"                android:hint="Password"/>
        </LinearLayout>



        <android.support.v7.widget.AppCompatButton            android:id="@+id/btn_signup"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:background="#C62828"            android:textColor="#ffffff"            android:textStyle="bold"            android:onClick="Signup"            android:layout_marginTop="24dp"            android:layout_marginBottom="24dp"            android:padding="12dp"            android:text="Signup"/>

    </LinearLayout>
</ScrollView>

=========================================================

package com.edupointbd.amirul.registerlogin;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;

public class AfterLoginActivity extends ActionBarActivity {


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_login);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
        getSupportActionBar().setTitle(Html.fromHtml("<font color='#e12929'>Home</font>"));


    }

}


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:gravity="center"    android:background="#E43F3F"    android:layout_height="match_parent">

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Welcome"        android:textSize="50sp"        android:textColor="#ffffff"        android:id="@+id/textView"        android:layout_gravity="center" />
</LinearLayout>



<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.edupointbd.amirul.registerlogin">

    <uses-permission android:name="android.permission.INTERNET" />

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" />
        <activity android:name=".SignupActivity" />
        <activity android:name=".AfterLoginActivity"></activity>
    </application>

</manifest>




dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'    })
    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.squareup.retrofit:retrofit:1.9.0'    compile 'com.squareup.okhttp:okhttp:2.4.0'    testCompile 'junit:junit:4.12'}