TextView

                                                                  TextView


Basically, the android widget class  is responsible for the design or user interface in android. It contain many other classes which give android user the ability to communicate with the interface. The text view is part of those classes which is contain in the widget. As the name implies, the main purpose of the text view is to display text on the screen on the android device.
    The text view can also be used to test for certain action as in the case of toast but it is a good practice when testing for certain action you should use toast because toast is more flexible and light weighted.
    Today, we shall understand how we can use the text view to develop in android. We shall also understand that most widget class (especially text view) acts as a form of button which means that in one way or the other it can listen to on click listener. We shall also understand how to declear the text view programmatically in the .java class


Firstly, look at a simple text view inside a linear layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:id="@+id/linearLayout1"
    android:gravity="center">


    <TextView
android:id="@+id/tv"
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Our main concern here is the text view. The text view is in the linear layout. If you like you can called it encapsulation.
For every  class in the widget class is assigned a unique Id. This id is what is used to identify two widget classes which are identical.
Every class in the widget class must possesses its space. Through the use of the space possesses by the class you can know the size, width and height of that class
A text view class must have its text value. This is because why they called it textview in the first instance.

Here is the complete code of the text view class which listen to on click listener when ever the text view is click.

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:id="@+id/linearLayout1"
    android:gravity="center">


    <TextView
android:id="@+id/tv"
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java



import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.graphics.*;

public class MainActivity extends Activity
{
TextView text;
//LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


text = (TextView)findViewById(R.id.tv);
text.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
text.setText("You Just Click On Me");
text.setHeight(200);
text.setTextSize(50);
text.setTextColor(Color.BLUE);
}});
}
    }



When you click on the text, it will change the text and display another text.






The first thing that was declear was the instance variable of the text view. This is because using an instance variable, the text view can get access to all method declear inside of the MainActivity.
The second, was to find the Id of the of the text.

Note: Notice how the text was use to set the text size, height and color. This is just the introduction of developing the text view programmatically. Every thing here can easily be accomplished when using the text view tag and its corresponding attribute in the main.XML


                                                    The Big Story
We have seen a little introduction of how the text view class can be developed programmatically. Here we shall go fully on how to use the text view class in the MainActivity without using the Main.XML. To make the journey simple we shall keep the linear layout in the xml

What you should take note of here is the id that is assign to the linear layout in the XML

public class MainActivity extends Activity 
{
TextView text;
LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
/* //add LInearLayout
linearLayout =(LinearLayout) findViewById(R.id.linearLayout1);

//add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);

//add textView
text = new TextView(getApplicationContext());
text.setText("Programming Anything new with TextView");
text.setTextColor(Color.BLACK);
text.setTextSize(30);
text.setPadding(50,0,50,0);
text.setLayoutParams(params);

//add the textView to LinearLayout
linearLayout.addView(text);

} }*/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:id="@+id/linearLayout1"
    android:gravity="center">

</LinearLayout>








After finding the linear layout id. We declear the layout params. Let us just say that the layout params is what will be needed to create the space of the linear layout. This space can now have basically the two attribute of any class which is the height and width.

We set our orientation to vertical  to let any other text to appear at the bottom of the text declear earlier. We set the gravity to center through the user of our variable of linear layout.

An Object was created for text view
text = new TextView(getApplicationContext()); This is used to get the instance of the text view class. Also note that the object can also be created using
text = new TextView(this);
The attribute of text color is very important. Without specifying the text color as black, the text might not be using the required color in your application.
Then we attach our text to the params and we set the TextView by calling the addView() function

Hope it was helpful because most of the widget class is what you shall be using to develop some if not all of your applications

Goodbye.





1 comment:

  1. I really enjoy the tutorial. you made it so simple and easy to understand.
    I will love to ask if there is any simple code that can make the text of the textview move on the screen?

    ReplyDelete