In this tutorial we will learn about how to create a textview  android, how to get the text value using edittext from the user and set the text value on textview when button is clicked and their example .

TextView is an user interface design that displays text to the user . TextView is an android support widget which is used to show the text to the user .

TextView

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Add TextView in android xml layout :

Add TextView to your XML Layout file i.e. activity_main.xml to create a textview to set the value for better user experience or just add the following code to activity_main.xml file .

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

Using the following attributes we can customize the view of texview :

android:texttext to display
android:gravityalign the text by the view’s x- and/or y-axis
android:textColortext color
android:paddingto set the padding
android:textSizeto set text size
android:textStyleto set text style (bold, italic, bolditalic)

activity_main.xml :

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_above="@id/edittext"
        android:layout_margin="24dp"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:text="Text here" />


    <EditText
        android:id="@+id/edittext"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="24dp"
        android:hint="Enter your text here"/>


    <Button
        android:background="@android:color/holo_red_light"
        android:id="@+id/btn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginRight="24dp"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:text="Button" />


</RelativeLayout>

3. Now find the TextView and set the text to textview:

Now set the text value to TextView using setText method in MainActivity.java file.

TextView textView = (TextView)findViewById(R.id.textView);
//To set the value on TextView
textView.setText(textValue);

Also there so many methods which is used to work with text view :

length()It gives the lenth of characters in text view
setAllCaps(boolean allCaps) to set the characters of the text to transform input to ALL CAPS display
setPadding(int left, int top, int right, int bottom)to set the padding
setText()to set the text to be display
setTextColor(int color) to sets the text color to be display
setTextSize()to set text size

MainActivity.java :

package net.technxt;

import android.Manifest;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    TextView textView;
    EditText edittext;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.textView);
        edittext = (EditText) findViewById(R.id.edittext);

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //To get the value of EditText
                String textValue = edittext.getText().toString();
                //To set the value on TextView
                textView.setText(textValue);
            }
        });

    }

}

Now run the project/app in android device or any Emulator to add TextView in android app.

%d bloggers like this: