Android TextView


Android TextView is a User interface widget that displays basic texts.

In almost every Graphical User Interface toolkit out there, a component or control for displaying text is there. Be it the Label in Windows Forms or the JLabel in Swing.

This is because we mostly communicate via texts and these texts have to be rendered. Well the textview renders them in Android.

TextViews and labels are normally considered basic and are easy to work with.

In android textviews are actually editable though by default this is disabled. Instead it's subclass the EditText on the other hand allows for editing.

TextView as a class resides in the android.widget package.

package android.widget;

TextView derives from android.view.View class and implements import android.view.ViewTreeObserver.OnPreDrawListener.

public class TextView extends View implements OnPreDrawListener {}

TextViews can be created either programmatically or via inflation of XML. Here are the constructors to create a TextView object programmatically.

No. Constructor
1. public TextView(Context context)
2. public TextView(Context context, AttributeSet attrs)
3. public TextView(Context context, AttributeSet attrs, int defStyleAttr)
4. public TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Let's look at various TextView examples:

Creating TextView From XML Definition

Most of the time we define textviews via xml and inflate them in our activity. Here's a typical definition of android textview;

<TextView
    android:id="@+id/greetingsTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
/>
  • First we specify a unique id for the textview usingandroid:id="" attribute that will be used to reference the textview from C# code.
  • We then define the layout width(android:layout_width="") and height(android:layout_height="") of the textview.
  • Next we specify the text via the android:text="" attribute.

We then come to our MainActivity's OnCreate() method. OnCreate() method is a lifecycle callback that gets called when the activity in android has been created.

Normally we make view initializations here since the activity has been created.

But first we make sure that the following method has been invoked:

setContentView(R.layout.activity_main);
```language

The above method will inflate our activity_main.axml layout and set it as the layout of our activity. This layout has to be inflated first since it contains our TextView.

Then we come reference our TextView:

TextView greetingsTxt= (TextView) findViewById(R.id.greetingsTxt);

This will give us a textview reference which we can use to set text:

 greetingsTxt.setText("Hello World Over there");

If we run the project we get:

Xamarin TextView Example

Here's the full source code: activity_main.axml:

<?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="com.tutorials.hp.textviewapp.MainActivity">

    <TextView
        android:id="@+id/greetingsTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />
</RelativeLayout>

MainActivity.java

package com.tutorials.hp.textviewapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView greetingsTxt= (TextView) findViewById(R.id.greetingsTxt);
        greetingsTxt.setText("Hello World Over there");
    }
}

Creating TextView Programmatically as the ContentView of an Activity

It's not mandatory that you set a layout as the content view of an activity.

You can use a view instead of inflating a layout. If anything the layouts do get inflated into a view object.

However this is only suitable for simple interfaces. if you need a complex interface with nested widgets, then you use the layout as it's easier to write such declaratively.

First we instantiate a TextView programmatically, passing in the Context object:

TextView greetingsTxt=new TextView(this);

Let's then set the textview's background color programmatically:

greetingsTxt.setBackgroundColor(Color.GREEN);

Then set the text:

greetingsTxt.setBackgroundColor(Color.GREEN);

And finally set the content view:

setContentView(greetingsTxt);

Here's the full code. Note we don't need an xml layout:

package com.tutorials.hp.textviewapp;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView greetingsTxt=new TextView(this);
        greetingsTxt.setBackgroundColor(Color.GREEN);
        setContentView(greetingsTxt);
    }
}

Here's what we get: Android TextView Example

More

Let's look at some examples.

{% set card_number = 1 %} {% for p in page.find('/android/textview').children if p != page %}
{{ card_number }} . {{ p.title }}

{% if p.summary != p.content %} {{ p.summary }} {% else %} {{ p.content }} {% endif %}
{% set card_number = card_number + 1 %} {% endfor %}

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post