Table of Contents
TextView In Android – Introduction
TextView In Android – TextView is used to display the text that cannot be editable by the user. TextView is used to display the text on the screen, activity, fragment, etc. TextView is a widget in Android, users can edit or modify (text, color, orientation, size, height, width, etc) accordingly.
Create activity_textview_layout File in XML
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
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”
android:orientation=”vertical”
android:gravity=”center”
tools:context=”.textviewLayout”>
<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”This is our First Text View”
android:textSize=”15sp” />
<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”This is our Second Text View”
android:textColor=”@color/purple_700″
android:textSize=”20sp”
android:layout_marginTop=”10dp”
android:textStyle=”bold”/>
<TextView
android:id=”@+id/textView3″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”10dp”
android:background=”@color/design_default_color_primary”
android:gravity=”center”
android:text=”This is our Third Text View”
android:textColor=”@color/purple_200″
android:textSize=”25sp”
android:textStyle=”bold” />
</LinearLayout>
Create textviewLayout File in Java
package com.eiheducation.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class textviewLayout extends AppCompatActivity implements View.OnClickListener {
TextView textView1, textView2, textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textview_layout);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
textView1.setOnClickListener(this);
textView3.setOnClickListener(this);
textView2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.textView1:
Toast.makeText(this, “This is our first Text View”, Toast.LENGTH_LONG).show();
break;
case R.id.textView2:
Toast.makeText(this, “This is our Second Text View”, Toast.LENGTH_LONG).show();
break;
case R.id.textView3:
Toast.makeText(this, “This is our Third Text View”, Toast.LENGTH_LONG).show();
break;
}
}
}
Android Manifest File
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.textviewLayout” />
</application>
</manifest>
TextView In Android – Output
Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Online Surveys – Earn Real Money by Online Surveys
Kids Books – 10+ books for Kids | Age group 2 – 10 | Improve knowledge & Productivity