Download Code

Android Studio TextView  Example Android

In Android Studio textview example android you will learn about Text View and how to set text/get text in/from a TextView.Let me give you some introduction about View.

View

View is a Base Class of Widgets.It is basic building block for UI components.It occupies rectangular area on screen.It handle every event on the screen like drawing etc.

Text View

Displays text to the user which can optionally be edited.Basically TextView is complete text editor but basic configuration not allows to edit it.

Now let’s go with implementation.

Create new android studio textview example android application project.If you forgot how to create new project just gothrough previous tutorial.

After creating your new android application project.As you know that you have two options to add Button/Textview.

  • Design View
  • Text View

So,choose is your’s but at the start i recommend you to just drag and drop these components within the design view.

activity_main.xml

Android Studio TextView,Android Studio TextView example,Android Studio Textview example android,textview example android,

Alright it’s a pretty simple task you have done recently.Now you are going to deal with MainActivity.java class.

What actually you will do with that layout when you click on button.TextView will change to Android Codec text and display it.

Basically beginner’s think that how they did it in the tutorial.I will tell you that thing what’s going on with code.

First how to set text within the TextView and get Text from the TextView.For that purpose you have two public methods.

  • setText() which set the text to TextView.
  • getText() which get the text from TextView.

One thing i want to tell you that how to refer your xml UI component within your java class.First drag and drop your any UI component after that you will find an attribute called android:id=”@+id/textview” now this is the key to make this component work.

Next i am going to refer that component in MainActivity.java class.

public class MainActivity extends AppCompatActivity{
    TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       setContentView(R.Layout.activity_main);
       txt = (TextView) findViewById(R.id.textview);    // this is called refer id.
    }
}

Ok,you have basic information about TextView methods and about TextView.So,now go to the MainActivity.java class and do the following steps.

  • Create UI Component variables (Button/TextView).
  • Refer id’s of UI Components.
  • Apply click event on button and settext,display text.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button setBtn;
    TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.textView);
        setBtn = (Button) findViewById(R.id.button);
        setBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt.setText("Android Codec");
                getTextOnButtonClick();   // displaying set text in TextView
            }
        });
    }
    //getting text of textview method
    public void getTextOnButtonClick() {
        Toast.makeText(getApplicationContext(),txt.getText().toString(),Toast.LENGTH_SHORT).show();
    }

}

Deploy your android studio textview example android application project.

Android Studio TextView,Android Studio TextView example,Android Studio Textview example android,textview example android,
That’s it for this tutorial.
Thanks.