Posts

Showing posts from January, 2022

ArrayAdapter in Android

Image
Overview ArrayAdapter is used to create a customized item for Spinner. Example of Simple ArrayAdapter public class UserAdapter extends ArrayAdapter<User> { public UserAdapter (Context context , ArrayList<User> centerList) { super (context , 0 , centerList) ; } @NonNull @Override public View getView ( int position , @Nullable View convertView , @NonNull ViewGroup parent) { return initView(position , convertView , parent) ; } @Override public View getDropDownView ( int position , @Nullable View convertView , @NonNull ViewGroup parent) { return initView(position , convertView , parent) ; } private View initView ( int position , View convertView , ViewGroup parent) { if (convertView == null ) { convertView = LayoutInflater. from (getContext()).inflate( R.layout. style_spinner_mnames , parent , false ) ; } TextView modelName = convertView.findViewBy

Message Digests in Android

Image
Overview Message Digests are one way hash functions that take arbitrary-sized data and returns fixed-length hash value. Steps to use Message Digests public static String encryptString (String data) { String salt = "iamraajkanchan" ; String output ; try { //Create Byte valiable byte [] dataByte ; data = data + salt ; dataByte = data.getBytes( "UTF-8" ) ; //Define hash algorithm MessageDigest messageDigest = MessageDigest. getInstance ( "SHA-256" ) ; //Hashed given pain-text dataByte = messageDigest.digest(dataByte) ; // return the hash as a base 64 encoded string output = Base64. encodeToString (dataByte , Base64. NO_WRAP ) ; return output ; } catch (Exception e) { e.printStackTrace() ; return null; } } Reference https://developer.android.com/reference/java/security/MessageDigest

Convert Password Characters into a placeholder character.

Image
Overview This code is used to covert password characters into an Asterisk. To do that you have to create a class and extend the class with PasswordTransformationMethod public class AsteriskMethod extends PasswordTransformationMethod { public CharSequence mPassword ; @Override public CharSequence getTransformation (CharSequence source , View view) { return new AsteriskMethod.PasswordCharSequence(source) ; } private class PasswordCharSequence implements CharSequence { public PasswordCharSequence (CharSequence source) { mPassword = source ; // Store char sequence } public char charAt ( int index) { return '*' ; // This is the important part } public int length () { return mPassword .length() ; // Return default } public CharSequence subSequence ( int start , int end) { return mPassword .subSequence(start , end) ; // Return default

Memory Leak in Android

Image
Overview You can use the library LeakCanary to check Memory Leak Steps to follow Add the required dependency in build.gradle (Module)  debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1' Hello World Reference https://square.github.io/leakcanary/getting_started/

Handler in Android

Image
Overview Handler allows you to send Message and process Runnable objects which are associated with the thread of a Message Queue.  Steps to create a Handler with Message final Handler handler = new Handler() { @Override public void handleMessage (Message msg) { super .handleMessage(msg) ; char c = s .charAt( i [ 0 ]) ; bottomText .append(String. valueOf (c)) ; //bottomText is a TextView i [ 0 ]++ ; } } ; final Timer timer = new Timer() ; TimerTask taskEverySplitSecond = new TimerTask() { @Override public void run () { handler .sendEmptyMessage( 0 ) ; if ( i [ 0 ] == length - 1 ) { timer .cancel() ; } } } ; timer.schedule(taskEverySplitSecond , 1 , 500 ) ; The above code throws a warning "This Handler class should be static" replace the code with the below code. final Handler handler = new Handler(msg -> { char c = s .charAt( i [ 0 ]) ; bottomtext .append(String. value

Shared Preferences in Android

Image
Overview Shared Preference is a way to store and receive primitive data in the form of key-value pair. Steps to follow Create an instance of Shared Preference SharedPreferences sharedPreference = context .getSharedPreferences( PREF_NAME , Context. MODE_PRIVATE ) ; SharedPreferences.Editor editor = sharedPreference.edit() ; Add the data in the form of Key/Value pair in the Editor editor.putInt( "key" , value ) ; editor.apply() ; To get the data from Shared Preference SharedPreferences sharedPreference = context .getSharedPreferences( PREF_NAME , Context. MODE_PRIVATE ) ; sharedPreference.getInt( "key" , defaultValue ) ; To clear data from Shared Preference SharedPreferences sharedPreference = context .getSharedPreferences( PREF_NAME , Context. MODE_PRIVATE ) ; SharedPreferences.Editor editor = sharedPreference.edit() ; editor.clear() ; editor.apply() ; Hello World Do's and Dont's If you are creating a Shared Preference, create a separate class for i

Dagger-Hilt in Android

Image
Overview Class without dependency injection: class Mobile { private var battery : Battery? = null private var processor : Processor? = null init { battery = Battery() processor = Processor() } } In the above line of code, when you create an object of a Mobile, then its constructor will have to create the object of Battery and Processor to initialize the properties of Mobile. Henceforth, it has to violate the principle defined by "Uncle Bob" i.e. 1st principle of Solid Principle (Single responsibility Principle).  In addition, we have to admit that whenever we are creating an object of Mobile, the class will create an object of Battery and Processor. In future, if we want to pass the object of Battery in the class then we are not allowed to do it because of scope limitation.  What is dependency injection? When an object of a class depends on the object of another classes that is dependency injection. class Mobile( var battery : Battery?