[Android Kotlin 기초] 2-1. Anatomy of Basic Android Project
1: Anatomy of Basic Android Project
Created: Apr 1, 2021 12:03 PM link: https://developer.android.com/codelabs/kotlin-android-training-app-anatomy?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fkotlin-fundamentals-two%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fkotlin-android-training-app-anatomy#0
Activities
AppCompatActivity
AppCompatActivity
is a subclass of Activity that supports all modern Android features while providing backward compatibility with older versions of Android. To make your app available to the largest number of devices and users possible, always useAppCompatActivity.
Views
- All UI elements in the app layout are subclasses of the
[View](http://developer.android.com/reference/android/view/View.html)
class
View attributes
android:layout_width
android:layout_height
- The
match_parent
value stretches the view to its parent’s width or height. Thewrap_content
value shrinks the view to fit the view’s contents.
- The
Strings
- String resources are contained in the
res/values/string.xml
file. - To extract strings, use
Alt+Enter
(Option+Enter
on a Mac). Select Extract string resources from the popup menu.
Using view
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.setOnClickListener { rollDice() }
android:id
findViewById()
setOnClickListener()
Question 1
Which method on an Activity
inflates the app’s layout and makes its views available as objects?
onCreate()
setClickListener()
setContentView()
show()
In onCreate()
, you specify which layout is associated with the activity, and you inflate the layout. The setContentView()
method does both those things.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
Question 2
Which view attribute do you use to set the width of a view so that it adjusts to fit the content?
android:view_width="wrap"
**android:layout_width="wrap_content"
**android:layout_height="wrap_content"
android:layout_width="match_parent"