Download Code

Android TextView Example

In android textview example you will learn about TextView and how to set text in 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.

TextView

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

Now let’s go with implementation.

Create android textview example android application project named as Text View.

android textview,android textview example,textview example,

 

After your created android project.Now you have two option to add button on your layout.

  • Graphical Layout
  • XML Layout

First you should do that using Graphical Layout and then i’ll also tell you how to add through XML Layout.Graphical display is at your front just like this image.At the bottom of image with round rectangle you see two options Graphical and XML right now you are in Graphical Layout.If you click on activity_main.xml then you will be in xml layout just like second image.

android textview,android textview example,textview example,

android textview,android textview example,textview example,

Now there is UI component Panel on your left side you will see Text View at the top .It might be small,large or medium so doesn’t matter which one you drop.Now drag and drop that Button and Text View on your Graphical Layout.When you drop that Text View  and button it’ll display like this

android textview,android textview example,textview example,

Now go to MainActivity or which your eclipse by default created.Create variable for TextView and Button and if you see red line under Button and Text View and take your cursor over them and import.

public class MainActivity extends Activity{
           
    Button click;
    TextView txt;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Now refer the id of TextView and Button.Then your code will look like this

public class MainActivity extends Activity {

	Button click;
	TextView txt;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.androidcodec);
        click = (Button) findViewById(R.id.c);
    }
}

Now apply click Event on Button just write click and press dot (.) hint will be shown to you now type setOnClickListener.It should View don’t forget it.After that your code looks like this code

public class MainActivity extends Activity {

	Button click;
	TextView txt;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.androidcodec);
        click = (Button) findViewById(R.id.c);
        click.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
    }
}

Now next step is to set text whatever by default i’ll set on click event.Before i’ll tell about setText method.

setText

setText is basically a public method of Text View which set texts or character sequence in a Text View by refering it’s id.
Now you’ll write code for setText.setText method is called when you click on Button.setText code will be like this

public class MainActivity extends Activity {

	Button click;
	TextView txt;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.androidcodec);
        click = (Button) findViewById(R.id.c);
        click.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txt.setText("Android Codec");
			}
		});
    }

Now Run your android textview example project on your emulator or on your Android Phone.When application installed then it will display like this picture.

android textview,android textview example,textview example,

and when click on Button your TextView will set to “Android Codec” or whatever your write in parameter.

android textview,android textview example,textview example,
MainActivity.java

public class MainActivity extends Activity {

	Button click;
	TextView txt;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.androidcodec);
        click = (Button) findViewById(R.id.c);
        click.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txt.setText("Android Codec");
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

That’s it for this tutorial.
Thanks.