Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views49 pages

Unit Ii

The document discusses different types of views in Android, which are user interface elements that draw visible content on activity screens. It describes common view types like TextView, EditText, and Button. It provides details on creating views by extending classes like View or using XML tags, and includes attributes for TextView and EditText.

Uploaded by

john joseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views49 pages

Unit Ii

The document discusses different types of views in Android, which are user interface elements that draw visible content on activity screens. It describes common view types like TextView, EditText, and Button. It provides details on creating views by extending classes like View or using XML tags, and includes attributes for TextView and EditText.

Uploaded by

john joseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Views (UI Elements, Controls or Widgets)

Views are the user interacting elements in an Activity window. Actually a view draws something visible content on
the activity screen. All android Views are derived from an android class called View. Some of the popular android Views
(Widgets/controls/UI elements) are TextView, EditText, ImageView, Button etc.

View is the base class of all types of User Interface elements, Different Views such as ImageView, TextView, Button
ImageButton etc etc and different ViewGroups (Layouts) such as LinearLayout AbsoluteLayout, RelativeLayout,
TableLayout, ConstraintLayout etc etc are all derived from this View class since View is the top most class in the android
View System.
View class is derived from the Object class
The View class hierarchy is given below:

AutoComplet RadioGroup
eTextView

Where is the location of this View class? or where can we find the definition of this View class?
Go to the following package
android.view.
Different Views

1. TextView

2. ImageView

3. ViewGroup

4. Button

5. EditText

6. CompoundButton

7. RadioButton

8. CheckBox

9. Switch

10. ToggleButton

11. AdapterView

12. AbsSpinner

13. Spinner

14. AbsListView

15. ListView

16. LinearLayout

17. AbsoluteLayout

18. RelativeLayout

19. FrameLayout

20. ConstraintLayout

21. HorizontalScrollView

22. ScrollView

Note: All library classes has a naming convention in which all the staring letters of all words in the class name are
written in Capital letters.
Eg: HorizontalScrollView, AbsSpinner and Spinner etc.

While Camel notation is used in naming library methods in the library classes where, all letters of first word of the
method name is written in lower case letters, starting letters of all other words in the method name are written in Upper case
letters.
Eg: setContentView(),setOnCheckedChangeListener() etc.
How to make a view (or UI element, control, Widget)?
1. By Extending the System library class View and its various sub classes

eg. creation of a TextView:


TextView t1=new TextView();

creation of a button view:


Button b1=new Button();

2. By using corresponding XML tag

<TextView>
<Button>
<EditText>

<LinearLayout></LinearLayout> etc

We choose the xml option to create various UI elements (Views, Controls, Widgets) in our android application because
it is little bit easier method rather than using pure Java View classes and its objects. Anyway, Android compiler will
convert all of our XML View tags in to corresponding GUI classes in Java at the time of compilation.

Some of the Commonly used Widgets (Views) in Android are


1. TextView
2. ImageView
3. EditText
4. Button
5. RadioButton
6. CheckBox
7. Switch
8. ToggleButton
9. Spinner
10. PrograssBar
11. ScrollView
12. GridView
1. TextView
TextView is the UI element which displays the text on the screen to the user (similar to a label in HTML). The
displayed text will not be editable.

There are two methods to create a TextView:

1. Using the TextView class and its methods (The location of TextView class is the package android.widget)
Example

TextView t1=new TextView(this);


t1.setText("Demo Text");
LinearLayout l= (LinearLayout)findViewById(R.id.LL1);
l.addView(t1);

Some of the TextView calss metrhods are given below:


setText()
getText()
setHint()
setTextColor(Color.COLORNAME)
setHintTextColor(Color.COLORNAME)
setTextSize()
setBackgroundColor(Color.COLORNAME)
It is pure Java code, it is quite simple.

2. Using XML tag <TextView> and its attributes Example:


<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=”match_parent” android:layout_height=”match_parent”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Demo Text” />

</LinearLayout>

We choose the XML tag method, because it is more simple than the pure Java code.

Let us study the <TextView> tag and its Attributes:


<TextView> is usually an empty tag, so use the ‘/’ along with the opening tag before the ‘>’ bracket
<TextView/>
You may also use this tag as a paired tag <TextView></TextView>

<TextView> tag attributes


1. All standard xml tag attributes.
2. android:text – Displays the Text for the TextView element
3. android:textColor – Specifies the color for the text (formats are #rgb, #argb, #aarrggbb)
4. android:textStyle – Specifies the text style such as normal, bold, italic etc
5. android:textSize – Specifies a text size for the text, size must be in the following format(in,mm,px,pt,dp,sp)
6. android:typeface – specifies a font type for the text in the TextView element such as normal, monospaced, serif, sans
7. android:fontFamily – specifies a font name for the text in the TextView
8. android:maxHeight – Specifies a maximum height in Pixels for the TextView
9. android:minHeight – Specifies a minimum height in Pixels for the TextView
10. android:maxWidth – Specifies a maximum width in Pixels for the TextView
11. android:minWidth – Specifies a minimum width in Pixels for the TextView
12. android:textAllCaps - Present the text in ALL CAPS. Possible value either "true" or "false"

A TextView Element using pure Java Code

Layout File (XML File – activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LL1">
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=new TextView(this); t1.setText("Demo Text");
LinearLayout l= (LinearLayout)findViewById(R.id.LL1);
l.addView(t1);

}
}

A TextView Element using XML tag (A more Simple Method)

Layout File (XML File - activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="10pt"
android:textColor="#FF0000"
android:typeface="monospace"
android:fontFamily="sans-serif-condensed"
android:text="Demo TextView"/>
</LinearLayout>
Activity File (JAVE File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

3. EditText
EditText is another common Widget seen in most of the android apps, it allows the user to input a
multiline editable text.
There are two methods to create EditText View.

1. Using the EditText class and its methods (EditText is a direct Sub class of TextView class. The location of
EditText class is the package android.widget)

2. Using XML tag <EditText> and its attributes Example:


<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:hint=”User Name” />

</LinearLayout>

<EditText> tag Attributes:


1. All Standard attributes

2. All Attributes of <TextView> tag are applicable to <EditText> tag.

3. Some extra attributes:


android:inputType=”textURI/textPersonName/textPassword/textEmailAddress/date/time/numbe r/numberPassword”
Layout File (XML File - activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="10pt"
android:textColor="#0000FF"
android:typeface="monospace"
android:fontFamily="sans-serif-condensed"
android:hint="Demo TextView"
android:textColorHint="#FF0000"
android:inputType="textPassword"/>
</LinearLayout>

Activity File (Java File MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
3. Button View
Button View creates Push- Button on the Screen with a text on Top of it.
How to create a Button?
1. Using the class Button (it is derived from TextView class). Create an object of it. The definition contains in
the package android.widget.
2. Using <Button> xml tag
<Button> tag is an empty tag .
Attributes of Button Tag:
1. All of the <TextView> tag attributes
2. All standard xml tag attributes.

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="10pt"
android:text="Button View"/>
</LinearLayout>

Activity File (JAVA file - MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
How to invoke a function on Clicking a Button?
How to display message to the user?
First problem can be easily solved by a static interface defined in View class called ‘OnClickListener’ which will
listen the occurrence of click event on a particular View (UI element/Control).
OnClickListener interface is having an abstract method in it called onClick(View).
All you have to do is just implement this Interface in to your activity class and override the onClick(View) method
with your required code.

How to add an OnClickListener object to a particular view?


By two means,
Method 1:
By using the method setOnClickListener(OnClickListener Object) of all Views
Example:
Button b1=(Button)findViewById(R.id.Btn1);
View.OnClickListener cl=new View.OnClickListener()
{
public void onClick(View v)
{
//Your code here;
b1.setOnClickListener(cl);
}
}
another Short hand method (here we are using a nameless object of View.OnClickListener)

B1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//Your code here
}
});

Another Method (more simple method)

Just write the function which should be executed in connection with the Button (or any View) click event.
Just mention that function name in the onClick attribute of the <Button> (or any View) tag
Example:
public void DemoFn(View v)
{
//your code here which should be executed in connection with Click event
}

Link this function to a View (Button etc) using the standard attribute onClick
<Button
Android:onClick=”DemoFn” />
Example Method 1:
Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button View"
android:textColor="#FFFFFF" android:id="@+id/Btn1"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View; import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1=(Button) findViewById(R.id.Btn1);
View.OnClickListener cl=new View.OnClickListener()
{

public void onClick(View v)


{
Toast.makeText(getApplicationContext(),"Button clickedusing
method 1",Toast.LENGTH_LONG).show();
}
};

B1.setOnClickListener(cl);
}

Method 2
Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button View"
android:textColor="#FFFFFF" android:id="@+id/Btn1"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1=(Button) findViewById(R.id.Btn1);
B1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button clickedusing method
2",Toast.LENGTH_LONG).show();

}
}
);
}
}
Method 3

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button View"
android:textColor="#FFFFFF"
android:onClick="DemoFn"/>
</LinearLayout>

package com.example.myapp1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View; import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1=(Button) findViewById(R.id.Btn1);
}
public void DemoFn(View v)
{
Toast ts=Toast.makeText(getApplicationContext(),"Buttonclicked using method
3",Toast.LENGTH_LONG);

ts.show();
}
}

How to display a message to the user

Using the Toast class and its methods


What is a Toast Message in Android?
An Android Toast is a small message displayed on the screen, similar to a tool tip or other similar popup
notification. A Toast is displayed on top of the main content of an activity, and only remains visible for a short time
period. It is used to display a message or an information to the user. The toast message will automatically disappear from
the activity screen.
It can be compared to the alert message in JavaScript, but unlike alert message, a toast message will automatically
disappear from the screen after a pre-defined period of time. In alert message user has to explicitly close the message by
clicking the ‘close’ button or ‘OK’ Button on the message.
How to create a Toast Message in android?
Using the Library class ‘Toast’
Create an object of this Toast class

Let us familiar with some of the members of this Toast class

Static member variables (used to set the duration of the Toast message display) of Toast class
LENGTH_LONG
LENGTH_SHORT

Non static constructor of Toast class Toast(Context) – used to construct a Toast Object

Static member function of Toast class


makeText(Context, StringMessage, Duration) – used to create a Toast Object and returns the created
Toast Object

Non Static member functions of Toast class

setView(View) – used to add a view in to the Toast Object


setDuration(int) – used to set a display duration for the Toast Message
setGravity(Gravity.value1| Gravity.value2|…Gravity.valuen, x-offset, y-offset) –
used to set the position of the Toast Message on the screen (by default: almost center bottom of the screen)
show() – used to make visible our Toast Object on the Activity screen

A simple example for default Toast message Layout File (XML file – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button View"
android:textColor="#FFFFFF"
android:onClick="DemoFn"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public void DemoFn(View v)
{
Toast t=Toast.makeText(this,"Simple default Toast
message",Toast.LENGTH_LONG);
t.show();
}
}
A simple example of Custom Toast Message
Layout File for the Toast Object (toast_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toast Message"
android:background="#0000FF"/>
</LinearLayout>

Layout File for the Activity Window (activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button View"
android:textColor="#FFFFFF"
android:onClick="DemoFn"/>
</LinearLayout>

Activity File (Java File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public void DemoFn(View v)
{

LayoutInflater li = getLayoutInflater();
View toastV = li.inflate(R.layout.toast_layout, null);
Toast t = new Toast(getApplicationContext());
t.setView(toastV);
t.setDuration(Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_HORIZONTAL & Gravity.CENTER_VERTICAL,0,0);
t.show();
}
}

4.ImageView
ImageView is used to display an image on the activity screen .

How to create an ImageView?


1. Using ImageView class (directly derived from View class) and its methods. ImageView
class definition is stored in android.widget package
2. Using the XML tag <ImageView> and its attributes
<ImageView> tag attributes. It is an Empty tag that is only Opening (Start) tag, no Closing (End) tag
All Standard/Global Attributes
Some special attributes of <ImageView> Tag:

1. android:scaleType - Scale type options are used for scaling the bounds of an image to the bounds of the
imageview. Its possible values are center, centerCrop, fitXY, fitStart, fitEnd, centerInside, fitCenter and matrix
2. android:src – used to specify the source of the image (drawable) file
Note: Save your image file in the drawable folder of res folder. Use @drawable/imagereference in XML (Layout)
File to refer the image file, and use R.drawable.imagereference in Java (Activity) file to refer the image file.

ImageView example
Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/androidlogo" />
</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
5.ImageButton
ImageButton is similar to ImageView except it can be pushed like a Button.
In simple words we can say, ImageButton is a button with an image that can be pressed or clicked by the users.
Important Note: 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.
Creation of An ImageButton
1. Using the ImageButton class (Derived from ImageView class) and its methods. Location of ImageView
class definition: android.widget package
2. Using XML tag < ImageButton> and its attributes
Attributes:
1. All Standard/Global attributes
2. All <Button> tag attributes
3. All <ImageView> tag attributes

ImageButton example

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/homebutton"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
CheckBox in android is nothing but a two state button, checked and
unchecked states respectively. CheckBox is used to input more than one options
from a given set of options.

How to create A CheckBox in android?


1. Using the CheckBox view class (stored in package
android.widget) – a subclass of CompoundButton class. Create
an object of CheckBox class
2. Using xml tag <CheckBox> and its attributes.
<CheckBox> tag attributes
All <Button> tag attributes are applicable to <CheckBox>
A special attribute of <CheckBox> tag is checked, it is a Boolean
attribute (Receives either true or false as its value) used to
make the CheckBox checked by default
Syntax

<CheckBox android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Red Color”
android:checked=”true”/>

A simple CheckBox Example


Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:id="@+id/CB1"
android:onClick="DemoFn"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"
android:id="@+id/CB2"
android:onClick="DemoFn"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"
android:id="@+id/CB3"
android:onClick="DemoFn"
/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

public void DemoFn(View v)


{

if(v.getId()==R.id.CB1)
{
CheckBox c1 = (CheckBox) findViewById(R.id.CB1);
Toast t= Toast.makeText(this, c1.getText().toString(),
Toast.LENGTH_LONG);
t.show();
}
else if(v.getId()==R.id.CB2)
{
CheckBox c2 = (CheckBox) findViewById(R.id.CB2);
Toast t = Toast.makeText(this, c2.getText().toString(),
Toast.LENGTH_LONG);
t.show();
}
else if(v.getId()==R.id.CB3)
{
CheckBox c3 = (CheckBox) findViewById(R.id.CB3);
Toast t = Toast.makeText(this, c3.getText().toString(),
Toast.LENGTH_LONG);
t.show();
}
}
}

RadioButton and RadioGroup

RadioButton
RadioButton is also a two state button just like the CheckBox, the two states are ‘checked’ and
‘unchecked’. It allows the user to input any one option from the given list of options. Example
Gender selection control found in forms, in which either ‘Male’ or ‘Female’ option can be selected
not both.

How to create A RadioButton in android?


1. Using the RadioButton view class (stored in package
android.widget) – a subclass of CompoundButton class. Create
an object of RadioButton class
2. Using xml tag <RadioButton> and its attributes.
<RadioButton> tag attributes
All <Button> tag attributes are applicable to <RadioButton>
A special attribute of <RadioButton> tag is checked, it is a
Boolean attribute (Receives either true or false as its value)
used to make the RadioButton checked by default
Syntax

<RadioButton android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Male”
android:checked=”true”/>

RadioGroup is an android widget used to group a set of child Radio Buttons,


among which one can be checked at a time. That is, if we check one radio button that
belongs to a radio group, it automatically unchecks any previously checked radio button
within the same group.
How to Create a RadioGroup?
1. Using the View class RadioGroup, it is a direct subclass of the
LinearLayout class. The package android.widget contains its
definition.
2. Using xml tag <RadioGroup> and its attributes
All standard attributes are applicable to <RadioGroup>
An important attribute of <RadioGroup> is orientation (possible
values: horizontal and vertical [by default])

An Example
<RadioGroup android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”/>

<RadioButton android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Male”/>
<RadioButton android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Female”/>

</RadioButton>
A simple example of RadioButton and RadioGroup
Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="@+id/RB1"
android:onClick="DemoFn" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/RB2"
android:onClick="DemoFn" />
</RadioGroup>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

public void DemoFn(View v)


{

if(v.getId()==R.id.RB1)
{
RadioButton c1 = (RadioButton) findViewById(R.id.RB1);
Toast t= Toast.makeText(this,
c1.getText().toString(),Toast.LENGTH_LONG);
t.show();
}
else if(v.getId()==R.id.RB2)
{
RadioButton c2 = (RadioButton) findViewById(R.id.RB2);
Toast t = Toast.makeText(this,
c2.getText().toString(),Toast.LENGTH_LONG);
t.show();
}

}
}

Another Programme which displays the selected RadioButton text on clicking a Button View
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/RG1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="@+id/RB1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/RB2"
/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Submit"
android:id="@+id/SBtn1"/>
</RadioGroup>

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RadioGroup r= (RadioGroup)findViewById(R.id.RG1);
Button b= (Button)findViewById(R.id.SBtn1);

b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
int i=r.getCheckedRadioButtonId();
RadioButton rb=(RadioButton)findViewById(i);
Toast t =
Toast.makeText(getApplicationContext(), rb.getText(),Toast.LENGTH_LONG);
t.show();
}
}
);
}
}
ToggleButton is also a two-state button the ON (Checked) and OFF (Unchecked)
state respectively. A toggle button allows the user to change a setting between two states.

How to create a ToggleButton?

1. Using ToggleButton class (a direct sub class of CompoundButton class). Its location:
android.widget package
2. Using <ToggleButton> xml tag and its attributes
Its attributes:
All standard (Global) attributes
All Button Attributes
android:checked – used to make the ToggleButton checked by default
android:textOn – used to specify a text when the ToggleButton is in ON state
androidTextOff - used to specify a text when the ToggleButton is in OFF state

A simple Programme

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="WiFi ON"
android:textOff="WiFi OFF"
android:id="@+id/TBtn1"
/>

</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ToggleButton tb= (ToggleButton)findViewById(R.id.TBtn1);

tb.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{

Toast t =
Toast.makeText(getApplicationContext(), tb.getText(),Toast.LENGTH_LONG);
t.show();
}
}
);
}
}
ProgressBar
ProgressBar is an Android widget used to display the progress of a process. It shows a bar which
shows how are the operation has progressed. Some Progress bar shows a secondary progress bar
which shows the progress of the intermediate operation such the buffer level of stream playback
progress bar.

There are two types of Progress bars in android

1. Determinate ProgressBar – which shows the progress of the operation by showing a


horizontal bar
2. Indeterminate ProgressBar – if we are unaware of the time to finish the operation or if we
dont know how much time will the process take to finish, but the process is still going on, it
is better to use a Indeterminate ProgressBar. Normally it would be a rotating circle. But we
can create horizontal Indeterminate ProgressBar also.
Creation of a ProgressBar
1. Using ProgressBar class and its methods
2. Using XML tag <ProgressBar> and its attributes
<ProgressBar> tag attributes
1. style=”?android:attr/progressBarStyleHorizontal”
2. android:indeterminate =”true/false”
3. android:max=”number” – Specifies total length of the ProgressBar
4. progress=”number” – specifies the amount of operation has been done so far
and all standard/global attributes

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100px"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginTop="100px"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginTop="100px"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
}
A Dynamic Determinate ProgressBar Example using Thread
Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginTop="100px"
android:id="@+id/PB1"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity implements Runnable
{
int progressvalue=0;
ProgressBar p;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p=(ProgressBar)findViewById(R.id.PB1);
Thread t=new Thread(this,"ProgressBarThread");
t.start();
}
public void run()
{
int i=0;
while(i<100)
{
try
{
progressvalue++;
p.setProgress(progressvalue);
Thread.sleep(500);
}
catch (Exception e)
{
}
i++;
}
}
}
Spinner
Spinner is another important UI in android which is used to select a single item from the given list
of items, just like the Radio Button in a RadioGroup.

A Spinner is a composite control made from a TextView ,a ButtonView and a ListView.

A Spinner selects a single option from a given list of options

New concepts in connection with a Spinner , an AutoCompleteTextView and a ListView, that you
have to understand are

1. AdapterView and ArrayAdapter


2. ItemSelected Event and the Interface used to track this Event OnItemSelectedListener of
AdapterView class, and two of its abstract methods which we have to override
onItemSelected(AdapterView<>, View, int, long) and onNothingSelected(AdapterView<>).

An AdapterView is a class which acts as a bridge between the data source (either a table from a
database or an Array) and the android UI element such as a Spinner and an
AutoCompleteTextView. It retrieves data items from the data source and organizes each item in
an appropriate layout such as simple_spinner_item. It also places each data items in a layout
used for the Spinner ListView such as simple_spinner_dropdown_view using the method
setDropDownViewResource(DropDownViewLayout). Use the setAdapter(Adapter) method to
attach an Adapter View object to any of the UI element such as Spinner and
AutoCompleteTextView.

Creation a Spinner

- Using Spinner class (Derived from AbsSpinner class). Its Location: android.widget package
- Using <Spinner> xml tag and its attributes (all standard attributes)

A simple Spinner Example

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Spinner android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SpinnerUI"
android:paddingLeft="100px"/>

</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity


{
String a[]={"Red ","Green ","Blue ","White ","Golden
"};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner s= (Spinner)findViewById(R.id.SpinnerUI);
ArrayAdapter aa=new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,a);

aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(aa);
s.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener()
{
public void
onItemSelected(AdapterView<?> ab,View v, int index,long id)
{

Toast.makeText(getApplicationContext(),a[index],Toast.LENGTH_LONG).show();
}
public void
onNothingSelected(AdapterView<?> x)
{
}

}
);

}
}
AutoCompleteTextView
AutoCompleteTextView is an important android widget which is similar to EditText, except that it
shows a list of completion suggestions automatically while the user is typing. The list of
suggestions is displayed in a drop down menu. The user can choose an item from there to replace
the content of edit box with.

How to create an AutoCompleteTextView?

1. Using the AutoCompleteTextView class and its methods. AutoCompleteTextView class is


derived from the EditText class. Its location: android.widget package
2. Using the XML tag < AutoCompleteTextView > and its attributes
< AutoCompleteTextView > tag attributes
All Standard attributes
All EditText attributes
android:completionThreshold - number ( This defines the number of characters that the
user must type before completion suggestions are displayed in a drop down menu)
android:completionHint - This defines the hint displayed in the drop down menu.
android:dropDownHeight - This specifies the basic height of the dropdown.
android:dropDownWidth - This specifies the basic width of the dropdown.

A very simple AutoCompleteTextView example

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity


{

String z[]={"Red","Green","Blue","White","Golden"};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView
actv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> aa=new
ArrayAdapter<String>(this,android.R.layout.select_dialog_item,z);
actv.setThreshold(1);
actv.setAdapter(aa);
}

}
ListView
ListView is a type of AdapterView that displays a vertical list of scroll-able views and each
view is placed one below the other.
Using adapter, items are inserted into the list from an array or database. For displaying the
items in the list method setAdaptor() is used. setAdaptor() method conjoins an adapter with the
list.
Android ListView is a ViewGroup (Refer the View class hierarchy given above) that is used to
display the list of items in multiple rows and contains an adapter that automatically inserts the
items into the list.
Note : ListView is a ViewGroup to hold different Child Views

Creation of ListView
1. Using ListView class (A sub class derived from AbsListView class). Its Location:
android.widget package
2. Using XML tag <ListView> and its attributes
<ListView> tag attributes
All Standard attributes
android:divider – specifies a colour or a drawable image as the divider between two list
items in the ListView
android:dividerHeight – specifies the divider’s height
android:entries - Reference to an array resource that will populate the ListView
android:footerDividersEnabled - When set to false, the ListView will not draw the divider
before each footer view
android:headerDividersEnabled - When set to false, the ListView will not draw the divider
before each header view

A very Simple ListView example


Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/LV1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity


{

String z[]={"Red","Green","Blue","White","Golden","Orange","Brown","Sky
Blue","Olive Green","Bright Red","Black","Majenta","Silver","Violet"};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ListOne=(ListView)findViewById(R.id.LV1);
ArrayAdapter<String> aa=new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,z);

ListOne.setAdapter(aa);
}

}
Grid View
Android GridView shows items in two-dimensional scrolling grid (rows & columns) and the
grid items are not necessarily predetermined but they automatically inserted to the layout using
a ListAdapter

An adapter actually bridges between UI components and the data source that fill data into
UI Component. Adapter can be used to supply the data to like Spinner, AutoCompleteTextView,
ListView, GridView etc.

The ListView and GridView are subclasses of AdapterView and they can be populated by
binding them to an Adapter, which retrieves data from an external source and creates a View that
represents each data entry.
A GridView looks like the image given below
Creation of GridView

1. Using the View class ‘GridView’ – derived from the ‘AbsListView’ class. Its location:
android.widget package
2. Using xml tag <GridView> and its attributes
<GridView> tag attributes
All Standard attributes such as android:layout_width, android:layout_height, id etc
android:verticalSpacing - Defines the default vertical spacing between rows. This could be in
px, dp, sp, in, or mm.
android:horizontalSpacing - Defines the default horizontal spacing between columns. This
could be in px, dp, sp, in, or mm.
android:columnWidth - This specifies the fixed width for each column. 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.

Note : Just like ListView, GridView is also a ViewGroup to hold different Child Views, both are
derived from AbsListView class. Please refer View class hierarchy which is given above.
Note: We can use simply a <GridView> in our layout without nesting it in any other ViewGroup
Object such as LinearLayout since GridView itself is a ViewGroup like LinearLayout. But in our
example, we have nested our GridView in a LinearLayout. But it is not mandatory.
A Simple GridView Example
Layout File (XML File – activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridView
android:id="@+id/GV1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:horizontalSpacing="4px"
android:verticalSpacing="50px"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity


{

String z[]={"Red","Green","Blue","White","Golden","Orange","Brown","Sky
Blue","Olive Green","Bright
Red","Black","Majenta","Silver","Violet","Red","Green","Blue","White","Gold
en","Orange","Brown","Sky Blue","Olive Green","Bright
Red","Black","Majenta","Silver","Violet","Red","Green","Blue","White","Gold
en","Orange","Brown","Sky Blue","Olive Green","Bright
Red","Black","Majenta","Silver","Violet","Red","Green","Blue","White","Gold
en","Orange","Brown","Sky Blue","Olive Green","Bright
Red","Black","Majenta","Silver","Violet"};

protected void onCreate(Bundle savedInstanceState)


{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GridView GridOne=(GridView)findViewById(R.id.GV1);

ArrayAdapter<String> aa=new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,z);
GridOne.setAdapter(aa);
}

}
ScrollView
ScrollView is another important View in android. The android.widget.ScrollView class provides the
functionality of scroll view. ScrollView is used to scroll the child elements of palette inside ScrollView.
Android supports vertical scroll view as default scroll view. Vertical ScrollView scrolls elements
vertically. Android uses HorizontalScrollView class for horizontal ScrollView.

As we all know ListView and GridView are also vertically scrollable, but they receive the data from an
external source (Data base or and Array), but our ScrollView does not insist on a Data base as well as
an Array instead it places all types of Views in a vertically scrollable View.

Note: ScrollView is better to view an UI element which has content larger than the height of the
window. Use a LinearLayout element within the ScrollView element

How to Create a ScrollView?

1. Using the view class ScrollView (a Sub class of FrameLayout class, please refer the View Class
hierarchy which is given at the beginning of the note). Its location: android.widget package
2. Using XML tag <ScrollView></scrollView> and its attributes

<ScrollView> is a paired tag

<ScrollView> tag attributes

All Standard attributes

android:scrollbars - Defines which scrollbars should be displayed on scrolling or not.

Let us do a Simple Example of ScrollView

Layout File (XML File – activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 5 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 6 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 7 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 8 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 9 "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 10"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 11"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 12"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 13"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 14"/>
</LinearLayout>

</ScrollView>

Activity File (JAVA File – MainActivity.java)


package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{

protected void onCreate(Bundle savedInstanceState)


{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

}
TimePicker
Android Time Picker allows you to select the time of day in either 24 hour or AM/PM
mode. The time consists of hours, minutes and clock format. Android provides this functionality
through TimePicker class.

How to create a TimePicker?


1. Using view class TimePicker (derived from FrameLayout class). Its
location:android.widget package
Some methods in TimePicker class
getCurrentHour()
getCurrentMinute()
setCurrentHour(Integer currentHour)
setCurrentMinute(Integer currentMinute)
2. Using xml tag <TimePicker> and its attributes
<TimePicker> tag attributes
All standard attributes (Please refer the topic Standard/Global attributes)
A Standard attributes
An Example

Layout File (XML File – activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GOD is Love"/>

</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DatePicker
Android Date Picker allows you to select the date consisting of day, month and year in your
custom user interface. Android provides this functionality through DatePicker class.

How to create a DatePicker?


1. Using view class DatePicker (derived from FrameLayout class). Its
location:android.widget package
Some methods in TimePicker class
getDayOfMonth()
getMonth()
getYear()
updateDate(int year, int month, int dayOfMonth)
2. Using xml tag <DatePicker> and its attributes
<DatePicker> tag attributes
All standard attributes (Please refer the topic Standard/Global attributes)
A Standard attributes
An Example

Layout File (XML File – activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<DatePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Activity File (JAVA File – MainActivity.java)

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity


{

protected void onCreate(Bundle savedInstanceState)


{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

You might also like