Thursday, 25 April 2019

RxAndroid | Reactivex.Observable | How to create a Observable which can emit String

Dependencies :-
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version)    
implementation 'io.reactivex.rxjava2:rxjava:2.2.1'
MainActivity.java :-
Observable<String> observable;
Observer<String> observer;
@Override 
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    observable = Observable.just("Android Hubbbb");
    observer = new Observer<String>() {
        @Override        
           public void onSubscribe(Disposable d) {

        }

        @Override        
           public void onNext(String s) {
            tvText.setText(s);
        }

        @Override        
          public void onError(Throwable e) {

        }

        @Override        
           public void onComplete() {

        }
    };

    buttob.setOnClickListener(new View.OnClickListener() {
        @Override         
           public void onClick(View v) {
           observable.subscribe(observer);
        }
    });

Tuesday, 16 April 2019

Generic in Java | Example of Generic in android | Generic method | Generic Class in android or java

What is Generic:-
Ans:- Generic are used to check the type compatibility at the compile time. It will reduce the chances of ClassCastException at run time.

Generic Class example:-
class GenericClass<T> {

    T x;

    //setter    public void setX(T t) {
       this.x = t;
    }
    //getter    public  T getX(){
        return x;
    }
}
Generic Method:-

//Array in genericpublic static <E> void printArray(E[] array) {
    for (E element : array) {
        Log.d("supriya", element + "");
    }
}


// find max using comparable in Genericprivate static <T extends Comparable<T>> T maximum(T x, T y, T z) {
    T max = x;
    if (y.compareTo(max) > 0) {
        max = y;
    }
    if (z.compareTo(max) > 0) {
        max = z;
    }
    return max;
}


//addition in Genericprivate <T> T add(T a, T b) {
    Integer result = (Integer) a + (Integer) b;
    Log.d("Supriya Add==  ", result + "");
    return (T) result;
}

//a method can hold both generic and non generic dataprivate <T> void setUserData(GenericClass<T> genericClass, String name) {
    genericClass.setX((T) "supriya");
    Log.d("supriya", "Name ::= "+name + "\n"+"generic data :: "+genericClass.getX());

}

Calling of method are following :- 

printArray(number);
printArray(str);
int max = maximum(120, 34, 79);
Log.d("AndroidHubb" + " Maximum = ", max + "");
int value = add(12, 13);
Log.d("supriya" + " Add2:;  = ", value + "");

GenericClass genericClass = new GenericClass();
genericClass.setX(20);
Log.d("AndroidHubb", "genericClass :: " + genericClass.getX());
setUserData(genericClass,"Generic");

Thursday, 11 April 2019

sonarQube Android | How to use sonarQube in android studio | End to end integration of sonarQube in android project | sonar qube code quality check in android studio | Android studio sonarQube




1. Download SonarQube from following link v 6.6.7

https://www.sonarqube.org/downloads/

2. Now Open your terminal and go to your downloaded sonarQube folder:
for example u are on root like:-

SupriyaMac:~ SupriyaBharti$ cd Downloads/sonarqube-6.7.6/bin/macosx-universal-64/
SupriyaMac:macosx-universal-64 SupriyaBharti$ ./sonar.sh start
Starting SonarQube...
SonarQube is already running.

Images:-

3. Now time to open sonar local host in your browser , please copy paste below link in your web browser:-


4. Open your android studio project and open the project build.gradle and add following :-

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" } // added for sonarQube 
    }

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.1' // added for sonarQube
}

Images :- 

5. Now open  app build.gradle and add following :-
apply plugin: 'org.sonarqube' // added for sonar qube
// added for sonar qube
sonarqube {
    properties {
        property "sonar.projectName", "ContentResolver"
        property "sonar.projectKey", "ContentResolver"
        property "sonar.host.url", "http://localhost:9000"
        property "sonar.language", "java"
        property "sonar.sources", "src/main/java"
        property "sonar.java.sources", "src/main/java"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.login", "admin"
        property "sonar.password", "admin"
    }
}

Note :- Here in sonarQube property "sonar.sources""src/main/java" this source path is very important .
Images :- 
Note :- put sonar properties above dependencies. 


6. Now open your terminal inside android studio 
check you are at you current project root like for me my current project location will looks like following :-

SupriyaMac:ContentResolver SupriyaBharti$ 

7. Now run following command :

SupriyaMac:ContentResolver SupriyaBharti$ ./gradlew sonarqube -Dsonar.host.url=http://localhost:9000/


NOTE :  no 7 is very important if you will not run this command than u will not able to connect your project with sonarLocal host.

8. Finally your refresh sonarQube local host and it will look look following :-
Images:-

For End to End sonar Qube integration you can also watch my youtube video :

https://www.youtube.com/watch?v=AjoI7EvVgMQ&t=4s 

How to use sdk's .aar file in a android studio project.

I have created my own SDK and now i want to use the generated AAR file in a different project. for this following things need to do :-
1. Copy .aar file in your project lib folder.
2. Now open your app:gradle  and inside dependencies add following
/*add num library*/compile(name: 'addnum-debug', ext: 'aar')




3. Now open Project : build.gradle and add following :-
flatDir {
    dirs 'libs'    dirs project(':addnum').file('libs')
}


AFTER THIS YOU WILL ABLE USE SDK CLASS.

Tuesday, 9 April 2019

How to create or develop a library or sdk in android. | How to build an AAR library in android.

To make your own library you have to do following thing:
1. making library for android needs parent app to run and access the library things.For this if you have already any project where u wants to make library that also fine or u can create a new android project and than do the following thing:-




To invoke SDK  we need to add this in our app gradle :-

dependencies {
compile project(':addnum')
}

To call the SDK first Activity or to inter inside the SDK we have do like following :-
addBtn.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        // if u wants to pass any data to the SDK  sent it into bundle we can use all other method to do this        
Bundle bundle =  new Bundle();
        bundle.putString("name","Supriya");
        bundle.putString("id","123");
        Intent intent = new Intent(MainActivity.this,AddNumberActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
});
 SDK code will be like following (AddNumberActivity) :-
to get the bundle from parent app :-
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String name = bundle.getString("name");
String id = bundle.getString("id");
}

tvname.setText(name);
tvid.setText(id); 

btnAdd.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        if (ed1.getText().toString() != null && ed2.getText().toString() != null) {
            float result = Float.parseFloat(ed1.getText().toString()) + Float.parseFloat(ed2.getText().toString());
            tvResult.setText(String.valueOf(result));
        }
    }
});

Note:-
1. To generate .aar file please clean the project (Build > Clean Project ). 
2. And than Rebuild the project (Build > Rebuild Project ).
3. To see the .aar file please  choose view code as project.
4. And than click on sdk  addnum > build > outputs > aar. 
5. Here right click on .aar file and click on an option Reveal in Finder .
6. Now you can copy your .aar file to use in another project.


Monday, 8 April 2019

How to take multiple permission in android at app launch or runtime. | Multiple permission in android.


btnMulti.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        checkPermission();
    }
});



//method
private void checkPermission() {
    if (ActivityCompat.checkSelfPermission(this, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(this, permissionsRequired[1]) != PackageManager.PERMISSION_GRANTED) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("This app needs Camera and Location permissions.");
        builder.setTitle("Need Multiple Permissions");
        builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(MultiplePermissionActivity.this, permissionsRequired, 100);
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();
    }
}

Thursday, 4 April 2019

How to take single permission in android at runtime | How to show DialogBox with grant and cancel option before taking run time permission in android.

on the click on specific button we have do like following :-

actionButton.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        if(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) 
!= PackageManager.PERMISSION_GRANTED) {
            //Show Information about why you need the permission            
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Need Storage Permission");
            builder.setMessage("This app needs storage permission.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override                
public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(MainActivity.this
,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},200);
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override                
public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
                }
            });
            builder.show();

        }else {
            //just request the permission            
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},200);
        }
    }
});

@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
 @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case 200:
            Toast.makeText(this,"Access denied",Toast.LENGTH_LONG).show();
            break;
    }
}