Chapter 4
Chapter 4
ViewGroup
The ViewGroup class is a subclass of the View class. ViewGroup instances work as
containers for View instances to group View instances together. Android contains the
following commonly used ViewGroup subclasses:
LinearLayout ,RelativeLayout
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.
Attributes of Android
1 android:id
This is the ID which uniquely identifies the control.
2 android:capitalize
If set, specifies that this TextView has a textual input method and should
automatically capitalize what the user types.
3 android:cursorVisible
Makes the cursor visible (the default) or invisible. Default is false.
4 android:editable
If set to true, specifies that this TextView has an input method.
5 android:fontFamily
Font family (named by string) for the text.
6 android:gravity
Specifies how to align the text by the view's x- and/or y-axis when the text is smaller
than the view.
7 android:hint
Hint text to display when the text is empty.
8 android:inputType
The type of data being placed in a text field. Phone, Date, Time, Number, Password
etc.
9 android:maxHeight
Makes the TextView be at most this many pixels tall.
10 android:maxWidth
Makes the TextView be at most this many pixels wide.
11 android:minHeight
Makes the TextView be at least this many pixels tall.
12 android:minWidth
Makes the TextView be at least this many pixels wide.
13 android:password
Whether the characters of the field are displayed as password dots instead of
themselves. Possible value either "true" or "false".
14 android:phoneNumber
If set, specifies that this TextView has a phone number input method. Possible value
either "true" or "false".
15 android:text
Text to display.
16 android:textAllCaps
Present the text in ALL CAPS. Possible value either "true" or "false".
17 android:textColor
Text color. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or
"#aarrggbb".
18 android:textColorHighlight
Color of the text selection highlight.
19 android:textColorHint
Color of the hint text. May be a color value, in the form of "#rgb", "#argb", "#rrggbb",
or "#aarrggbb".
20 android:textIsSelectable
Indicates that the content of a non-editable text can be selected. Possible value either
"true" or "false".
21 android:textSize
Size of the text. Recommended dimension type for text is "sp" for scaled-pixels
(example: 15sp).
22 android:textStyle
Style (bold, italic, bolditalic) for the text. You can use or more of the following values
separated by '|'.
normal - 0
bold - 1
italic – 2
23 android:typeface
Typeface (normal, sans, serif, monospace) for the text. You can use or more of the
following values separated by '|'.
normal - 0
sans - 1
serif - 2
monospace – 3
<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"
tools:context=".MainActivity">
<TextView
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Before Clicking"
android:textColor="#f00"
android:textSize="25sp"
android:textStyle="bold|italic"/>
</RelativeLayout>
Edit Text
EditText is a standard entry widget in android apps. It is an overlay over TextView that
configures itself to be editable.
EditText is a subclass of TextView with text editing operations.
We often use EditText in our applications in order to provide an input or text field, especially
in forms.
Attributes of EditText:
id: id is an attribute used to uniquely identify a text EditText. Below is the example code in
which we set the id of a edit text.
gravity: The gravity attribute is an optional attribute which is used to control the alignment
of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.
hint: hint is an attribute used to set the hint i.e. what you want user to enter in this edit text.
Whenever user start to type in edit text the hint will automatically disappear.
textColor: textColor attribute is used to set the text color of a text edit text. Color value is in
the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
textSize: textSize attribute is used to set the size of text of a edit text. We can set the text size
in sp(scale independent pixel) or dp(density pixel).
textStyle: textStyle attribute is used to set the text style of a edit text. The possible text styles
are bold, italic and normal. If we need to use two or more styles for a edit text then “|”
operator is used for that.
background: background attribute is used to set the background of a edit text. We can set a
color or a drawable in the background of a edit text.
<EditText
android:id="@+id/simpleEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="right"
android:text="Username"
android:hint="Enter Your Name Here"
android:textColor="#f00"
android:textColorHint="#0f0"
android:textSize="25sp"
android:textStyle="bold|italic"
android:background="#000"
/>
android:background="@color/design_default_color_secondary"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="19dp"
android:ems="10"
android:hint="password"
android:inputType="numberPassword" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_marginTop="12dp"
android:ems="10"
android:hint="E_mail"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/button"
style="@android:style/Widget.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_marginTop="62dp"
android:text="submit"
android:textSize="16sp"
android:textStyle="normal|bold" />
</RelativeLayout>
Java File:
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (name.getText().toString().isEmpty() || password.getText().toString().isEmpty() ||
email.getText().toString().isEmpty() )
{
Toast.makeText(getApplicationContext(), "Enter the Data",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Name - " + name.getText().toString()
+ " \n" + "Password - "
+ password.getText().toString()+ " \n" + "E-Mail - " +
email.getText().toString() ,Toast.LENGTH_SHORT).show();
}
}
});
}
}
Button
In Android, Button represents a push button. A Button is a Push-button which can be pressed,
or clicked, by the user to perform an action. Button is a subclass of TextView class
Attribute of Button:
gravity: The gravity attribute is an optional attribute which is used to control the alignment
of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.
textColor: textColor attribute is used to set the text color of a Button. Color value is in the
form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
textSize: textSize attribute is used to set the size of the text on Button.
<Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:layout_centerInParent="true"
android:gravity="right|center_vertical"
android:textColor="#f00"
android:textSize="20sp"
android:textStyle="bold|italic"
android:textStyle="bold|italic"
android:drawableBottom="@drawable/ic_launcher"/>
We can perform action on button using different types such as calling listener on button or
adding onClick property of button.
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//code
}
});
Example
<EditText
android:id="@+id/editText1"
android:inputType="number" />
<EditText
android:id="@+id/editText2"
android:inputType="number" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:text="ADD" />
</RelativeLayout>
Java File:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
buttonSum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
String value1=t1.getText().toString();
String value2=t2.getText().toString();
int a=Integer.parseInt(value1);
int b=Integer.parseInt(value2);
int sum=a+b;
Toast.makeText(getApplicationContext(),String.valueOf(sum), Toast.LENGTH_LON
G).show();
}
});
}
}
Toggle Button
Android Toggle Button can be used to display checked/unchecked (On/Off) state on the
button.
It is beneficial if user have to change the setting between two states. It can be used to On/Off
Sound, Wifi, Bluetooth etc.
By default, the android ToggleButton will be in OFF (Unchecked) state. We can change the
default state of ToggleButton by using android:checked attribute.
Attributes :
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:textOn It is used to set the text when toggle button is in ON / Checked state.
android:textOff It is used to set the text when toggle button is in OFF / Unchecked state.
android:background It is used to set the background color for toggle button control.
android:padding It is used to set the padding from left, right, top and bottom.
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
Example
ImageButton
In Android, ImageButton is used to display a normal button with a custom image in a button.
In simple words we can say, ImageButton is a button with an image that can be pressed or
clicked by the users.
ImageButton has all the properties of a normal button so you can easily perform any event
like click or any other event which you can perform on a normal button.
Attributes:
1. id: id is an attribute used to uniquely identify a image button. Below is the example code in
which we set the id of a image button.
2. src: src is an attribute used to set a source file of image or you can say image in your
image button to make your layout look attractive.
3. background: background attribute is used to set the background of an image button. We
can set a color or a drawable in the background of a Button.
4. padding: padding attribute is used to set the padding from left, right, top or bottom of the
ImageButton.
paddingRight : set the padding from the right side of the image button.
paddingLeft : set the padding from the left side of the image button.
paddingTop : set the padding from the top side of the image button.
paddingBottom : set the padding from the bottom side of the image button.
padding : set the padding from the all side’s of the image button.
<ImageButton
android:id="@+id/simpleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/home"
android:padding="30dp"/>
Checkbox:
CheckBox is a type of two state button either unchecked or checked in Android. Or you
can say it is a type of on/off switch that can be toggled by the users. You should
use checkbox when presenting a group of selectable options to users that are not mutually
exclusive.
Attributes of CheckBox :
1. id: id is an attribute used to uniquely identify a check box
2. checked: checked is an attribute of check box used to set the current state of a check box.
3. gravity: The gravity attribute is an optional attribute which is used to control the
alignment of the text in CheckBox like left, right, center, top, bottom, center_vertical,
center_horizontal etc.
4. text: text attribute is used to set the text in a check box.
5. textColor: textColor attribute is used to set the text color of a check box. Color value is in
form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
6. textSize: textSize attribute is used to set the size of text of a check box. We can set the
text size in sp(scale independent pixel) or dp(density pixel).
7. textStyle: textStyle attribute is used to set the text style of the text of a check box. The
possible text styles are bold, italic and normal. If we need to use two or more styles for a text
view then “|” operator is used for that.
8. background: background attribute is used to set the background of a check box. We can
set a color or a drawable in the background of a check box.
9. padding: padding attribute is used to set the padding from left, right, top or bottom.
paddingRight :set the padding from the right side of the check box.
paddingLeft :set the padding from the left side of the check box.
paddingTop :set the padding from the top side of the check box.
paddingBottom :set the padding from the bottom side of the check box.
Padding :set the padding from the all side’s of the check box.
<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple CheckBox"
android:checked="true"
android:gravity="right|center_vertical"
android:text="Text Attribute Of Check Box"/>
tools:context=".MainActivity">
<TextView
android:id="@+id/select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30dp"
android:text="Select your food"/>
<CheckBox
android:id="@+id/pizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/select"
android:text="Pizza"/>
<CheckBox
android:id="@+id/burger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pizza"
android:text="Burger"/>
<CheckBox
android:id="@+id/tea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/burger"
android:text="Tea"/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tea"
android:text="Submit"/>
</RelativeLayout>
Java File
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
pizza = findViewById(R.id.pizza);
tea = findViewById(R.id.tea);
burger = findViewById(R.id.burger);
buttonOrder = findViewById(R.id.submit);
buttonOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int totalAmount = 0;
StringBuilder result = new StringBuilder();
result.append("Selected Items");
if (pizza.isChecked())
{
result.append("\n Pizza");
totalAmount+=100;
}
if (burger.isChecked())
{
result.append("\n Burger");
totalAmount+=150;
}
if (tea.isChecked())
{
result.append("\n Tea");
totalAmount+=10;
}
result.append("\nTotal: " + totalAmount + "Rs");
Toast.makeText(getApplicationContext(), result.toString(),
Toast.LENGTH_SHORT).show();
}
});
}
}
RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one
radio button out of several radio button added in it will automatically unchecked all the
others.
It means at one time we can checked only one radio button from a group of radio buttons
which belong to same radio group.
RadioGroup is a widget used in Android for the grouping of radio buttons and provide the
feature of selecting only one radio button from the set.
When a user try to select any other radio button within same radio group the previously
selected radio button will be automatically unchecked.
Attributes
checked: checked attribute in radio button is used to set the current state of a radio button.
gravity: The gravity attribute is an optional attribute which is used to control the alignment
of text like left, right, center, top, bottom, center_vertical, center_horizontal etc.
textColor: textColor attribute is used to set the text color of a radio button.
textSize: textSize attribute is used to set the size of the text of a radio button.
textStyle: textStyle attribute is used to set the text style of the text of a radio button.
background: background attribute is used to set the background of a radio button. We can
set a color or a drawable in the background of a radio button.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/simpleRadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<RadioButton
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textSize="25sp"
android:textStyle="bold|italic"
android:padding="20dp"
android:layout_centerHorizontal="true"
android:text="Male"
android:textColor="#f00"
android:background="#000"/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/male"
android:text="Female"/>
</RadioGroup>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/group"
android:layout_marginTop="99dp"
android:layout_centerHorizontal="true"
android:text="Submit" />
</RelativeLayout>
Java File:
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
male=findViewById(R.id.male);
female=findViewById(R.id.female);
b1=findViewById(R.id.submit);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String selected;
if(male.isChecked())
{
selected="You selected"+male.getText();
}
else
{
selected="You Selected"+female.getText();
}
Toast.makeText(getApplicationContext(),selected,Toast.LENGTH_LONG).show();
}
});
ProgressBar
ProgressBar is a user interface control which is used to indicate the progress of an operation.
For example, downloading a file, uploading a file.
Following is the pictorial representation of using a different type of progress bars in android
applications.
By default the ProgressBar will be displayed as a spinning wheel, in case if we want to show
it like horizontal bar then we need to change the style property to horizontal like style="?
android:attr/progressBarStyleHorizontal".
Attribute Description
android:progress It is used to set the default progress value between 0 and max. It must be an integer
value.
Generally, we use the Determinate progress mode in progress bar when we want to show the
quantity of progress has occurred. For example, the percentage of file downloaded, number of
records inserted into database, etc.
<ProgressBar
android:id="@+id/pBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="50" />
Indeterminate progress mode in progress bar when we don’t know how long an operation
will take or how much work has done.
In indeterminate mode the actual progress will not be shown, only the cyclic animation will
be shown to indicate that some progress is happing
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"/>
<ProgressBar
android:id="@+id/p2"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/p1"
android:layout_centerVertical="true"
android:layout_marginTop="103dp" />
<ProgressBar
android:id="@+id/p3"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/p2"
android:layout_centerVertical="true"
android:layout_marginTop="118dp"
android:indeterminate="true" />
</RelativeLayout>
Example:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar"
android:layout_below="@+id/progressBar"/>
</RelativeLayout>
Java File:
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.os.Handler;
}
}
ListView
ListView is used when you have to show items in a vertically scrolling list. Best example of it
is our device's Contact List.
With ListView, user can easily browse the information, while scrolling up and down. You
can set divider between every item and set its height and color as per your UI design.
Inside a ListView, we can show list of Text items by using TextView, or pictures using
ImageView, or any other view or a combination of views.
As ListView is generally used to display a large set of data, hence it is not feasible to
manually create list items for the complete data.
hence Android provides us with special Adapter classes which can be used to supply data
from datasets to ListView.
Attribute Description
android:divider Using this attribute we can specify a divider between List items. A
drawable or any color can be specified as a value for this attribute.
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/black"
android:dividerHeight="1dp"/>
Example
activity_main.xml
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffeb00"
android:dividerHeight="1dp" />
</RelativeLayout>
List_item.xml
Java File
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
listView=findViewById(R.id.list);
t1=findViewById(R.id.textview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String element="Festival Is: "+adapter.getItem(position);
Toast.makeText(getApplicationContext(),element,Toast.LENGTH_LONG).show();
}
});
}
}
GridView
GridView just works like ListView, . The only difference is that GridView is used to display
grid of View objects.
The view objects can be a Text view, an Image view or a view group which has both an
image and some text.
Best example for this view is phone gallery which shows images in a grid.
We can set number of columns to specific number and auto-fit the images into the columns.
Vertical and horizontal spacing between every single items of gridView can be set
by verticalSpacing and horizontalSpacing.
GridView Attributes
android:id
This is the ID which uniquely identifies the layout.
android:columnWidth
This specifies the fixed width for each column. This could be in px, dp, sp, in, or mm.
android:gravity
Specifies the gravity within each cell. Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.
android:horizontalSpacing
Defines the default horizontal spacing between columns. This could be in px, dp, sp, in, or
mm.
android:numColumns
Defines how many columns to show. May be an integer value, such as "100" or auto_fit
which means display as many columns as possible to fill the available space.
android:stretchMode
Defines how columns should stretch to fill the available empty space, if any. This must be
either of the values −
none − Stretching is disabled.
spacingWidth − The spacing between each column is stretched.
columnWidth − Each column is stretched equally.
spacingWidthUniform − The spacing between each column is uniformly stretched..
android:verticalSpacing
Defines the default vertical spacing between rows. This could be in px, dp, sp, in, or mm.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
tools:context=".MainActivity">
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffeb00"
android:dividerHeight="1dp"
android:numColumns="2"/>
</RelativeLayout>
List_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:id="@+id/textview"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="4dp"
android:textColor="#0000FF" />
Java File
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
gridView=findViewById(R.id.grid);
t1=findViewById(R.id.textview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String element="Festival Is: "+adapter.getItem(position);
Toast.makeText(getApplicationContext(),element,Toast.LENGTH_LONG).show();
}
});
}
}
ImageView
Image file is easy to use but hard to master in Android, because of the various screen sizes in
Android devices.
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/img_nature"/>
Attributes of ImageView:
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/lion"
android:padding="30dp"/>
ScrollView
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
<ScrollView android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 10" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
DatePicker and TimePicker are used to display date and time selection widget in android
application.
They can be used in either spinner mode or calendar mode ( date picker), clock mode ( time
picker ).
DatePicker Properties:
datePickerMode :
Value can be spinner or calendar. If set to calendar, it will display a calendar which let you
choose date. If set to spinner, it will display a spinner to let you choose date.
DatePicker Calendar Mode DatePicker Spinner Mode
calendarViewShown :
This is a boolean value, only take effect when datePickerMode is calendar. If set to true, it
will display a calendar.
spinnersShown :
This is a boolean value also, only take effect when datePickerMode is spinner. If set to true,
it will display a spinner.
If both above two properties set to true of false, it will show both a spinner besides a
calendar.
TimePicker Properties
timePickerMode :
Value can be spinner or clock. When set it’ value to clock, it will display a clock. When set
is’s value to spinner will display a spinner.
TimePicker Clock Mode TimePicker Spinner Mode
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
tools:context=".MainActivity">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_date"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"
android:id="@+id/btn_date"
android:layout_alignBottom="@+id/in_date"
android:layout_toRightOf="@+id/in_date"
android:layout_toEndOf="@+id/in_date" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_time"
android:layout_below="@+id/in_date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:id="@+id/btn_time"
android:layout_below="@+id/btn_date"
android:layout_alignLeft="@+id/btn_date"
android:layout_alignStart="@+id/btn_date" />
</RelativeLayout>
Java File:
package com.example.ifcdiv;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectDate = findViewById(R.id.btn_date);
selectTime = findViewById(R.id.btn_time);
date = findViewById(R.id.in_date);
time = findViewById(R.id.in_time);
selectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog=new DatePickerDialog(MainActivity.this, new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
date.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
},year,month,dayOfMonth);
datePickerDialog.show();
}
});
selectTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
hours = c.get(Calendar.HOUR_OF_DAY);
minutes = c.get(Calendar.MINUTE);
timePickerDialog=new TimePickerDialog(MainActivity.this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText(hourOfDay + ":" + minute);
}
},hours,minutes,false);
timePickerDialog.show();
}
});
}
}