\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{scottstoll2017 (ScottHOC)} \pdfinfo{ /Title (udacity-google-developing-android-apps.pdf) /Creator (Cheatography) /Author (scottstoll2017 (ScottHOC)) /Subject (Udacity / Google Developing Android Apps Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{1834A3} \definecolor{LightBackground}{HTML}{F0F2F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Udacity / Google Developing Android Apps Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{scottstoll2017 (ScottHOC)} via \textcolor{DarkBackground}{\uline{cheatography.com/45425/cs/14625/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}scottstoll2017 (ScottHOC) \\ \uline{cheatography.com/scotthoc} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 14th February, 2018.\\ Updated 20th February, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Shared Preferences}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Reading From Shared Preferences:} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`get{\bf{Default}}SharedPreferences(context)`\{\{nl\}\}Gets a SharedPreferences instance that points to the {\bf{default}} file that is used by the preference framework in the given context.\{\{nl\}\}\{\{nl\}\}getSharedPreferences(context)\{\{nl\}\}Gets a specific SharedPreferences instance {\bf{by name}} in case you have more than one preference in the same context.\{\{nl\}\}\{\{nl\}\}{\emph{Note that there is no type checking, so if you ask shared preferences for a type that is different from what is actually stored in that key, the app will crash.}}} \tn % Row Count 12 (+ 11) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Implementing OnSharedPreferenceChangeListener}}} \tn % Row Count 13 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`public class MyClass extends ExtendedClass implements\{\{nl\}\}~~SharedPreferences.OnSharedPreferenceChangeListener \{ ...`} \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Error Levels}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Error (Remains in release versions)} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Warning (Remains in release versions)} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Info (Remains in release versions)} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Debug (Not in release versions)} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Verbose (Not in release versions)} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Permissions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Add Permissions to AndroidManifest.xml \newline \newline \textless{}uses-permission \seqsplit{android:name="android.permission}.XXXXXXXXX"\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lifecycle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{8.4cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/scotthoc_1518445820_AndroidLifecycle.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{A Simple Way to Launch URL in Browser}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`// In mainifest.xml -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}--`\{\{nl\}\}`\textless{}uses-permission \seqsplit{android:name="android.permission}.INTERNET"/\textgreater{}`\{\{nl\}\}~} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`// Imports -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-``\{\{nl\}\}import android.app.Activity;`\{\{nl\}\}`import android.content.Intent;`\{\{nl\}\}`import android.net.Uri;`\{\{nl\}\}`import android.os.Bundle;`\{\{nl\}\}`import android.view.Window;`\{\{nl\}\}~} \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{*}}* The block below will not allow more than 500 characters and the formatting tags caused it to go over. If you cut and paste this code, use auto-format (Alt-L in intelliJ or AS) to format it properly. If you use another IDE, consult your setup or help file to find the auto-format hotkey.\{\{nl\}\}~} \tn % Row Count 15 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`// The code -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-` \newline \{\{nl\}\} `public class MainActivity extends Activity \{` \newline \newline `@Override` \newline `protected void onCreate(Bundle savedInstanceState) \{` \newline `requestWindowFeature(Window.FEATURE\_NO\_TITLE);` \newline `super.onCreate(savedInstanceState);` \newline `setContentView(R.layout.activity\_main);` \newline `Intent browserIntent = new \seqsplit{Intent(Intent.ACTION\_VIEW},` \newline `Uri.parse("http://www.google.com"));` \newline `startActivity(browserIntent);` \newline `\}` \newline \newline `\}`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Loaders}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To create a loader:} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1) Auto-import the Loader class as you type, Then, have the IDE fill in the Loader Callbacks for you:\{\{nl\}\}\{\{nl\}\}LoaderManager.LoaderCallbacks\textless{}String\textgreater{}\{\{\{nl\}\}~~~~~onCreateLoader()\{\{nl\}\}~~~~~onLoadFinished()\{\{nl\}\}~~~~~onLoaderReset()\{\{nl\}\}\}} \tn % Row Count 8 (+ 7) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{2) Create an integer constant for a loader ID:\{\{nl\}\}\{\{nl\}\}private static final int myLoaderName = 20;} \tn % Row Count 11 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{3) Initialize the loader with the Loader Manager} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Preferences}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The SharedPreference class saves key-value pairs of preferences.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The PreferenceFragment subclass was specifically designed to display preferences and has replaced the now depricated Preference Activity.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Preference Fragements populate themselves with data that's defined in an XML document. The XML is used to generate UI widgets in the fragment.} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{When the user updates preferences in the widget, these changes are automatically updated in the SharedPreferences file.} \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Recycler View}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Layout Manager \textgreater{} {\bf{RECYCLER VIEW}} \textless{} View Holder \textless{} Adapter \textless{} Data} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{"Whereas the View Holder determines how an individual entry is displayed, the Layout Manager determines how the entire collection of entries is displayed."}}} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{"Layout Manager is a key part of the way recycling works in Recycler View since it determines when to recycle views that are no longer visible to the user."}}} \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{RecyclerView Adapter:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ {\emph{An adaper is called by Recycler View to:}} }}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Create new items in the form of ViewHolders} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Populate (or bind) items with data} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Return information abou the data (IE \# of items)} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ {\emph{The Adapter requires 3 functions to be overridden:}} }}} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{OnCreateViewHolder()}} : Called when RecyclerView instantiates a new ViewHolder instance (inflates from XML or generates it in code)} \tn % Row Count 10 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{OnBindViewHolder()}} : Called when RecyclerView wants to populate ViewHolder with data} \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{GetItemCount()}} : Returns the number of items in teh data source. (This might be called a number of times during the layout process so it needs to be a fast, efficient process.)} \tn % Row Count 16 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{{\bf{Recycler View Layout Manager}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{"Whereas the View Holder determines how an individual entry is displayed, the Layout Manager determines how the entire collection of entries is displayed."}}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{"Layout Manager is a key part of the way recycling works in Recycler View since it determines when to recycle views that are no longer visible to the user."}}} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{There are 3 implementations of Layout Manager:}}\{\{nl\}\}LinearLayoutManager\{\{nl\}\}GridLayoutManager\{\{nl\}\}StaggeredGridLayoutManger} \tn % Row Count 11 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{ {\emph{LinearLayoutManager}} }} allows for vertical or horizontal scrolling. Vertical is the default.} \tn % Row Count 13 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ {\emph{GridLayoutManager}} }} is a subclass of LinearLayoutManager that is laid out in a grid and can scroll vertically or horizontally.} \tn % Row Count 16 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{ {\emph{StaggeredGridLayoutManager}} }} displays a offset grid of items. Commonly used for situations where the content is of varying dimentions.} \tn % Row Count 19 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{It is also possible to directly extend from Layout Manager and create your own.} \tn % Row Count 21 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Persistence}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{There are 5 different ways to persist data:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1) "Saved Instance State" uses key-value pairs (a map) to save the state of one of your views. It's usually used to save state when the view must be quickly destroyed and restarted such as when the phone is rotated or if the activity has to be destroyed because of memory constraints but will need to be recreated at some point, when it returns to the foreground. If the user quits the app or restarts the phone, this data is lost.} \tn % Row Count 10 (+ 9) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{If you need to have data persist beyond app closures and phone restarts, it needs to be saved to a file system.} \tn % Row Count 13 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{2) The SharedPreferences class saves simple key-value pairs (a map) to a file. Keys are always strings. values are primitives (as opposed to objects). This can be used for things like saving a String for the user name between sessions or the URL (as a String) of the last web page someone was on and returning to it when restarting the app.} \tn % Row Count 20 (+ 7) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{3) For more complex data (IE: an object) we use a database, and Android uses SQL lite. Android also has various framework components (such as Content Providers) that allow you to manage and share data in databases.} \tn % Row Count 25 (+ 5) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4) For larger items (IE: audio, video, image or e-book files) you can save to internal or external storage.} \tn % Row Count 28 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5) We can also save larger files to a Cloud or Firebase, rather than taking up space in the device's storage.} \tn % Row Count 31 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}