Android Tutorial TextView

In this tutorial we are going know how to use android TextView with a simple TextView example.
We will create a simple app which helps us understand how to use Android TextView. In our app the user enters data in the EditText field and when the user clicks the button the entered data is displayed in the TextView.
In Android a TextView is used to show text to the user.


First of all we have to create a new project and in the .xml file create one EditText field, one Button widget and one TextView  widget by just dragging and dropping them in Android Studio.

Here is the .xml file code of our TextView example.
 
<?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" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:showIn="@layout/activity_main">
 
 <EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"
         android:id="@+id/eText" 
 android:layout_marginTop="64dp" 
 android:layout_alignParentTop="true" 
 android:layout_alignParentStart="true" />
 
<Button 
 style="?android:attr/buttonStyleSmall" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Button" 
 android:id="@+id/button" 
 android:onClick="onButtonClick" 
 android:layout_below="@+id/eText" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="52dp" />

 <TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Large Text" 
 android:id="@+id/textView" 
 android:layout_centerVertical="true" 
 android:layout_alignParentStart="true" />
</RelativeLayout>
 
Java code of our TextView example
 
 public class MainActivity extends AppCompatActivity {

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

           }
    public void onButtonClick(View view){
        EditText eText = (EditText)findViewById(R.id.eText);
        String userText = eText.getText().toString();
        TextView displayEnteredText = (TextView)findViewById(R.id.textView);
        displayEnteredText.setText(userText);

    }
} 
 
A String variable userText is created and is assigned the value it gets from the
user.The getText()method is used to get the text in the EditText field and is 
converted into string through the toString() method.
setText()method is used to set the text on the TextView.
 
So this was a simplest example of Android TextView. 

0 comments: