18 Text View
18 Text View
18 Text View
WORLD
TextView
A TextView displays text to the user and optionally allows them to edit it. A TextView is
a complete text editor, however the basic class is configured to not allow editing.
TextView Attributes
Following are the important attributes related to TextView control. You can check
Android official documentation for complete list of attributes and related methods
which you can use to change these attributes are run time.
Attribute Description
android:id This is the ID which uniquely identifies the control.
android:capitalize If set, specifies that this TextView has a textual input
method and should automatically capitalize what the
user types.
normal - 0
bold - 1
italic - 2
normal - 0
sans - 1
serif - 2
monospace - 3
Example
This example will take you through simple steps to show how to create your own
Android application using Linear Layout and TextView.
Ste Description
p
1 You will use Eclipse IDE to create an Android application and name it as
GUIDemo under a package com.example.guidemo as explained in the Hello World
Example chapter.
2 Modify src/MainActivity.java file to add a click event.
2 Modify the detault content of res/layout/activity_main.xml file to include Android
UI control.
3 Define required constants in res/values/strings.xml file
4 Run the application to launch Android emulator and verify the result of the
package com.example.guidemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"You have clicked the Label : " + Label,
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:capitalize="characters"
android:text="@string/hello_world" />
</RelativeLayout>
<string name="app_name">GUIDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guidemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your GUIDemo application. I assume you had created your AVD while
doing environment setup. To run the app from Eclipse, open one of your project's
activity files and click Run icon from the toolbar. Eclipse installs the app on your AVD
and starts it and if everything is fine with your setup and application, it will display
following Emulator window:
Now let's click on the Lable "Hello World", it will give following screen: