Android Unit 4
Android Unit 4
Text View
In android, Text View is a user interface control which is used to set and display the text to the
user based on our requirements. The Text View control will act as like label control and it won’t
allow users to edit the text.
A Text View is a entire text editor, however the basic class is configured to not allow editing
Attributes of TextView
Sr.No. Attribute & Description
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.
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
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
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
3 Unit No:04:Mobile Application Development Notes| P.S.Gaidhani
android:text="HelloWorld!"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginTop="350dp"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="italic"/>
</LinearLayout>
MainActivity.java File
package com.example.textviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output
EditText Attributes
1
android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects some
common spelling errors.
2
android:drawableBottom
This is the drawable to be drawn below the text.
3
android:drawableRight
This is the drawable to be drawn to the right of the text.
4
android:editable
If set, specifies that this TextView has an input method.
5
android:text
This is the Text to display.
1
android:background
This is a drawable to use as the background.
3
android:id
This supplies an identifier name for this view.
4
android:onClick
This is the name of the method in this View's context to invoke when the view is clicked.
5 android:visibility
This controls the initial visibility of the view.
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:text="EditText Example" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:ems="10"
android:text="@string/enter_text" android:inputType="text" />
</RelativeLayout>
MainActivity.java File
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eText = (EditText) findViewById(R.id.edittext);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str = eText.getText().toString();
Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
msg.show();
Button Attributes
Following are the important attributes related to Button control. You can check Android official
documentation for complete list of attributes and related methods which you can use to change
these attributes are run time.
Inherited from android.widget.TextView Class −
1
android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects some
common spelling errors.
2
android:drawableBottom
This is the drawable to be drawn below the text.
3
android:drawableRight
This is the drawable to be drawn to the right of the text.
4
android:editable
If set, specifies that this TextView has an input method.
5
android:text
This is the Text to display.
Attribute Description
1
android:background
2
android:contentDescription
This defines text that briefly describes content of the view.
3
android:id
This supplies an identifier name for this view.
4
android:onClick
This is the name of the method in this View's context to invoke when the view is
clicked.
5
android:visibility
This controls the initial visibility of the view.
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:gravity="center"
/>
</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Welcome in GGSP",
Toast.LENGTH_SHORT).show();
}
});
}
}
Output
ImageButton Attributes
Following are the important attributes related to ImageButton control. You can check Android
official documentation for complete list of attributes and related methods which you can use to
change these attributes are run time.
Inherited from android.widget.ImageView Class −
android:adjustViewBounds
1
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its
drawable.
2 android:baseline
android:baselineAlignBottom
3
If true, the image view will be baseline aligned with based on its bottom edge.
android:cropToPadding
4
If true, the image will be cropped to fit within its padding.
android:src
5
This sets a drawable as the content of this ImageView.
1
android:background
This is a drawable to use as the background.
2
android:contentDescription
This defines text that briefly describes content of the view.
3
android:id
This supplies an identifier name for this view
4
android:onClick
This is the name of the method in this View's context to invoke when the view is clicked.
5
android:visibility
This controls the initial visibility of the view.
<TextView android:text="ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher_foreground"/>
</RelativeLayout>
MainActivity.java
package com.example.textviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
@Override
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Image Change",
Toast.LENGTH_SHORT).show();
b1.setImageResource(R.drawable.ic_launcher_background);
}
});
}
}
Output
ToggleButton Attributes
Following are the important attributes related to ToggleButton control. You can check Android
official documentation for complete list of attributes and related methods which you can use to
change these attributes are run time.
1
android:disabledAlpha
This is the alpha to apply to the indicator when disabled.
2
android:textOff
This is the text for the button when it is not checked.
3
android:textOn
This is the text for the button when it is checked.
1
android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects some
2
android:drawableBottom
This is the drawable to be drawn below the text.
3
android:drawableRight
This is the drawable to be drawn to the right of the text.
4
android:editable
If set, specifies that this TextView has an input method.
5
android:text
This is the Text to display.
1
android:background
This is a drawable to use as the background.
2
android:contentDescription
This defines text that briefly describes content of the view.
3
android:id
This supplies an identifier name for this view,
4
android:onClick
This is the name of the method in this View's context to invoke when the view is clicked.
5
android:visibility
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="150dp"
android:orientation="horizontal">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tb1"
android:layout_gravity="center_horizontal"
android:checked="false"
android:drawablePadding="20dp"
android:textColor="#000"
/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tb2"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="50dp"
android:checked="true"
android:drawablePadding="20dp"
android:textColor="#000"
/>
</LinearLayout>
<Button
MAinActivity.java File
package com.example.togglebutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
RadioGroup Attributes
Following are the important attributes related to RadioGroup control. You can check Android
official documentation for complete list of attributes and related methods which you can use to
change these attributes are run time.
Attribute Description
android:checkedButton This is the id of child radio button that should be checked by default within this
radio group.
1
android:background
This is a drawable to use as the background.
2
android:contentDescription
This defines text that briefly describes content of the view.
3
android:id
This supplies an identifier name for this view
4
android:onClick
This is the name of the method in this View's context to invoke when the view is clicked.
Activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Single Radio Buttons"
android:gravity="center"
android:id="@+id/text1"
android:layout_marginTop="40dp"
android:textSize="20dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:layout_marginTop="70dp"
android:id="@+id/rb1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:layout_marginTop="20dp"
android:id="@+id/rb2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/text2"
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/rg1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_marginTop="20dp"
android:id="@+id/rb3"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_marginTop="20dp"
android:id="@+id/rb4"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show selected"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="40dp"
android:textSize="20dp"
android:id="@+id/button1"
/>
</LinearLayout>
.java
package com.example.p12_1;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1=findViewById(R.id.rg1);
b1=findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intselectid = rg1.getCheckedRadioButtonId();
rb1= findViewById(selectid);
Toast.makeText(MainActivity.this, rb1.getText(), Toast.LENGTH_SHORT).show();
});
}
}
A CheckBox is an on/off switch that can be toggled by the user. You should use check-boxes
when presenting users with a group of selectable options that are not mutually exclusive.
android:autoText
1
If set, specifies that this TextView has a textual input method and automatically corrects some
common spelling errors.
android:drawableBottom
2
This is the drawable to be drawn below the text.
android:drawableRight
3
This is the drawable to be drawn to the right of the text.
android:editable
4
If set, specifies that this TextView has an input method.
android:text
5
This is the Text to display.
1 android:background
This is a drawable to use as the background.
android:id
3
This supplies an identifier name for this view.
android:onClick
4
This is the name of the method in this View's context to invoke when the view is clicked.
android:visibility
5
This controls the initial visibility of the view.
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza"
android:gravity="center"
android:textSize="20sp"
android:layout_marginLeft="150dp"
android:layout_marginTop="150dp"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger"
android:gravity="center"
android:layout_marginLeft="150dp"
android:layout_marginTop="50dp"
android:textSize="20sp" />
27 Unit No:04:Mobile Application Development Notes| P.S.Gaidhani
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffee"
android:textSize="20sp"
android:gravity="center"
android:layout_marginLeft="150dp"
android:layout_marginTop="50dp" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Submit"
android:layout_marginTop="70dp"
android:textSize="30sp"
android:textColor="#000000"/>
</LinearLayout>
.java file
package com.example.p11_1;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizza=findViewById(R.id.cb1);
burger=findViewById(R.id.cb2);
coffee=findViewById(R.id.cb3);
28 Unit No:04:Mobile Application Development Notes| P.S.Gaidhani
bu1=findViewById(R.id.b1);
bu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer res=new StringBuffer();
int sum=0;
res.append("Selected Item:-"+"\n");
if(pizza.isChecked())
{
res.append("Pizza Rs.100" +"\n");
sum=sum+100;
}
if (burger.isChecked())
{
res.append("Burger Rs.200"+"\n");
sum=sum+200;
}
if (coffee.isChecked())
{
res.append("Coffee Rs.300"+"\n");
sum=sum+300;
}
res.append("Total amount"+"\n"+sum+"Rs");
Toast.makeText(MainActivity.this, res.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
Output
1
getMax()
This method returns the maximum value of the progress.
2
incrementProgressBy(int diff)
This method increments the progress bar by the difference of value passed as a parameter.
3
setIndeterminate(boolean indeterminate)
This method sets the progress indicator as determinate or indeterminate.
4
setMax(int max)
This method sets the maximum value of the progress dialog.
5
setProgress(int value)
This method is used to update the progress dialog with some specific value.
6
show(Context context, CharSequence title, CharSequence message)
This is a static method, used to display progress dialog.
Example
Xml file
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/p1"/>
</LinearLayout>
Java file
package com.example.cuastomtoast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
}}
}}
Output
ListView Attributes
1
android:id
This is the ID which uniquely identifies the layout.
2
android:divider
This is drawable or color to draw between list items.
3
android:dividerHeight
This specifies height of the divider. This could be in px, dp, sp, in, or mm.
4
android:entries
Specifies the reference to an array resource that will populate the ListView.
5
android:footerDividersEnabled
6
android:headerDividersEnabled
When set to false, the ListView will not draw the divider after each header view. The default value
is true.
Program
<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"
tools:context=".ListActivity" >
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
package com.example.ListDisplay;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Grid View
34 Unit No:04:Mobile Application Development Notes| P.S.Gaidhani
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
GridView Attributes
1 android:id
This is the ID which uniquely identifies the layout.
android:columnWidth
2
This specifies the fixed width for each column. This could be in px, dp, sp, in, or mm.
android:gravity
3 Specifies the gravity within each cell. Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.
android:horizontalSpacing
4
Defines the default horizontal spacing between columns. This could be in px, dp, sp, in, or mm.
android:numColumns
5 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 −
Exapmle:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView 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:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
MainActivity.java File
package com.example.gridview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview=(GridView)findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java File
package com.example.gridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
public ImageAdapter(Context c)
{
mcontext=c;
}
@Override
public int getCount() {
return array.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView image;
if(view==null)
{
image=new ImageView(mcontext);
image.setLayoutParams(new GridView.LayoutParams(70,70));
image.setScaleType(ImageView.ScaleType.CENTER);
image.setPadding(8,8,8,8);
}
else
{
image=(ImageView) view;
}
image.setImageResource(array[i]);
return image;
}
Integer[]
array={R.drawable.ic_launcher_background,R.drawable.ic_launcher_foreground};
ImageView
1
copy(Bitmap.Config config, boolean isMutable)
This method copy this bitmap's pixels into the new bitmap
2
createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height
3
createBitmap(int width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height
4
createBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap
5
extractAlpha()
Returns a new bitmap that captures the alpha values of the original
6
getConfig()
This mehtod eturn that config, otherwise return null
7
getDensity()
Returns the density for this bitmap
8
getRowBytes()
Return the number of bytes between rows in the bitmap's pixels
10
setDensity(int density)
This method specifies the density for this bitmap
Example
Xml File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher_foreground" />
<ImageView
android:id="@+id/image2"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="@+id/image1"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image3"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="@+id/image2"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image4"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="@+id/image3"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/image5"
android:layout_width="fill_parent"
Java File
package com.example.imageviewexamle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "IMage1 Clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
ScrollView
Scroll View
Xml file
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Gayatri"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="Iqra"
android:layout_below="@id/b1"/>
<Button
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/b2"
android:text="Alfiya" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
41 Unit No:04:Mobile Application Development Notes| P.S.Gaidhani
android:id="@+id/b4"
android:text="Aqsa"
android:layout_below="@id/b3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b5"
android:text="Ishita"
android:layout_below="@id/b4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b6"
android:text="Madhu"
android:layout_below="@id/b5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b7"
android:text="Riya"
android:layout_below="@id/b6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b8"
android:text="Pratiksha"
android:layout_below="@id/b7"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b9"
android:text="Krutika"
android:layout_below="@id/b8"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b10"
android:text="Kanchan"
android:layout_below="@id/b9"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b11"
android:text="Tejal"
android:layout_below="@id/b10"/>
<Button
</RelativeLayout>
</ScrollView>
Output
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:id="@+id/showtoast"
/>
</LinearLayout>
Java file
package com.example.gridviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.showtoast);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater li=getLayoutInflater();
View
layout=li.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom_toast));
45 Unit No:04:Mobile Application Development Notes| P.S.Gaidhani
Toast toast=new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
}
output
Date Picker:
Android Date Picker allows you to select the date consisting of day, month and year in your
custom user interface. For this functionality android provides DatePicker and DatePickerDialog
components.
. You can use the following methods of the DatePicker to perform further operation.
1
getDayOfMonth()
This method gets the selected day of month
2
getMonth()
This method gets the selected month
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
5
setMinDate(long minDate)
This method sets the minimal date supported by this NumberPicker in milliseconds since January 1,
1970 00:00:00 in getDefault() time zone
6
setSpinnersShown(boolean shown)
This method sets whether the spinners are shown
8
getCalendarView()
This method returns calendar view
9
getFirstDayOfWeek()
This Method returns first day of the week
Date Picker:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<DatePicker
android:id="@+id/dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar"/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me" />
</LinearLayout>
package com.example.datetimepicker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
}
});
}
}
there are other methods in the API that gives more control over TimePicker Component. They
are listed below.
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
4
setCurrentMinute(Integer currentMinute)
This method sets the current 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