- Open the XML layout file where you want to add the TextView widget.
- Add the following code where you want to add the TextView widget:
XML
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
In your Java class file, you can access the TextView widget using the findViewById() method and set its properties programmatically. For example:
TextView textView = findViewById(R.id.textView);
textView.setText("Hello World!");
Kotlin
Open the XML layout file where you want to add the TextView widget.
Add the following code where you want to add the TextView widget:
xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
In your Kotlin class file, you can access the TextView widget using the findViewById() method or using Kotlin Android Extensions, which allows you to access the TextView widget directly. For example:
Kotlin
val textView: TextView = findViewById(R.id.textView)
textView.text = "Hello World!"
Or, using Kotlin Android Extensions:
import kotlinx.android.synthetic.main.activity_main.*
textView.text = "Hello World!"
Note that for Kotlin Android Extensions to work, you need to enable it in your project by adding the following code to your app's build.gradle file:
gradle
apply plugin: 'kotlin-android-extensions'
கருத்துரையிடுக