1. Definition
Basic Views are the smallest UI components in Android that users can see or interact with.
Examples:
TextView → display text
EditText → input text
Button → clickable action
ImageView → display images
Simple meaning:
Views = Visible UI elements on the screen
2. Purpose
Basic Views are used to:
Show text information
Take user input
Display images
Perform actions when clicked
Example in a Login Screen:
Username → EditText
Password → EditText
Login → Button
3. Basic XML Syntax
Example Layout file:
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Android"/>
4. Using Views in Kotlin Code
In Kotlin (inside MainActivity.kt):
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.textTitle)
textView.text = "Welcome to Kotlin"
}
}
Explanation:
findViewById()→ connects Kotlin code with XML viewtextView.text→ changes text
5. Common Properties
These properties are used in XML.
1. layout_width
match_parent
wrap_content
Example:
android:layout_width="match_parent"
2. layout_height
android:layout_height="wrap_content"
3. text
android:text="Hello"
4. textSize
android:textSize="20sp"
5. background
android:background="#FF0000"
6. padding
android:padding="10dp"
6. Important Methods (Kotlin)
setText()
textView.text = "Hello User"
setOnClickListener()
For button click.
val button = findViewById<Button>(R.id.btnSubmit)
button.setOnClickListener {
println("Button Clicked")
}
setVisibility()
textView.visibility = View.GONE
Options:
View.VISIBLE
View.INVISIBLE
View.GONE
setEnabled()
button.isEnabled = false
7. Real-Time Examples
Example 1: Login Screen
XML Layout
<EditText
android:id="@+id/etUsername"
android:hint="Username"/>
<Button
android:id="@+id/btnLogin"
android:text="Login"/>
Kotlin Code
val username = findViewById<EditText>(R.id.etUsername)
val button = findViewById<Button>(R.id.btnLogin)
button.setOnClickListener {
val name = username.text.toString()
println("Hello $name")
}
Example 2: Change Text on Button Click
XML
<TextView
android:id="@+id/txtMessage"
android:text="Welcome"/>
<Button
android:id="@+id/btnChange"
android:text="Change Text"/>
Kotlin
val text = findViewById<TextView>(R.id.txtMessage)
val button = findViewById<Button>(R.id.btnChange)
button.setOnClickListener {
text.text = "Text Changed!"
}
Example 3: Display Image
XML
<ImageView
android:id="@+id/imgLogo"
android:src="@drawable/logo"
android:layout_width="100dp"
android:layout_height="100dp"/>
Kotlin
val image = findViewById<ImageView>(R.id.imgLogo)
image.alpha = 0.8f
8. Advantages
✔ Easy to use
✔ Makes UI interactive
✔ Works with Kotlin easily
✔ Essential for every Android app
9. One-Line Summary
Basic Views are UI components that display information or allow users to interact with an Android app.