Chapter 4MAD
Chapter 4MAD
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Hello"
android:textColor="#F00"
android:background="#00F"
/>
</RelativeLayout>
Contents of MainActivity.java
package com.example.demoapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= findViewById(R.id.textview1);
}
}
2. EditText
• It provide an input or text field where user can enter
something like his name details, phone number etc.
• EditText is a subclass of TextView with text editing
operations
• It is like a TextField control.
• EditText is represented by the Android
class android.widget.EditText.
EditText
Creation of EditText
• EditText code in XML:
<EditText android:id="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
• EditText code in JAVA:
EditText editText1 = findViewById(R.id.editText1);
EditText
Attributes or Properties of EditText
Sr Attribute
No.
1. android:id: id is an attribute used to uniquely identify a edit
text This can be retrieved using findViewById() method.
Ex: android:id="@+id/edittext1”
2. android: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. Ex: android:gravity="center_horizontal”
3 android:autoText: If set, specifies that this EditText has a
textual input method and automatically corrects some
common spelling errors.
4 android:text This is the Text to display.
EditText
Sr Attribute
No.
5 android:drawableBottom :This is the drawable to be drawn
below the text.
6. android:drawableRight :This is the drawable to be drawn to
the right of the text.
7. android:editable:If set, specifies that this EditView has an
input method.
8 android:background: background attribute is used to set the
background of a edit view. We can set a color or image in the
background of a edit view. Ex: android:background=“#00b“
9 android:onClick :This is the name of the method in this View's
context to invoke when the view is clicked.
10 android:visibility: This controls the initial visibility of the view.
It may take one of the 3 values: visible,invisible,gone.
Example of EditText
Contents of activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Name" />
</RelativeLayout>
Contents of MainActivity.java
package com.example.demoapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName= findViewById(R.id.editText1);
}
}
3. AutoCompleteTextView
• AutoCompleteTextView is an type of edit text.
• It gives suggestions to the user if the user types
something in the AutoCompleteTextView. The
suggestions list is shown in a drop down menu from
which a user can select the desired item.
• The list of suggestions is obtained from an adapter and
it appears only after a number of characters that are
specified in the threshold.
• It is subclass of EditText class
• AutoCompleteTextView is represented by the Android
class android.widget.AutoCompleteTextView.
AutoCompleteTextView
Creation of AutoCompleteTextView
• AutoCompleteTextView code in XML:
< AutoCompleteTextView android:id="@+id/autotv"
android:layout_height="wrap_content"
android:layout_width="match_parent“/>
• AutoCompleteTextView code in JAVA:
AutoCompleteTextView actv= findViewById(R.id.autotv);
AutoCompleteTextView
Attributes or Properties of AutoCompleteTextView
Sr Attribute
No.
1. android:id: id is an attribute used to uniquely identify a auto
complete text view. This can be retrieved using findViewById()
method. Ex: android:id="@+id/autotv”
2. android:completionThreshold :This defines the number of
characters that the user must type before completion
suggestions are displayed in a drop down menu.
3 android:completionHint :This defines the hint displayed in the
drop down menu.
4 android:dropDownAnchor: This is the View to anchor the
auto-complete dropdown to.
Attributes or Properties of AutoCompleteTextView
Sr Attribute
No.
5. android:dropDownHeight:This specifies the basic height of
the dropdown.
6. android:dropDownWidth :This specifies the basic width of the
dropdown.
7 android:popupBackground :This sets the background.
8 android:dropDownHorizontalOffset Amount of pixels by
which the drop down should be offset horizontally.
9 android:dropDownVerticalOffset Amount of pixels by which
the drop down should be offset vertically
Example of AutoCompleteTextView
Contents of activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<AutoCompleteTextView
android:id="@+id/autotv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_centerInParent="true"
android:hint="Search"/>
Contents of MainActivity.java
package com.example.demoapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
actv=(AutoCompleteTextView) findViewById(R.id.autotv);
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,branches);
actv.setAdapter(adapter);
actv.setThreshold(1);
}
4. Button
• A Button is a Push-button. Button can be pressed, or
clicked, by the user to perform an action.
• There are different types of buttons used in android such
as CompoundButton, ToggleButton, RadioButton.
• Button is a subclass of TextView class .
• Button may contain text and images. To add both image
and text in android button, use button
property android:drawableTop, android:drawableBottom,
android:drawableLeft or android:drawableRight.
• Buttons with images on are also called ImageButton.
• Button is represented by the Android
class android.widget.Button.
Button
Creation of Button
• Button code in XML:
<Button
android:id="@+id/btsubmit"
android:layout_height="wrap_content“
android:layout_width="match_parent“
android:text=“Submit”/>
</RelativeLayout>
using onClick attribute
Contents of MainActivity.java
package com.example.sampleapp; etName=findViewById(R.id.editText1);
tvresult=findViewById(R.id.textView1);
import android.app.Activity;
}
import android.os.Bundle;
public void submit(View V)
import android.view.View;
{
import android.widget.EditText;
String s=etName.getText().toString();
import android.widget.TextView;
tvresult.setText("Welcome "+s);
}
public class MainActivity extends Activity
}
{
EditText etName;
TextView tvresult;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Example of Button using OnClickListener
Contents of activity_main.xml file <Button
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/button1"
<RelativeLayout android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/ android:layout_height="wrap_content"
apk/res/android" android:layout_below="@+id/editText1“
android:layout_height="match_parent" android:layout_centerHorizontal="true"
android:layout_width="match_parent"> android:layout_marginTop="82dp“
android:text=“Submit" />
<EditText
android:id="@+id/editText1" <TextView
android:layout_width="wrap_content" android:id="@+id/textView1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentTop="true" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_below="@+id/button1"
android:layout_marginTop="56dp" android:layout_centerHorizontal="true"
android:ems="10“/ > android:layout_marginTop="76dp"
android:text="TextView" />
</RelativeLayout>
using OnClickListener
Contents of MainActivity.java
package com.example.sampleapp; etName=findViewById(R.id.editText1);
tvresult=findViewById(R.id.textView1);
import android.app.Activity;
bt=findViewById(R.id.button1);
import android.os.Bundle;
bt.setOnClickListener(new
import android.view.View;
View.OnClickListener() {
import android.widget.EditText;
public void onClick(View v)
import android.widget.TextView;
{
Import android.widget.Button;
String s=etName.getText().toString();
tvresult.setText("Welcome "+s);
public class MainActivity extends Activity }
{ });
EditText etName; }
TextView tvresult;
Button bt; }
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
5. ImageButton
• ImageButton is a button with an image that can be
pressed or clicked by the users.
• ImageButton is represented by the Android
class android.widget.ImageButton.
• has all the properties of a normal button
• We can add a image to the button by using
attribute android:src of <ImageButton> in XML
layout file and within java class by using
setImageResource() method.
ImageButton
Creation of ImageButton
• ImageButton code in XML:
<ImageButton
android:id="@+id/btsubmit"
android:layout_height="wrap_content“
android:layout_width="match_parent“
android:src="@drawable/submit_icon”/>
3 android: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.
4 android:src: used to set a source file of image.
Ex: android:src="@drawable/home"
<ImageButton
android:id="@+id/imgBt1"
android:layout_width="131dp"
android:layout_height="50dp"
android:src="@drawable/submit"
android:layout_centerInParent="true“ />
</RelativeLayout>
Contents of MainActivity.java
package com.example.sampleapp; bt.setOnClickListener(new
View.OnClickListener() {
import android.app.Activity; public void onClick(View v)
import android.os.Bundle; {
import android.view.View; Toast.makeText(getApplicationContext(),"This is
Import android.widget.ImageButton; ImageButton",Toast.LENGTH_LONG).show();
}
public class MainActivity extends Activity });
{ }
ImageButton bt;
@Override }
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt= findViewById(R.id.bton1);
6. ToggleButton
• Used to display ON (Checked) or OFF (Unchecked) states as
a button with a light indicator.
• useful for the users to change the settings between two
states either ON or OFF.
• example of ToggleButton is doing on/off in sound,
Bluetooth, wifi, hotspot etc.
• ToggleButton belongs to android.widget.ToggleButton class.
• It is a subclass of CompoundButton.
• By default, ToggleButton will be in OFF (Unchecked) state.
We can change the state of ToggleButton by using
android:checked attribute.
• For ex: android:checked = “true”
• From Android 4.0 Switch control
can be used
ToggleButton
Creation of ToggleButton
• ToggleButton code in XML:
<ToggleButton
android:id="@+id/tb1"
android:layout_height="wrap_content“
android:layout_width=“wrap_content“
android:checked="true"
android:drawableLeft="@drawable/bluetooth"
android:textOff="OFF"
android:textOn="ON“ />
• ToggleButton code in JAVA:
ToggleButton b1=findViewById(R.id.tb1);
ToggleButton
Attributes or Properties of ToggleButton
Sr Attribute
No.
1 android:id: id is an attribute used to uniquely identify a togglebutton. This
can be retrieved using findViewById() method. Ex: android:id="@+id/tb”
2 android:onClick :This is the name of the method in this View's context to
invoke when the view is clicked.
3 android:textOff This is the text for the togglebutton when it is not
checked.
4 android:textOn This is the text for the togglebutton when it is checked.
</RelativeLayout>
Contents of MainActivity.java
package com.example.sampleapp; tb.setOnClickListener(new
View.OnClickListener() {
import android.app.Activity;
public void onClick(View v)
{
import android.os.Bundle;
if(tb.isChecked())
import android.view.View; Toast.makeText(getApplicationContext(),
Import android.widget.ToggleButton; “Toggle On", Toast.LENGTH_SHORT).show();
import android.widget.Toast; else
Toast.makeText(getApplicationContext(),
public class MainActivity extends Activity “Toggle Off", Toast.LENGTH_SHORT).show(); }
{ });
ToggleButton tb; }
@Override
protected void onCreate(Bundle savedInstanceState) }
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb=(ToggleButton) findViewById(R.id.tb1);
Example of ToggleButton using
OnCheckedChangeListener
Contents of activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ToggleButton
android:id="@+id/tb1"
android:layout_width="96dp"
android:layout_height="40dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="130dp"
android:checked="true“
android:textOff="OFF"
android:textOn="ON“/> </RelativeLayout>
Example of ToggleButton using OnCheckedChangeListener
Contents of MainActivity.java
tb.setOnCheckedChangeListener(new
package com.example.sampleapp;
CompoundButton.OnCheckedChangeListe
ner()
import android.app.Activity;
{
import android.os.Bundle;
@Override
import android.view.View;
public void
Import android.widget.ToggleButton;
onCheckedChanged(CompoundButton
import android.widget.CompoundButton; buttonView, boolean isChecked)
import android.widget.Toast; {
if(isChecked)
public class MainActivity extends Activity Toast.makeText(getApplicationContext(),
{ “Toggle On", Toast.LENGTH_SHORT).show();
ToggleButton tb;
@Override else
Toast.makeText(getApplicationContext(),
protected void onCreate(Bundle savedInstanceState)
“Toggle Off", Toast.LENGTH_SHORT).show(); }
{
});
super.onCreate(savedInstanceState);
}
setContentView(R.layout.activity_main);
tb=(ToggleButton) findViewById(R.id.tb1);
}
7. RadioButton
• Radio buttons allow the user to select only one option to
select from the group of options
• A radio button consists of two states – checked and
unchecked. Clicking an unchecked button changes its state to
“checked” state and “unchecked” for the previously selected
radio button.
• Radio buttons are used with a RadioGroup to combine
multiple radio buttons into one group and it will make sure
that user can select only one option from the group of
multiple options.
• RadioButton belongs to android.widget.RadioButton class.
• It is subclass of CompoundButton.
• We can change the default state of RadioButton by
using android:checked attribute.
RadioButton
RadioButton
Attributes or Properties of RadioButton
Sr Attribute
No.
1 android:id: id is an attribute used to uniquely identify a radiobutton. This
can be retrieved using findViewById() method. Ex: android:id="@+id/rb”
2 android:onClick :This is the name of the method in this View's context to
invoke when the view is clicked.
3 android:textOff This is the text for the radiobutton when it is not checked.
<RadioButton android:id="@+id/rb1”
android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:text=“Male“/>
<RadioButton android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:text=“Female" />
</RadioGroup>
RadioButton
Creation of RadioButton
• RadioButton code in Java:
RadioButton b1=findViewById(R.id.rb1);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkId)
{
switch(checkId)
{
case R.id.rbmale: tv.setText("Male is selected");
break;
case R.id.rbfemale: tv.setText("Female is selected");
break;
}
}
});
}
}
8. CheckBox
• It has two states –checked or unchecked.
• Checkboxes allow the user to select one or more
options from a set. For example, selecting hobbies.
• CheckBox belongs to android.widget.CheckBox class.
• It is subclass of CompoundButton.
• We can change the default state of CheckBox by
using android:checked attribute.
CheckBox
Attributes or Properties of CheckBox
Sr Attribute
No.
1 android:id: id is an attribute used to uniquely identify a checkbox. This can
be retrieved using findViewById() method. Ex: android:id="@+id/cb”
2 android:onClick :This is the name of the method in this View's context to
invoke when the view is clicked.
3 android:checked: takes boolean value and represents the initial state of
the toggle button
4 android:background: background attribute is used to set the background
of a text view. We can set a color or image in the background of a text
view. Ex: android:background=“#00b“
5 android:gravity:It is used to specify how to align the text like left, right, center,
top, etc.
CheckBox
Attributes or Properties of CheckBox
Sr Attribute
No.
6 android:text: text attribute is used to set the text in a checkbox
<ProgressBar
android:id="@+id/progress"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:max="100"
android:progress="0" />
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="200dp"
android:text="Start" /> </RelativeLayout>
Example of ProgressBar
Contents of MainActivity.java file
package com.example.progressdemo;
import androidx.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
pb = findViewById(R.id.progress);
b = findViewById(R.id.bt);
Example of ProgressBar
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setProgressValue(progress);
} });
}
void setProgressValue(final int progress) {
pb.setProgress(progress);
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
setProgressValue(progress+10);
} };
thread.start();
}
}
ListView
• ListView is a view which groups several items and display
them in vertical scrollable list.
• list items are automatically inserted to the list using
an Adapter that pulls data from a source such as an array
or database.
• ListView subclasses is of AdapterView .
• List View is by default scrollable.
• A list view does not know the details, such as type and
contents.
• list view requests views on demand from a Adapter as
needed, such as to display new views as the user scrolls up
or down.
• Adapter will act as an intermediate between the data
sources and adapter views such as ListView
• Types of adapters
ArrayAdapter It will expects an Array or List as input.
CursorAdapter It will accepts an instance of cursor as an input.
SimpleAdapter It will accepts a static data defined in the
resources.
BaseAdapter It is a generic implementation for all three
adapter types and it can be used
for ListView, Gridview or Spinners based on
our requirements
setAdapter(adapter)
ListView
Attributes or Properties of ListView
Sr Attribute
No.
• In Java File
ListView listView = findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
ArrayAdapter
• Adapter will act as an intermediate between the data
sources and adapter views.
• Adapter can be used to supply the data to like spinner, list
view, grid view etc.
• ArrayAdapter is useful when list contains single type of
items which stored in an array. For ex, list of phone
contacts, countries or names.
• Creation of ArrayAdapter
ArrayAdapter(Context context, int resource,Type[] objects)
1. context means the reference of current class( this keyword),
getApplicationContext() can also be used.
2. resource id used to set the layout(xml file) for list items in which
we have a text view.
3. array of objects, used to set the array of elements in the textView.
ArrayAdapter
• Ex:
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
Example of ListView
Contents of activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="@+id/animal"
android:layout_width="match_parent"
android:layout_height="wrap_content“
android:divider="#000“
android:dividerHeight="2dp“/ >
</LinearLayout>
Example of ListView
Contents of MainActivity.java file
package com.example.listdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent“
android:layout_height="wrap_content“
android:padding="1dp“
android:numColumns="3" />
</LinearLayout>
Example of GridView
Contents of MainActivity.java file
package com.example.griddemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent“
android:layout_height=“200dp“
android:src=“@drawable/rose" />
</RelativeLayout>
Example of ImageView
Contents of MainActivity.java file
package com.example.imageviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ScrollView & HorizontalScrollView
• It is a view group that allows the view hierarchy placed
within it to be scrolled.
• Scroll view and HorizontalScrollView can have only one
direct child placed within it.
• To add multiple views within the scroll view, we need to
use different layouts like TableLayout, RelativeLayout
Linear Layout.
• ScrollView and HorizontalScrollView are subclasses
of FrameLayout.
• Scroll view supports vertical scrolling only. For horizontal
scrolling, use HorizontalScrollView.
• Never add a ListView to a scroll view. List View is default
scrollable.
ScrollView and HorizontalScrollView
• Creation of ScrollView • Creation of
• code in XML: HorizontalScrollView
<ScrollView • code in XML:
android:id="@+id/scrollView“
<HorizontalScrollView
android:layout_width="fill_parent“
android:id="@+id/hscrollView“
android:layout_height="fill_parent">
android:layout_width="fill_parent“
android:layout_height="fill_parent">
<!-- add child view’s here -->
<!-- add child view’s here -->
</ScrollView>
</HorizontalScrollView>
ScrollView
Attributes or Properties of ScrollView
Sr Attribute
No.
1 android:id: id is used to uniquely identify a ScrollView. This can be
retrieved using findViewById() method. Ex: android:id="@+id/tb”
2 android:scrollbars: used to show the scrollbars in horizontal or vertical
direction. The possible Value of scrollbars is vertical, horizontal or none.
By default scrollbars is shown in vertical direction in scrollView and in horizontal
direction in HorizontalScrollView.
Example of ScrollView
Contents of activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<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”>
<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" <Button
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Button 1" /> android:layout_height="wrap_content"
android:text="Button 6" />
<Button
<Button
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Button 2" /> android:text="Button 7" />
<Button <Button
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:text="Button 8" />
android:text="Button 3" /> <Button
<Button android:layout_width="fill_parent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="Button 9" />
android:text="Button 4" /> <Button
android:layout_width="fill_parent"
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Button 10" />
android:layout_height="wrap_content"
android:text="Button 5" /> </LinearLayout>
</ScrollView
</RelativeLayout>
Example of ScrollView
Contents of MainActivity.java file
package com.example.scrollviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Toast
• Toast is a small popup notification.
• Toast is used to display information for a certain amount of
time and automatically disappear after a timeout.
• A Toast can be displayed for either 2 seconds
(Toast.LENGTH_SHORT) or 3.5 seconds (Toast.LENGTH_LONG).
• For example, clicking Send on an email triggers a "Sending
message..." toast, as shown in the following screen capture:
• Syntax:
Toast.makeText(context, "message", duration).show();
• Example:
Toast.makeText(MainActivity.this, ”Clicked on Button",
Toast.LENGTH_SHORT).show();
• Methods of Toast
void setGravity(int gravity, int xOffset, int Set the location at which the notification should
yOffset) appear on the screen.
First parameter can be:
Gravity.TOP, Gravity.LEFT, Gravity.RIGHT,
Gravity.BOTTOM, Gravity.CENTER_HOROZONTAL,
Gravity.CENTER, Gravity.CENTER_VERTICAL
void setDuration(int duration) Set how long to show the view for.
void setText(CharSequence s) Update the text in a Toast that was previously created
using one of the makeText() methods.
void show() Show the view for the specified duration.
static Toast makeText(Context context, Make a standard toast that just contains text.
CharSequence text, int duration)
void setMargin(float hMargin, float vMargin) Set the margins of the view.
<Button
android:id="@+id/bt"
android:layout_width=“wrap_content“
android:layout_height=“wrap_content“
android:onClick=“submit”
android:text=“Button" />
</RelativeLayout>
Example of Toast
Contents of MainActivity.java file
package com.example.toastdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
<ImageView
android:layout_width="80dp"
android:layout_height="49dp"
android:layout_marginRight="10dp"
android:src="@drawable/rose" />
<TextView
android:id="@+id/txtvw"
android:layout_width="281dp"
android:layout_height="36dp"
android:layout_marginTop="13dp"
android:textColor="#FFF"
android:textSize="15dp“
android:text="This is Custom Toast"/>
activity_main.xml
<Button
android:id="@+id/b1"
android:layout_width=“wrap_content"
android:layout_height=“wrap_content"
android:layout_marginTop="100dp“
android:layout_centerHorizontal="true"
android:text=“Custom Toast Demo”/>
</RelativeLayout>
MainActivity.java
package com.example.customtoast;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.b1);
}
public void submit(View v)
{
LayoutInflater inf=getLayoutInflater();
View vw=View.inflate(this,R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_container));
Toast toast= new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,180);
toast.setView(vw);
toast.show();
}
}
DatePicker
• DatePicker is used to select a date.
• It allows to select date by day, month and year.
• Date pickers helps to ensure that users can pick a date
that is valid, formatted correctly, and adjusted to the
user's locale.
• If we need to show DatePicker view as a dialog then we
have to use a DatePickerDialog class. DatePickerDialog
is a simple dialog containing DatePicker.
• DatePicker is the subclass of FrameLayout class.
• Date Picker is available in two modes either spinner or
calendar. Calendar mode is deprecated after API level
21.
Creating Date Picker
• Creating a Date Picker
• Code in XML
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner" />
• Code in Java
DatePicker datepicker=findViewById(R.id.datePicker);
Methods of Date Picker
1 getDayOfMonth() This method gets the selected day of month
2 getMonth() This method gets the selected month. month is 0 based
3 getYear() This method gets the selected year
4 setMaxDate(long maxDate) This method sets the maximal date
supported by this DatePicker in milliseconds since January 1, 1970
00:00:00 in getDefault() time zone
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content“
android:layout_height="wrap_content“
android:datePickerMode="spinner" />
<Button
android:id="@+id/bt"
android:layout_width=“wrap_content“
android:layout_height=“wrap_content“
android:onClick=“submit”
android:text=“Button" /> </RelativeLayout>
Example of DatePicker
Contents of MainActivity.java file
package com.example.datepickerdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Button;
import android.widget.Toast;
String date=d+“/”+m+”/”+y;
• Code in Java
TimePicker timepicker=findViewById(R.id.timePicker);
Methods of Time Picker
1 is24HourView()
This method returns true if this is in 24 hour view else false
2 isEnabled()
This method returns the enabled status for this view
3 setCurrentHour(Integer currentHour)
This method sets the current hour .
setCurrentHour() method was deprecated in API level 23. From api
level 23 we have to use setHour(Integer hour).
4 setCurrentMinute(Integer currentMinute)
This method sets the current minute
setCurrentMinute() method was deprecated in API level 23. From
api level 23 we have to use setMinute(Integer minute).
5 setEnabled(boolean enabled)
This method set the enabled state of this view
6 setIs24HourView(Boolean is24HourView)
This method set whether in 24 hour or AM/PM mode
Methods of Time Picker
7 setOnTimeChangedListener(TimePicker.OnTimeChangedListener
onTimeChangedListener)
This method Set the callback that indicates the time has been
adjusted by the user
8 int getCurrentHour()
This method returns the current hour
getCurrentHour() method was deprecated in API level 23. From api
level 23 you have to use getHour(). This method returns an integer
value.
9 int getCurrentMinute()
This method returns the current minute
getCurrentMinute() method was deprecated in API level 23. From
api level 23 we have to use getMinute().
TimePicker
Attributes or Properties of TimePicker
Sr Attribute
No.
1 android:id: id is used to uniquely identify a TimePicker. This can be
retrieved using findViewById() method. Ex: android:id="@+id/timePicker”
2 android:timePickerMode: used to set the Date Picker in mode either
spinner or clock. Default mode is clock. Clock mode is no longer used after api
level 21.
3 android: background: used to set the background of a DatePicker. We can
set a color or a drawable in the background.
4 android:padding : used to set the padding from left, right, top or bottom
of the Imageview.
• Event Handling on TimePicker
1)onClick attribute
2)OnClickListener
3)OnTimeChangedListener
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content“
android:layout_height="wrap_content“
android:timePickerMode="spinner" />
</RelativeLayout>
Example of TimePicker
Contents of MainActivity.java file
package com.example.datepickerdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Button;
import android.widget.Toast;
timepicker=(TimePicker) findViewById(R.id.timePicker);
timepicker.setIs24HourView(false); // used to display AM/PM mode i.e 12 hour
Example of TimePicker