Let's create a basic app that displays a message.
Android Studio → File → New Project
Why do you need a package name?
Why does the package name begin with com
?
com.companyname.applicationname
, so, it's the reversed domain name, considered unique between publishers/companies. What language?
What API version?
Instant app support?
Androidx* artifacts?
app → java → com.fitbit.summerschool → MainActivity
manifest → AndroidManifest.xml
app → res
Gradle Scripts
In the Java code of your MainActivity you will use components defined in its layout (e.g. objects for text views, buttons etc). The next images show the generated xml layout and its 'Design' panel. You can edit using both panels, it is a matter of choice.
In the above images the IDE shows a warning in both views that the string is not defined in a separate resources file. You can press option+Enter or alt+Enter and it will create that resource for you.
We edited the automatically included TextView widget and changed it into “Hello Fitbit”.
All UI components must be included inside a layout, here we use ContraintLayout. You can also nest layouts, having one inside of another, e.g. a ConstraintLayout inside a ScrollView layout.
In the MainActivity
's code, you will see that Android Studio generated the following code:
package com.fitbit.summerschool; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
onCreate()
savedInstanceState
- the Bundle object in which we can persist the state of the activity in case it is destroyed and recreated, it is not necessary to use this, in case you don't this object is null.setContentView(R.layout.main_activity)
- MUST also be called in order to set the layout defined in the xml. R is a class provided by the Android framework that maps the ids of the resources you declare in the xml files (layouts, widgets, strings etc). The system automatically generates an object for each subfolder in your app's res/ folder.