In this post we will discuss about 3 majorly used components in android.
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to android" />
Important properties of EditText :
android:id :- To define unique identity to your EditText.
Important properties of Button :
android:id :- To define unique identity to your Button.
android:background :- This is a drawable to use as the background.
android:onClick :- To define the method name which will be invoke when button is clicked.
Sample Code :
<Button android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:background="#FF0000"/>
Now we will develop one application with use of these controls.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textviewdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.textviewdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<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"
android:gravity="center_horizontal">
<EditText android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter UserName"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:textColor="#000000"/>
<Button android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Save"
android:layout_marginTop="10dp"
android:layout_below="@+id/edtName"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSave"
android:layout_marginTop="10dp"/>
</LinearLayout>
MainActivity,java
1. TextView
Displays text to the user. You can take is as a label.
Important properties of TextView :
android:id :- To define unique identity to your textview.
android:layout_width :- To define width of textview.
android:layout_height :- To define height of textview.
android:text :- To define text of textview.
Sample code :
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to android" />
2. EditText
EditText is a predefined subclass of TextView that includes rich editing capabilities.Important properties of EditText :
android:id :- To define unique identity to your EditText.
android:textColor :- To define text color of EditText contents.
android:editable : - If set, specifies that this EditText has an input method.
Sample Code :
<EditText android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter UserName"
android:textColor="#000000"/>
3. Button
A push-button that can be pressed, or clicked, by the user to perform an action.Important properties of Button :
android:id :- To define unique identity to your Button.
android:background :- This is a drawable to use as the background.
android:onClick :- To define the method name which will be invoke when button is clicked.
Sample Code :
<Button android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:background="#FF0000"/>
Now we will develop one application with use of these controls.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textviewdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.textviewdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<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"
android:gravity="center_horizontal">
<EditText android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter UserName"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:textColor="#000000"/>
<Button android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Save"
android:layout_marginTop="10dp"
android:layout_below="@+id/edtName"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSave"
android:layout_marginTop="10dp"/>
</LinearLayout>
MainActivity,java
package com.ravi.textviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btnSave;
EditText edtName;
TextView tvName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave=(Button)findViewById(R.id.btnSave);
edtName=(EditText)findViewById(R.id.edtName);
tvName=(TextView)findViewById(R.id.tvName);
btnSave.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnSave)
{
String name=edtName.getText().toString();
tvName.setText("Welcome " + name);
}
}
}






