Android Club – How to show text in TextView?

First of all, let’s open your activity. It starts with this method:

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

    }

This line says that we are going to use activity_a layout as layout for our activity.

setContentView(R.layout.activity_a);

Open res folder, then layout, and open activity_a.xml. Inside it put one TextView. After adding TextView, your layout should look like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context="net.joerichard.helloandroid.ActivityA">

    <TextView
       android:id="@+id/txtCreate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="#000000" />

</LinearLayout>

Here you can see that we set txtCreate as TextView’s id. Later we will refer this id to get access to this TextView. Now open your Activity class, then refer TextView by its id. Then use method setText to set text.

    private static Integer counter_create = 0;

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        TextView txtCreate = (TextView) findViewById(R.id.txtCreate);
        counter_create++;
        txtCreate.setText(counter_create);
       
    }

Pay attention to that we created static class int variable(counter_create) which we increment each time we call corresponding method(onCreate).

Now your task is to count all callbacks and show in different TextViews. Good luck!