You are on page 1of 60

Introduction to Android APP

Development
CS 306: ICT Workshop - II
Android Introduction

• Android is an open-source operating system based on Linux with


a Java programming interface for mobile devices such as
Smartphone (Touch Screen Devices who supports Android OS)
as well for Tablets too.

• Android was developed by the Open Handset Alliance (OHA),


which is led by Google. The Open Handset Alliance (OHA) is a
consortium of multiple companies like Samsung, Sony, Intel and
many more to provide services and deploy handsets using the
android platform.
Features of Android

∙ Storage: Uses SQLite, a light weight relational database storage for data storage (really helpful when limited
mobile memory storage is to be considered).
∙ Media Support: Include support for large number of media formats for Images, Audio as well as for Video, like:
H.263, H.264, MPEG-4 SP, AMR, AMR WB, AAC, MP3, MIDI, WAV, JPEG, PNG, GIF & BMP.
∙ Messaging: Both SMS and MMS are supported.
∙ Web Browser: Based on Open Source WebKit, now known as Chrome.
∙ Connectivity: Supports large group of networks like: GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth,
WiFi, LTE and WiMAX.
∙ Hardware Support: Accelerometer Sensor, Camera, Digital Compass, Proximity Sensor & GPS and a lot more.
∙ Multi-Touch: Supports multi-touch screen.
∙ Multi-Task: Supports application multi-tasking.
∙ Flash Support: Supports Flash.
∙ Tethering: Supports sharing of Internet as wired or wireless hotspots.
Android Version
Android Architecture

The main components


• Applications
• Application Framework
• Android Runtime
• Platform Libraries
• Linux Kernel
Android Development Environment Setup

• We can set up an Android development environment using the


following two ways
• Setup Eclipse IDE Manually (Depreciated)
• Android Studio
• Download Android Studio
• Click on link and download android studio
Android Studio Installation for Development
Environment
Android Studio Installation for Development
Environment
Android Studio Installation for Development
Environment
Android Studio Installation for Development
Environment
Android Studio Installation for Development
Environment
Android Studio Installation for Development
Environment
Hello World App Example
Hello World App Example
Hello World App Example
Hello World App Example
Run Android Hello World App
Run Android Hello World App

Create Android Virtual Device


Run Android Hello World App
Create Android Virtual Device
Run Android Hello World App
Android Activity Lifecycle Diagram
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
}

protected void onStart() {


super.onStart();
Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show();
}

protected void onRestart() {


super.onRestart();
Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show();
}

protected void onPause() {


super.onPause();
Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show();
}

protected void onResume() {


super.onResume();
Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show();
}

protected void onStop() {


super.onStop();
Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show();
}

protected void onDestroy() {


super.onDestroy();
Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show();
}
}
Android Activity
• Now let’s see the situations where the life cycle methods and states will occur.
• When you open the app it will go through below states:
onCreate() –> onStart() –> onResume()
• When you press the back button and exit the app
onPaused() — > onStop() –> onDestory()
• When you press the home button
onPaused() –> onStop()
• After pressing the home button, again when you open the app from a recent task list
onRestart() –> onStart() –> onResume()
• After dismissing the dialog or back button from the dialog
onResume()
• If a phone is ringing and user is using the app
onPause() –> onResume()
• After the call ends
onResume()
• When your phone screen is off
onPaused() –> onStop()
• When your phone screen is turned back on
onRestart() –> onStart() –> onResume()
Android UI Layouts
• Layout is used to define the user interface for an app or activity and it
will hold the UI elements that will appear to the user.
• The user interface in the android app is made with a collection
of View and ViewGroup objects
• View - The View is a base class for all UI components in android and it is used
to create an interactive UI components such as Text View, Edit Text,
Checkbox, Radio Button, etc. and it responsible for event handling and
drawing.
• ViewGroup - The ViewGroup is a subclass of View and it will act as a base
class for layouts and layouts parameters. The ViewGroup will provide an
invisible containers to hold other Views or ViewGroups and to define the
layout properties.
Android Layout Types
• Linear Layout
• LinearLayout is a ViewGroup subclass which is used to render all child View instances one by one either in a
horizontal direction or vertical direction based on the orientation property.
• Relative Layout
• RelativeLayout is a ViewGroup subclass which is used to specify the position of child View instances relative
to each other (Child A to the left of Child B) or relative to the parent (Aligned to the top of a parent).
• Frame Layout
• FrameLayout is a ViewGroup subclass which is used to specify the position of View instances it contains on
the top of each other to display only a single View inside the FrameLayout.
• Table Layout
• TableLayout is a ViewGroup subclass which is used to display the child View elements in rows and columns.
• Web View
• WebView is a browser that is used to display the web pages as a part of our activity layout.
• List View
• ListView is a ViewGroup which is used to display scrollable single column list of items.
• Grid View
• GridView is a ViewGroup which is used to display items in a scrollable grid of columns and rows.
LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Add Child Views Here -->
</LinearLayout>

Or

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation=“horizontal" >
<!-- Add Child Views Here -->
</LinearLayout>
RelativeLayout
<?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"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Button1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Button2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Button3" />
</RelativeLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

TableLayout android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"

• android:layout_height="wrap_content"
android:layout_weight="1"
android:text="UserId" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Location" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Suresh Dasari" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hyderabad" />
</TableRow>
</TableLayout>
FrameLayout <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imgvw1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/flimg" />
<TextView
android:id="@+id/txtvw1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="#4C374A"
android:padding="10dp"
android:text="Grand Palace, Bangkok"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/txtvw2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:background="#AA000000"
android:padding="10dp"
android:text="21/Aug/2017"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</FrameLayout>
ListView
<?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/userlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


private ListView mListView;
private ArrayAdapter aAdapter;
private String[] users = { "Suresh Dasari", "Rohini Alavala", "Trishika Dasari", "Praveen Alavala", "Madav Sai", "Hamsika
Yemineni"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.userlist);
aAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, users);
mListView.setAdapter(aAdapter);
}
}
GridView
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="110dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
GridView gridView;
static final String[] numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gridView = (GridView) findViewById(R.id.gridView);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,


android.R.layout.simple_list_item_1, numbers);

gridView.setAdapter(adapter);
}
}
WebView <?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.webview);
String customHtml = "<html><body><h1>Welcome to iiit-Surat</h1>" +
"<h2>Welcome to iiit-Surat</h2><h3>Welcome to iiit-Surat</h3>" +
"<p>It's a Static Web HTML Content.</p>" +
"</body></html>";
wv.loadData(customHtml, "text/html", "UTF-8");
}
}
UI Controls

• In android UI or input controls are the interactive or View


components that are used to design the user interface of an
application.
• In android we have a wide variety of UI or input controls available,
those are TextView, EditText, Buttons, Checkbox, Progressbar,
Spinners, etc.
Different Types of UI Controls
• TextView – is a user interface control that is used to display the text to the user.
• EditText - is a user interface control which is used to allow the user to enter or modify the text.
• AutoCompleteTextView - is an editable text view which is used to show the list of suggestions based on the user typing text
• Button- is a user interface control that is used to perform an action when the user clicks or tap on it.
• ImageButton –is a user interface control that is used to display a button with an image to perform an action when the user clicks or tap on it.
• ToggleButton - is a user interface control that is used to display ON (Checked) or OFF (Unchecked) states as a button with a light indicator.
• CheckBox - is a two-states button that can be either checked or unchecked.
• RadioButton - is a two-states button that can be either checked or unchecked and it cannot be unchecked once it is checked.
• RadioGroup - is used to group one or more radio buttons into separate groups based on our requirements.
• ProgressBar - is a user interface control which is used to indicate the progress of an operation.
• Spinner - is a drop-down list which allows a user to select one value from the list.
• TimePicker - is a widget for selecting the time of day, either in 24-hour or AM/PM mode.
• DatePicker - is a widget for selecting a date.
• SeekBar - is an extension of ProgressBar control with a draggable thumb
• AlertDialog - is a small window that prompt messages to the user to make a decision or enter additional details.
• Switch - is a two-state user interface element that is used to display ON (Checked) or OFF (Unchecked) states as a button with thumb slider
• RatingBar - is used to get the rating from the user
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Welcome to Tutlane"
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:textAllCaps="true" />
<TextView
android:id="@+id/textView3"
public android:layout_width="wrap_content"
class MainActivity extends AppCompatActivity { android:layout_height="wrap_content"
@Override android:text="Welcome to Tutlane"
protected void onCreate(Bundle android:textStyle="bold"
savedInstanceState) { android:textColor="#fff"
super.onCreate(savedInstanceState); android:background="#7F3AB5"
setContentView(R.layout.activity_main); android:layout_marginBottom="15dp"/>
TextView tv = <TextView
(TextView)findViewById(R.id.textView2); android:id="@+id/textView4"
tv.setText("Welcome to Tutlane"); android:layout_width="wrap_content"
} android:layout_height="wrap_content"
} android:autoLink="email|web"
android:text="For more details visit http://tutlane.com and send mail to support@tutlane.com" />
</LinearLayout>
EditText <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<EditText
android:id="@+id/txtPhone"
android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:paddingLeft="40dp" android:ems="10"
android:orientation="vertical" android:id="@+id/linearlayout" > android:hint="Phone Number"
<EditText android:inputType="phone"
android:id="@+id/txtName" android:textColorHint="#FE8DAB"/>
android:layout_width="wrap_content"
</LinearLayout>
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Name"
android:inputType="text"
android:selectAllOnFocus="true" />
<EditText
android:id="@+id/txtPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password 0 to 9"
android:inputType="numberPassword" />
<EditText
android:id="@+id/txtEmai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:ems="10"
android:hint="Date"
android:inputType="date" />
AutoCompleteTextView
<?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/autoComplete_Country"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

String[] Countries = { "India", "USA", "Australia", "UK", "Italy", "Ireland", "Africa" };


ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, Countries);
AutoCompleteTextView actv =
AutoCompleteTextView)findViewById(R.id.autoComplete_Country);
actv.setAdapter(adapter);
Button
<LinearLayout xmlns:android="http://schemas.androi
d.com/apk/res/android"
android:orientation="vertical" android:layout_widt Button btnAdd = (Button)findViewById(R.id.addBtn);
h="match_parent" btnAdd.setOnClickListener(new View.OnClickListener() {
android:layout_height="match_parent"> public void onClick(View v) {
<Button // Do something in response to button click
android:id="@+id/addBtn" }
android:layout_width="wrap_content" });
android:layout_height="wrap_content" }
android:text="Add“
android:onClick="addOperation" />
</LinearLayout>
Or
LinearLayout layout =
(LinearLayout)findViewById(R.id.l_layout);
Button btn = new Button(this);
btn.setText("Test");
layout.addView(btn);
ImageButton
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.andr ImageButton btnAdd =
oid.com/apk/res/android" (ImageButton)findViewById(R.id.addBtn);
android:orientation="vertical" android:layout_wi btnAdd.setOnClickListener(new View.OnClickListener() {
dth="match_parent" public void onClick(View v) {
android:layout_height="match_parent"> // Do something in response to button click
<ImageButton }
android:id="@+id/addBtn" });
android:layout_width="wrap_content" }
android:layout_height="wrap_content"
android:src="@drawable/add_icon"
android:onClick="addOperation"/>
</LinearLayout>
Or
LinearLayout layout =
(LinearLayout)findViewById(R.id.l_layout);
ImageButton btn = new ImageButton(this);
btn.setImageResource(R.drawable.add_icon);
layout.addView(btn);
Toggle Button

<?xml version="1.0" encoding="utf-8"?> ToggleButton toggle = (ToggleButton)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
findViewById(R.id.togglebutton);
android:layout_width="match_parent" android:layout_height="matc toggle.setOnCheckedChangeListener(new CompoundButton.O
h_parent"> nCheckedChangeListener() {
<ToggleButton public void onCheckedChanged(CompoundButton
android:id="@+id/toggle1"
android:layout_width="wrap_content" buttonView, boolean isChecked) {
android:layout_height="wrap_content" if (isChecked) {
android:layout_marginLeft="100dp" // The toggle is enabled
android:layout_marginTop="120dp"
android:checked="true"
} else {
android:textOff="OFF" // The toggle is disabled
android:textOn="ON"/> }
</RelativeLayout> }
Or
RelativeLayout layout = (RelativeLayout)findViewById(R.id.r_layout); });
ToggleButton tb = new ToggleButton(this);
tb.setTextOff("OFF");
tb.setTextOn("ON");
tb.setChecked(true);
layout.addView(tb);
CheckBox

<?xml version="1.0" encoding="utf-8"?> CheckBox chk = (CheckBox)


<RelativeLayout xmlns:android="http://schemas.android. findViewById(R.id.chk1);
com/apk/res/android" chk.setOnClickListener(new View.OnClickListener()
android:layout_width="match_parent" android:layout_
{
height="match_parent">
<CheckBox @Override
android:id="@+id/chk1" public void onClick(View v) {
android:layout_width="wrap_content" boolean checked = ((CheckBox) v).isChecked();
android:layout_height="wrap_content" // Check which checkbox was clicked
android:checked="true" if (checked){
android:text="Java" // Do your coding
}
android:onClick="onCheckBoxClick"/></RelativeLayout>
else{
Or
LinearLayout layout = // Do your coding
(LinearLayout)findViewById(R.id.l_layout); }
CheckBox cb = new CheckBox(this); }
cb.setText("Tutlane"); });
cb.setChecked(true);
layout.addView(cb);
RadioButton

<?xml version="1.0" encoding="utf-8"?> RadioButton rdb = (RadioButton)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res
/android"
findViewById(R.id.radiobutton1);
android:layout_width="match_parent" android:layout_height="mat rdb.setOnClickListener(new View.OnClickListener() {
ch_parent"> @Override
<RadioGroup public void onClick(View v) {
android:layout_width="match_parent"
android:layout_height="wrap_content" boolean checked = ((RadioButton) v).isChecked();
android:orientation="vertical"> // Check which radiobutton was pressed
<RadioButton if (checked){
android:layout_width="wrap_content"
android:layout_height="wrap_content"
// Do your coding
android:text="Java" }
android:onClick="onRadioButtonClicked"/> else{
</RelativeLayout> // Do your coding
Or
LinearLayout layout = (LinearLayout)findViewById(R.id.l_layout); }
RadioButton rd = new RadioButton(this); }
rd.setText("Tutlane"); });
rd.setChecked(true);
layout.addView(rd);
ProgressBar

Determinate Mode Indeterminate Mode

<ProgressBar <ProgressBar
android:id="@+id/pBar" android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizonta style="?android:attr/progressBarStyleHorizontal"
l" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:indeterminate="true"/>
android:max="100"
android:progress="50" />
Spinner (Dropdown List)

<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

String[] users = { "Suresh Dasari", "Trishika Dasari", "Rohini Alavala", "Praveen Kumar", "Madhav Sai" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
DatePicker

<DatePicker android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

DatePicker with Calendar Mode


<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar"/>

DatePicker with Spinner Mode


<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>
TimePicker

<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

TimePicker with Clock Mode


<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="clock" />

TimePicker with Spinner Mode


<TimePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
SeekBar

<SeekBar android:id="@+id/seekBar1"
android:layout_width=" wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:indeterminate="false"
android:progress="50" />

SeekBar with Determinate Mode


<SeekBar
android:id="@+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:max="100"
android:progress="50" />

SeekBar with Indeterminate Mode


<SeekBar android:id="@+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:max="100"
android:indeterminate="true"
android:progress="0" />
RatingBar
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="3.5"/>

RatingBar Value

int noofstars = rBar.getNumStars();


float getrating = rBar.getRating();
tView.setText("Rating: "+getrating+"/"+noofstars);
Switch (ON / OFF) Button
<?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">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:text="Switch1:"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
</RelativeLayout>

Switch sw = (Switch) findViewById(R.id.switch1);


sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
AlertDialog
<?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">
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="200dp"
android:text="Show Alert" />
</RelativeLayout>

Button btn = (Button)findViewById(R.id.getBtn);


btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Login Alert")
.setMessage("Are you sure, you want to continue ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Selected Option: YES",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Selected Option: No",Toast.LENGTH_SHORT).show();
}
});
//Creating dialog box
AlertDialog dialog = builder.create();
dialog.show();
}
});
Menus (Options, Context, Popup)
• Menu is a part of the user interface (UI) component which is used to
handle some common functionality around the application. By using
Menus in our applications, we can provide better and consistent user
experience throughout the application.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.an
droid.com/apk/res/android"> public void onCreateContextMenu(ContextMenu menu,
<item android:id="@+id/mail" View v, ContextMenuInfo menuInfo) {
android:icon="@drawable/ic_mail"
super.onCreateContextMenu(menu, v, menuInfo);
android:title="@string/mail" />
<item android:id="@+id/upload" MenuInflater inflater = getMenuInflater();
android:icon="@drawable/ic_upload" inflater.inflate(R.menu.menu_example, menu);
android:title="@string/upload" }
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
Options Menu
• Options Menu is a primary collection of menu items for an activity
and it is useful to implement actions that have a global impact on the
app, such as Settings, Search, etc.
<?xml version="1.0" encoding="utf-8"?> public void onCreateOptionsMenu(ContextMenu
<menu xmlns:android="http://schemas.an menu, View v, ContextMenuInfo menuInfo) {
droid.com/apk/res/android"> super.onCreateContextMenu(menu, v,
<item android:id="@+id/mail" menuInfo);
android:icon="@drawable/ic_mail" MenuInflater inflater = getMenuInflater();
android:title="@string/mail" /> inflater.inflate(R.menu.menu_example, menu);
<item android:id="@+id/upload" }
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
Context Menu

• Context Menu is like a floating menu and that appears when the user
performs a long press or click on an element and it is useful to
implement actions that affect the selected content or context frame.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
registerForContextMenu(btn);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Upload");
menu.add(0, v.getId(), 0, "Search");
}
Popup Menu

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


<menu xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
> android:layout_width="match_parent"
<item android:id="@+id/mail" android:layout_height="match_parent"
android:icon="@drawable/ic_mail" android:orientation="vertical" >
android:title="@string/mail" />
<item android:id="@+id/upload" <Button
android:icon="@drawable/ic_upload" android:id="@+id/btnShow"
android:title="@string/upload" android:layout_width="wrap_content"
android:showAsAction="ifRoom" /> android:layout_height="wrap_content"
<item android:id="@+id/share" android:text="Show Popup Menu"
android:icon="@drawable/ic_share" android:layout_marginTop="200dp" android:layout_marginLeft="100dp"/
android:title="@string/share" /> >
</menu> </LinearLayout>

Button btn = (Button) findViewById(R.id.btnShow);


btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
popup.inflate(R.menu.popup_menu);
popup.show();
}
});
Toast

• Toast.makeText(context, "message", duration).show();


Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT).show();

• We have two ways to define the Toast duration, either in LENGTH_SHORT or LENGTH_LONG to
display the toast notification for a short or longer period of time.

• Change the Position of Android Toast Notification


Toast toast = Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 100, 250);
toast.show();
Custom Toast

<?xml version="1.0" encoding="utf-8"?> Button btn = (Button)findViewById(R.id.btnShow);


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" btn.setOnClickListener(new View.OnClickListener() {
android:id="@+id/custom_toast_container" @Override
android:orientation="horizontal"
android:layout_width="match_parent"
public void onClick(View v) {
android:layout_height="match_parent" LayoutInflater inflater = getLayoutInflater();
android:paddingLeft="10dp" View layout = inflater.inflate(R.layout.custom_toast,
android:paddingRight="10dp" (ViewGroup) findViewById(R.id.custom_toast_layout));
android:background="#80CC28"> TextView tv = (TextView) layout.findViewById(R.id.txtvw);
<ImageView android:src="@drawable/ic_notification" tv.setText("Custom Toast Notification");
android:layout_width="wrap_content" Toast toast = new Toast(getApplicationContext());
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 100);
<TextView android:id="@+id/txtvw" toast.setDuration(Toast.LENGTH_LONG);
android:layout_width="wrap_content" toast.setView(layout);
android:layout_height="wrap_content" toast.show();
android:layout_marginTop="13dp" }
android:textColor="#FFF" });
android:textStyle="bold"
android:textSize="15dp" />
</LinearLayout>
Intents (Implicit, Explicit)
• Intent is a messaging object which is used to request an action from
another app component such as activities, services, broadcast
receivers, and content providers.
• Intents will help us to maintain the communication between app
components from the same application as well as with the
components of other applications.
• two types of intents available in android
Implicit Intents
• Implicit Intents won’t specify any name of the component to start
instead, it declare an action to perform and it allows a component
from other apps to handle it.
<?xml version="1.0" encoding="utf-8"?> final EditText editText =
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" (EditText)findViewById(R.id.urlText);
xmlns:tools="http://schemas.android.com/tools" Button btn = (Button) findViewById(R.id.btnNavigate);
android:layout_width="match_parent"
android:layout_height="match_parent" btn.setOnClickListener(new View.OnClickListener() {
tools:context="com.tutlane.intents.MainActivity"> @Override
<EditText
android:layout_width="wrap_content" public void onClick(View v) {
android:layout_height="wrap_content" String url = editText.getText().toString();
android:id="@+id/urlText"
android:layout_alignParentTop="true" Intent intent = new Intent(Intent.ACTION_VIEW,
android:layout_centerHorizontal="true" Uri.parse(url));
android:layout_marginTop="100dp"
android:ems="10" /> startActivity(intent);
<Button }
android:layout_width="wrap_content"
android:layout_height="wrap_content" });
android:id="@+id/btnNavigate" }
android:layout_below="@+id/urlText"
android:text="Navigate"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Explicit Intents
• Explicit intents explicitly specify the name of the component to be invoked by activity and we use
explicit intents to start a component in our own app.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/fstTxt" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="match_parent"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:layout_height="match_parent">
android:text="First Number" <TextView
/>
<EditText android:layout_width="wrap_content"
android:id="@+id/firstNum" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/resultView"
android:layout_marginLeft="100dp"
android:ems="10">
android:layout_marginLeft="100dp"
</EditText> android:layout_marginTop="150dp"/>
<TextView
android:id="@+id/secTxt" </LinearLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Number"
android:layout_marginLeft="100dp"
/>
<EditText
android:id="@+id/secondNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Add" />
</LinearLayout>
Explicit Intents
<?xml version="1.0" encoding="utf-8"?> final EditText firstNum = (EditText)findViewById(R.id.firstNum);
<manifest xmlns:android="http://schemas.android.com/apk/res/android" final EditText secNum = (EditText)findViewById(R.id.secondNum);
package="com.tutlane.intents"> Button btnAdd = (Button)findViewById(R.id.addBtn);
<application btnAdd.setOnClickListener(new View.OnClickListener() {
android:allowBackup="true" @Override
android:icon="@mipmap/ic_launcher" public void onClick(View v) {
android:label="Explicit Intent - Activity1" int num1 = Integer.parseInt(firstNum.getText().toString());
android:roundIcon="@mipmap/ic_launcher_round" int num2 = Integer.parseInt(secNum.getText().toString());
android:supportsRtl="true" Intent intent = new Intent(MainActivity.this,ResultActivity.class);
android:theme="@style/AppTheme"> intent.putExtra("SUM",num1+" + "+num2+" = "+(num1+num2));
<activity android:name=".MainActivity"> startActivity(intent);
<intent-filter> }
<action android:name="android.intent.action.MAIN" /> });
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> TextView result = (TextView)findViewById(R.id.resultView);
</activity> Intent intent = getIntent();
<activity android:name=".ResultActivity" android:label="Explicit Intent - String addition = (String)intent.getSerializableExtra("SUM");
Activity2">
result.setText(addition);
</activity>
</application>
</manifest>

You might also like