Posts

Showing posts from June, 2021

Secure Android SharedPreferences

Image
Did you know the data stored in local storage or shared preferences can be hacked? What if the data stored is very confidential - can be user details, api keys or some other important data which you cannot afford losing. Securing app’s data has become very important as they have become easy to exploit targets. Android Jetpack provides a Security library which can help secure the data. Here are two simple classes which will help you secure your data stored in files and shared preferences. These classes are part of the Security Library of Android Jetpack. EncryptedFile  - To secure app's local storage files. EncryptedSharedPreferences  - To secure app's shared preferences. In this blog we are going to talk about  EncryptedSharedPreferences  and how it is different from existing  SharedPreferences  class . What is  SharedPreferences ? A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and w...

Steps to start and grow as an Android Engineer

Image
  Learn supported programming language for Android development Having knowledge about the supported programming is a must. This helps you to easily get started with the development. Pick either Java or Kotlin to start with. Having a practical knowledge about OOPs principles is an added advantage. Start with the basics For every topic you learn, make sure you have an understanding of 3 most important things: Concept Why does it exist? Why was it introduced? Why does it still exist as part of the framework? What problems does it solve? Design What design to consider while implementing it? Understanding best practices for the component Implementation Code required to implement the component Knowing the internal working of the component is an added advantage. Check  https://techshift-institute.blogspot.com blog to understand the basics. Pick the right resources to learn from There are infinite resources available about Android development. But because it's an evolving technology s...

ConstraintLayout - An Introduction

Image
                                                                                 ConstraintLayout is an Android ViewGroup which allows you to position and size widgets in flexible way. It helps in building complex UI layouts with flat-hierarchy. Known for the highest level of resource optimization. ConstraintLayout has only one direct class which is  MotionLayout . What is Constraint?  Defined in English as “a limitation or restriction”. Constraints defines a relation between 2 widget which will define how those widgets will be positioned in the layout. A few things to note: Every view should have at least one constraint in its X and Y axes. If we don’t give the constraint the view will be move to its default position along that specific axis. It can ...

TextView - An Android View

Image
TextView is a view class which extends View.java class. This view is part of the Android framework which provides out of box functionality to display the text to the user. Generally used to show informational data to the user i.e they are not editable by the user. Known direct subclasses Button, CheckedTextView, Chronometer, DigitalClock, EditText, TextClock There are many attributes available to use in TextView, a few of them defined specifically for TextView and remaining inherited from the View class. So it becomes very difficult to remember all of them and so the simplistic way to make sure they can be used when you need them is to assume that your requirement can be satisfied with one of these attributes. We are going discuss a few commonly used attributes: android:textAppearance Set using a style or theme attribute. Specifies text color, typeface, size and style of the component. Syntax: “@[+][package:]type/name” —> android:textAppearance = “@style/TextViewStyle” “?[package:]t...

LinearLayout - An Android ViewGroup

Image
LinearLayout is an android view group which extends ViewGroup.java class. This layout is part of the Android framework which provides out of box functionality to position views in particular order. Provides a way to stack all the child views in either vertically or horizontal order i.e it will always have 1 view each row. Known direct subclasses ActionMenuView, NumberPicker, RadioGroup, SearchView, TabWidget, TableLayout, TableRow, ZoomControls List of attributes available with LinearLayout: View attributes: android:orientation Can be one of the predefined keywords (vertical , horizontal) Specifies how children will be laid out - either vertically stacked or horizontally stacked. It is a compulsory attribute. E.g android:orientation=“vertical” android:weightSum Can be integer or float values. Defines maximum weighted sum. It defines the maximum sum of all the weights given to the child views. E.g: android:weightSum=“0.5” android:gravity Can be one of the predefined keywords. Possible m...

Android Layouts

Image
In Android, layouts define structure for the user interface shown on the window of the device. The structure in the layouts are built using Views and ViewGroups. These are the building block of any UI elements shown on the screen. View : Refers to the element which the user can touch and interact with. ViewGroup : Refers to an invisible component which contains Views, or other ViewGroups. Below are some predefined Views and ViewGroups available in Android Library: Views: Button, TextView (all these extend View class) ViewGroups: LinearLayout, ConstraintLayout, RelativeLayout and many others (all these extend ViewGroup class) What should you look for when defining layouts? Layouts are a very important part of Android applications. The devices we use do not have unlimited memory, battery or similar other resources. Everything is limited. So designing layouts in an optimized way helps in making efficient use of available these resources. If implemented poorly your layout can lead to a mem...

Fragment Lifecycle

Image
Just like Activities, Fragments also manages its own lifecycle. Though these lifecycle callbacks are a bit different than activity’s callbacks they exist for the similar reason as that of activity’s callback. Fragment manages two different lifecycles. One for the fragment instance and other for the view shown in the fragment. Having different lifecycle for fragment’s view is useful in the situations where the work should be performed only when view exists. Points to note: Fragment lifecycle call-backs are similar to Activity’s lifecycle. More important they are dependent on Activity’ state. Fragment manages 2 different lifecycles - Instance and View. Fragment callbacks are: onAttach, onCreate, onCreateView, onViewCreated, onViewStateRestored, onStart, onResume, onPause, onStop, onDestroyView, onDestroy, onDetach Fragment state is saved before onStop for API level less than 28 while in API level 28 and above its saved after onStop method. i.e In API level 28 or above devices its safe to...

Fragment Communication

Image
To make sure fragment is available for reuse, fragments should be written self contained way i.e it should handled its own layout and behaviour. But for this to happen fragment would need a channel of communicate to share user events, or share information with other components. Based on the use case there are 2 recommended ways: Using ViewModel and LiveData Using Fragment Result API We are going learn about #2 in this article. We will revisit #1 when we learn about ViewModel and Live Data (part of Architectural Components). Using Fragment Result Majorly used to send one-time value between two fragments, or between a fragment or with host activity. Internals: FragmentManager manages the sending and receiving the fragment result. It implements FragmentResultOwner which makes FragmentManager as a central store for fragment results. Communication between 2 fragments with same host activity Code for result listener: Code for result setter: Communication between parent and child fragment: Co...