Native Mobile App Development With Java

Android TextView java

15-Sep-2024

Tutorial for displaying text in Android Studio. How to use TextView.


Here is the tutorial for Kotlin


Android TextView Example : Text View Use for Show Text On Android Screen Like Long Text, Short Text, You can set your text on XML layout File also you can set your text programmatically for display in Android TextView Java or Android TextView kotlin..
Here's a basic overview of using TextView:


There Are Some Common Attributes for TextView :


id
This is unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
drawableLeft
Set Icon in TextView's Left
drawableRightSet Icon in TextView's Right
drawableTopSet Icon in TextView's Top
drawableBotomSet Icon in TextView's Botom
textStyle
bold, italic, normal -> Text Style Change
background
Set background Color


Step 1:

File open res/layout/activity_main.xml file −


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<!--Text Center-->
<TextView
android:id="@+id/idTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:gravity="center"
/>

<!--Text textStyle-->
<TextView
android:id="@+id/idTextSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:textSize="30dp"
android:gravity="center"
/>

<!--Text Style-->
<TextView
android:id="@+id/idTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:textSize="30dp"
android:textStyle="italic|bold"
android:gravity="center"
/>
<!--Text Color-->
<TextView
android:id="@+id/idTextColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:textSize="30dp"
android:textStyle="italic|bold"
android:textColor="#F40303"
android:gravity="center"
/>
<!--Text Margin Padding-->
<TextView
android:id="@+id/idTextChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="20dp"
android:textSize="30dp"
android:text="Change Text"
android:gravity="center"
/>
</LinearLayout>

</RelativeLayout>


Step 2:

File open com.microappvalley.newtestproject/MainActivity.java file −



package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView idTextChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idTextChange=(TextView) findViewById(R.id.idTextChange);

idTextChange.setText("Micro Tutorial Text Change");

}
}


Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:
















Comments