Program 7: Create an UI with all views.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- TextView -->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your name:"
android:textSize="18sp" />
<!-- EditText -->
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name here"
android:inputType="textPersonName" />
<!-- AutoCompleteTextView -->
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Choose your country" />
<!-- Button -->
<Button
android:id="@+id/buttonGreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say Hello" />
<!-- CheckBox -->
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the terms" />
<!-- RadioGroup with RadioButtons -->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<!-- Switch -->
<Switch
android:id="@+id/switchToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable notifications" />
<!-- ProgressBar (indeterminate) -->
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:indeterminate="true" />
<!-- ImageView -->
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:contentDescription="App Logo"
android:layout_marginTop="16dp" />
</LinearLayout>
</ScrollView>
[Link]:
package [Link]
import [Link]
import [Link].*
import [Link]
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val nameEdit = findViewById<EditText>([Link])
val greetButton = findViewById<Button>([Link])
val textView = findViewById<TextView>([Link])
val autoComplete =
findViewById<AutoCompleteTextView>([Link])
// Set up AutoCompleteTextView suggestions
val countries = arrayOf("India", "USA", "Canada", "Australia",
"Germany")
val adapter = ArrayAdapter(this,
[Link].simple_dropdown_item_1line, countries)
[Link](adapter)
[Link] {
val name = [Link]()
[Link] = "Hello, $name!"
}
}
}
[Link]:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="[Link]"
parent="[Link]" />
</resources>