Start from template
19 min
download an android project with source code and start using back4app introduction in this guide, you will learn how to get started with an android application written in java or kotlin and connect it to back4app if you want a detailed quickstart guide or connect back4app to an existing project, go to our install parse sdk tutorial https //www back4app com/docs/android/parse android sdk goal download an android template and connect it to back4app prerequisites android studio version 4 1 or newer https //developer android com/studio/index html an app created at back4app follow the new parse app tutorial https //www back4app com/docs/get started/new parse app to learn how to create a parse app at back4app 1 download the template there are 2 android templates, one written in java and the other on kotlin kotlin example repository https //github com/templates back4app/android kotlin starter template java example repository https //github com/templates back4app/android java starter template choose the template that suits you, and proceed to download or import your project on android studio android studio 1 1 download directly from github use the following commands to download and unzip your project template macos and linux $ curl lok https //github com/templates back4app/android java starter template/archive/master zip && unzip master zip$ curl lok https //github com/templates back4app/android kotlin starter template/archive/master zip && unzip master zip windows 1 2 open the project on android studio after downloading the files, unzip them let’s open android studio in the welcoming screen of android studio, choose ‘open an existing project’ and select the project’s folder choose your downloaded and unzipped folder’s location and open it please wait until the finish of the gradle run process now you can see gradle console bottom tabs in android studio 1 3 import from github(optional path) you can import the repository link directly to android studio on android studio welcoming screen, choose ‘get from version control’ android studio will ask you for the git repository link and the desired project path you can find repository links at the start of this section you can find repository links in the start of this section after filling the url and directory fields, click on the clone button then android studio will copy and open the project for you please wait until the finish of the gradle run process now you can see gradle console bottom tabs in android studio android studio will copy and open project for you please wait until gradle run is finished you can see gradle console bottom tabs in android studio 2 get your app keys in this guide we will use following files in project \<font color="#2166ae">androidmanifest xml\</font> we will set our back4app credentials as \<font color="#2166ae">\<meta data\>\</font> and app permissions \<font color="#2166ae">app java\</font> ( \<font color="#2166ae">app kt\</font> for kotlin) we will modify our initialization code in here \<font color="#2166ae">mainactivity java\</font> ( \<font color="#2166ae">mainactivity kt\</font> for kotlin) will contain our very first code for creating a parse object \<font color="#2166ae">strings xml\</font> we will store and read back4app setup credentails from here \<font color="#2166ae">build gradle\</font> we will set our parse android sdk version in here in order to connect your app project to back4app’s server, you’ll need three primary information the server url, the application id, and the client key in an android project, strings xml is a perfect place to set this information it is where parse android sdk reads application key values to make a connection with your back4app app the server url is already on the project you’‘ll need now go to back4app, copy your app keys, and update your strings xml with those values open your strings file \<font color="#2166ae"> /app/src/main/res/values/strings xml\</font> 2\ go to your app dashboard at back4app website https //www back4app com/ 3\ find you keys on \<font color="#2166ae">app settings\</font> > \<font color="#2166ae">security \& keys\</font> 4\ return to your \<font color="#2166ae">strings xml\</font> file and paste your \<font color="#2166ae">applicationid \</font> and \<font color="#2166ae">clientkey\</font> 1 \<resources> 2 \<string name="app name">back4appexample\</string> 3 \<string name="back4app server url" translatable="false">https //parseapi back4app com/\</string> 4 5 \<! paste both keys here > 6 \<string name="back4app app id" translatable="false">paste your application id here\</string> 7 \<string name="back4app client key" translatable="false">paste your client key here\</string> 8 \</resources> 5\ open your build gradle (module\ back4appexample app) file in gradle scripts from project explorer in \<font color="#2166ae">dependencies \</font> section change the parse sdk android value with version of your choice implementation "com github parse community parse sdk android\ parse\ latest version here" after saving \<font color="#2166ae">build gradle\</font> run ‘sync now’ you can see current version of sdk in here sdk versions https //jitpack io/#parse community/parse sdk android 3 connect to back4app after setting up your app credentials, you are ready to connect with your parse server instance on back4app this is the initialization code you’re going to use you can reach initialization code in project in \<font color="#2166ae">app java\</font> ( \<font color="#2166ae">app kt\</font> for kotlin) we are using \<font color="#2166ae">app java\</font> for our initialization because we need to establish connection before app takes any other action \<font color="#2166ae">app java\</font> is the first context to be created before any other activity and service and last to be destroyed below initilization code gets app keys from \<font color="#2166ae">strings xml\</font> and try to establish a connection with our back4app server we put our code oncreate() method because we want to connect to our server first before taking any other action app java 1 public class app extends application { 2 @override 3 public void oncreate() { 4 super oncreate(); 5 parse initialize(new parse configuration builder(this) 6 applicationid(getstring(r string back4app app id)) 7 clientkey(getstring(r string back4app client key)) 8 server(getstring(r string back4app server url)) 9 build()); 10 } 11 } app kt 1 class app application() { 2 override fun oncreate() { 3 super oncreate() 4 parse initialize( 5 parse configuration builder(this) 6 applicationid(getstring(r string back4app app id)) 7 clientkey(getstring(r string back4app client key)) 8 server(getstring(r string back4app server url)) 9 build()); 10 } 11 } now it is time to add some codes for interacting with the server let’s open our mainactivity file activity files are great for interacting with user they are main purpose providing a user interface you can choose which activity to show in launch in \<font color="#2166ae">androidmanifest xml\</font> 1 \<activity android\ name=" mainactivity"> 2 \<intent filter> 3 \<action android\ name="android intent action main" /> 4 \<category android\ name="android intent category launcher" /> 5 \</intent filter> 6 \</activity> in our project mainactivity is set to open on launch in this code sample we have a parse sdk code for saving a parse object to server and showing objectid of saved parse object to user with a textview mainactivity java 1 public class mainactivity extends appcompatactivity { 2 @override 3 protected void oncreate(bundle savedinstancestate) { 4 super oncreate(savedinstancestate); 5 setcontentview(r layout activity main); 6 textview textview = findviewbyid(r id textview); 7 parseobject firstobject = new parseobject("firstclass"); 8 firstobject put("message","hey ! first message from android parse is now connected"); 9 firstobject saveinbackground(e > { 10 if (e != null){ 11 log e("mainactivity", e getlocalizedmessage()); 12 }else{ 13 log d("mainactivity","object saved "); 14 textview\ settext(string format("object saved %s", firstobject getobjectid())); 15 } 16 }); 17 } 18 } mainactivity kt 1 class mainactivity appcompatactivity() { 2 override fun oncreate(savedinstancestate bundle?) { 3 super oncreate(savedinstancestate) 4 setcontentview(r layout activity main) 5 val textview = findviewbyid\<textview>(r id textview) 6 val firstobject = parseobject("firstclass") 7 firstobject put("message","hey ! first message from android parse is now connected") 8 firstobject saveinbackground { 9 if (it != null){ 10 it localizedmessage? let { message > log e("mainactivity", message) } 11 }else{ 12 log d("mainactivity","object saved ") 13 textview\ text = string format("object saved %s", firstobject objectid) 14 } 15 } 16 } 17 } 4 test the connection build your app in a device or virtual device ( \<font color="#2166ae">shift\</font> + \<font color="#2166ae">f10\</font> ) if you don’t have any virtual device to run app you can create a new one from avd manager in android studio wait until the \<font color="#2166ae">hello word! \</font> screen appears after \<font color="#2166ae">hello word!\</font> you will see object saved message this message will include saved object’s id 2\ login at back4app website https //www back4app com/ 3\ find your app and click on \<font color="#2166ae">dashboard \</font> > \<font color="#2166ae">database \</font> > \<font color="#2166ae">browser\</font> if everything works properly, you should find a class named \<font color="#2166ae">firstclass \</font> as follows it’s done! you can see objectid in dashboard and your app’s screen is matches ! at this point, you have learned how to get started with android apps learn more by walking around our android tutorials https //www back4app com/docs/android/android project with source code download or check parse open source documentation for android sdk http //docs parseplatform org/android/guide/