How to add text to a layout in Android?

To add text to a layout in Android, you can use the TextView widget. Here is an example:

  1. Open the XML layout file in res/layout (for example, activity_main.xml).
  2. Add a TextView widget to the layout file as shown below:
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, World!"
    android:textSize="18sp"
    android:textColor="#000000"
    android:layout_marginTop="16dp"/>
  1. In the Activity or Fragment, locate the corresponding TextView widget and set its text content, for example:
TextView textView = findViewById(R.id.textView);
textView.setText("Hello, Android!");

By adjusting the properties of the TextView, you can add and display text in an Android layout, allowing further customization of text style, color, and size.

Leave a Reply 0

Your email address will not be published. Required fields are marked *