Thursday, 12 December 2019

Null Safety


!! Not-null Assertion //use it when you are sure the value is notnull
//throws nullpointerexception if the value is found to be null

?.let{} safe call with let

?. Safe call operator
Val name: String = null
By default kotlin not support nullable.
If we want to assign null we can use -> Val name: String? = null

?.let{} safe call with let
//1. returns the length if the name is not null else return null
//2. Name?.let{//It Executes the block only if the name is notnull
Println("")
}
?: Elvis
// when we have a nullable reference name, we can say "is name is not null",use it ,
//otherwise use some non nullable value
  1. Val nameLen=if(name!=null)
Name.length
Else
-1
Or
Val leng = name?.length?: -1

Wednesday, 4 December 2019

Difference between DP , SP ,DPI and Pixel


DPI- Dots per Inch
DP - Density independent pixels—>it scales as per device screen density—>used to define layouts or views dimension—>pixel unit I.e independent of density of the device screen
Px- Pixel

SP: scale independent pixel—>used only for textview size—>it scales as per device screen density and user setting preferences.

px= dp*(dpi/160)

1dp equals to how many pixel for mdpi
Here mdpi = 160 pixel
         hdpi=  240 pixel
        Xhdpi = 320 pixel
xxhdpi=480 pixel


Px = 1*160/160

Px =1dp

For hdpi=1px=1*240/160=1.5px—>scale factor

Screen size measured in diagonally- 5.0’ screen
Screen resolution 720*1280 pixels

Screen density measured in DPI(Dots per Inch)
        mdpi = 160 pixel
         hdpi=  240 pixel
        Xhdpi = 320 pixel
xxhdpi=480 pixel

Tuesday, 20 August 2019

MVP Design Pattern


The mvp pattern separates the data model from view through presenter
This arch improves the testability of apps


0-package


  • MainActivity implements SigninView{
  • Private SigninPresenter signinPresenter;

     Oncreate()
    signinPresenter= new SigninPresenter(MainActivity.this)
    signinPresenter.signIn(“raja”,”raja”);

    void showValidationError()
    Log.e("validation","validation");
    void signinsuccess()
    Log.e("success","success");
    void signingError();
    Log.e("failure","failure");


    }

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

    View-package


    1.Interface SigninView{

    void showValidationError();
    void signinsuccess();
    void signingError();

    }

    Presenter-package

    2.interface SigninPresenter{
    void signIn(String username, String password);
    }


    Model-package

    3.class SigninPresenterImpl implements  SigninPresenter{
    private SigninView signinView;

    4.SigninPresenterImpl(SigninView signinView)
    this.signinView=signinView;

    @Override
    5.void signIn(String username, String password){

    if(TextUtils.isEmpty("username") || TextUtils.isEmpty("password")){
        signinView.showValidationError()

    }else{
        if(userName.equalIqnorecase("raja") && password.equalIqnorecase("raja")){
            signinView.signinsuccess()
        } else{
            signinView.signinerror()
        }
    }

    }

    Sunday, 3 February 2019

    mvvm sample

    MVVM sample


    android{

    dataBinding {
    enabled = true
    }
    }

    usermodel:

    public class User {
    
        private String username;
        private String password;
        public String usernamehint;
        public String passwordhint;
    
    
        public User() {
        }
    
        public User(String usernamehint, String passwordhint) {
            this.usernamehint = usernamehint;
            this.passwordhint = passwordhint;
        }
    }
    
    
    
    
    
    
    
    
    viewmodel:
    
    
    public class UserModel extends BaseObservable {
    
        private String username;
        private String password;
        public String usernamehint;
        public String passwordhint;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
            notifyPropertyChanged(R.id.email);
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
            notifyPropertyChanged(R.id.pass);
        }
    
        public String getUsernamehint() {
            return usernamehint;
        }
    
        public void setUsernamehint(String usernamehint) {
            this.usernamehint = usernamehint;
        }
    
        public String getPasswordhint() {
            return passwordhint;
        }
    
        public void setPasswordhint(String passwordhint) {
            this.passwordhint = passwordhint;
        }
    
        public UserModel(User user) {
            this.usernamehint =user.usernamehint;
            this.passwordhint =user.passwordhint;
        }
    }
    
    
    
    
    
    
    interface:
    
    
    public interface UserLogin {
    
        void onClickLogin();
    }
    
    
    activity:
    
    
    private ActivityMainBinding activityMainBinding;
        @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
            UserModel um =new UserModel(new User("User Name","Password"));
            activityMainBinding.setLogin(um);
    
            activityMainBinding.setOnClickButtion(new UserLogin() {
                @Override            public void onClickLogin() {
                    Toast.makeText(getApplicationContext(),""+activityMainBinding.getLogin().getUsername(),Toast.LENGTH_LONG).show();
                }
            });
    
    
        }
    
    
    layout:
    
    
    <?xml version="1.0" encoding="utf-8"?><layout  xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         xmlns:app="http://schemas.android.com/apk/res-auto">
    
    
    <data>
        <variable            name="Login"            type="mvvm.mvvmsample.viewmodel.UserModel">
        </variable>
        <variable            name="onClickButtion"            type="mvvm.mvvmsample.commands.UserLogin">
        </variable>
    
    </data>
    
    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        android:gravity="center"        android:layout_margin="5dp"        android:padding="5dp"        tools:context=".MainActivity_">
    
        <EditText            android:id="@+id/email"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:text="@={Login.username}"            android:hint="@{Login.usernamehint}"
               />
        <EditText            android:id="@+id/pass"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:text="@={Login.password}"            android:hint="@{Login.passwordhint}"
        />
        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@color/colorAccent"            android:text="Login"            android:onClick="@{(v)-> onClickButtion.onClickLogin()}"    />
    
    </LinearLayout>
    
    
    
    
    
    
    
    

    Saturday, 2 February 2019

    Retrofit Call In Android:Step by Step Implementation

    Retrofit Call In Android:

    url: https://jsonplaceholder.typicode.com/posts


    1.manifest
    <uses-permission android:name="android.permission.INTERNET"/>
    
    
    2.gradle
    
    
    dependencies {
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'implementation 'com.squareup.retrofit2:converter-gson:2.5.0'}
    
    
    3.model
    
    
    public class Post {
    
        private int userId;
        private int id;
        private String title;
        @SerializedName("body")
        private String textBody;
    
        public int getUserId() {
            return userId;
        }
    
        public int getId() {
            return id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getTextBody() {
            return textBody;
        }
    }
    
    
    4.interface for 
    import java.util.List;
    
    import retrofit2.Call;
    import retrofit2.http.GET;
    
    public interface JsonPlaceHolderApi {
        @GET("posts")
    Call<List<Post>> getPosts();
    }
    
    
    5. mServiceCall()---onCreate()
    textview = (TextView) findViewById(R.id.text_result);
    
    
    private void mServiceCall() {
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://jsonplaceholder.typicode.com/")
                .addConverterFactory(GsonConverterFactory.create()).build();
    
        JsonPlaceHolderApi jsonPlaceHolderApi= retrofit.create(JsonPlaceHolderApi.class);
        Call<List<Post>> call=jsonPlaceHolderApi.getPosts();
        call.enqueue(new Callback<List<Post>>() {
            @Override        public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                if(!response.isSuccessful()){
                    textview.setText("code:"+response.code());
                }
                List<Post> posts=response.body();
                for(Post post:posts){
                    String content ="";
                    content+="ID:"+post.getId()+"\n";
                    content+="Text Body:"+post.getTextBody();
                    textview.append(content);
    
    
                }
            }
    
            @Override        public void onFailure(Call<List<Post>> call, Throwable t) {
    
            }
        });
    }