Views in Android
Views in Android
Introducing views
<ViewName
Attribute1=Value1
Attribute2=Value2
Attribute3=Value3
.
.
AttributeN=ValueN
/>
It always start with an angle bracket, followed by
the View name.
we write attributes that will define how that view
will look on the screen of the application along
with a value for the attribute. Each view has its
own attributes
Attribute Description
Android:text: Used to specify the text to be
displayed in the TextView
android:textSize : Using this attribute we can
control the size of the text.
android:textColor: Using this attribute we can
specify the color of our text.
android:textAllCaps If set True, this will make the
text appear in upper case.
android:letterSpacing : Using this attribute we can
set the spacing between letters of the text.
Android:hint :This attribute is used to show a
default text, if no text is set in the TextView.
Generally, when we populate a TextView using
dynamic data coming from server(using the
programmatic approach), then we set this attribute
to show some default text in the TextView until
data is fetched from server.
<TextView
android:id="@+id/st"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Studytonight"
android:textSize="45sp"
android:padding="20dp"
android:textColor="#DD2C00"/>
EditText View in Android
EditText is a TextView which is editable.
It has almost similar properties as a TextView.
EditText is used when you want to have a text field in
your application where user can enter any text.
It can be either single line or multi-line.
Touching a text field places makes the field active,
places a cursor and automatically displays the keyboard.
Following are some of the attributes that are most
commonly used:
Attribute Description
android:inputType
Used to specify what the text entered should be
like and for what purpose it will be used. If this is
set to none, then the text cannot be edited. Some
commonly used constant values for this attribute
are:
text
textAutoComplete - This provides with
suggestions as user is typing in text.
textAutoCorrect - This will enable auto correct on
user input text.
textPassword - Display the entered text in form of
dots or stars.
textUri
textEmailAddress
phone - This will present only the numeric
keyboard to users.
datetime, etc.
android:inputType="textCapSentences|textAutoCor
rect"
<EditText
android:id="@+id/et_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Write your email Address here"
android:textSize="20sp"
android:inputType="textWebEmailAddress"
android:maxLines="3"/>
CheckBox View in Android
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textColor="@android:color/black"/>
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="Female"/>
android:textColor="@android:color/holo_blue_dark
"
/>
Button b = (Button)
findViewById(R.id.btn_submit);
// setting on click event listener
b.setOnClickListener(new
View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="@android:color/holo_blue_dark
"
android:onClick="study"
/>
When user will click on the Button defined in the
above layout xml file, then Android system will call
study(View) method, defined in
MainActivity.java file. In order for this to
work, the method must be public and accept a
View type as its only parameter.
public void study(View view) {
ImageView in Android
Attribute Description
android:src="@drawable/img_nature"/>
A few more, commonly used attributes with
ImageView
android:src="@drawable/img_nature"/>
imageButton =
(ImageButton)findViewById(R.id.imgButton);
imageButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// do anything here
}
});
Switch