Unit II - Full
Unit II - Full
UNIT -2
Method Description
onResume called when activity will start interacting with the user.
onPause called when activity is not visible to the user.
1. package example.javatpoint.com.activitylifecycle;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.util.Log;
6.
7. public class MainActivity extends Activity {
8.
9. @Override
10. protected void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.activity_main);
13. Log.d("lifecycle","onCreate invoked");
14. }
15. @Override
16. protected void onStart() {
17. super.onStart();
18. Log.d("lifecycle","onStart invoked");
19. }
20. @Override
21. protected void onResume() {
22. super.onResume();
23. Log.d("lifecycle","onResume invoked");
24. }
25. @Override
26. protected void onPause() {
27. super.onPause();
28. Log.d("lifecycle","onPause invoked");
29. }
30. @Override
protected void onStop() {
31. super.onStop();
32. Log.d("lifecycle","onStop invoked");
33. }
34. @Override
35. protected void onRestart() {
36. super.onRestart();
37. Log.d("lifecycle","onRestart invoked");
38. }
39. @Override
40. protected void onDestroy() {
41. super.onDestroy();
42. Log.d("lifecycle","onDestroy invoked");
43. }
44.}
v7 Support Libraries :
There are several libraries designed to be used with Android 2.1 (API level
7) and higher. These libraries provide specific feature sets and can be
included in your application independently from each other.
v7 appcompat library :
This library adds support for the Action Bar user interface design
pattern. This library includes support for material design user
interface implementations.
Here are a few of the key classes included in the v7 appcompat
library:
● ActionBar - Provides an implementation of the action bar user
interface pattern. For more information on using the Action Bar,
see the Action Bar developer guide.
● AppCompatActivity - Adds an application activity class that can be
used as a base class for activities that use the Support Library
action bar implementation.
● AppCompatDialog - Adds a dialog class that can be used as a base
class for AppCompat themed dialogs.
● ShareActionProvider - Adds support for a standardized sharing
action (such as email or posting to social applications) that can be
included in an action bar.
v7 cardview library :
This library adds support for the CardView widget, which lets you show
information inside cards that have a consistent look on any app. These
cards are useful for material design implementations, and are used
extensively in layouts for TV apps.
v7 mediarouter library :
This library provides MediaRouter, MediaRouteProvider, and related media
classes that support Google Cast. In general, the APIs in the v7 mediarouter
library provide a means of controlling the routing of media channels and
streams from the current device to external screens, speakers, and other
destination devices.
v7 palette library :
The v7 palette support library includes the Palette class, which lets you
extract prominent colors from an image. For example, a music app could
use a Palette object to extract the major colors from an album cover, and
use those colors to build a color-coordinated song title card.
v7 recyclerview library :
The recyclerview library adds the RecyclerView class. This class provides
support for the RecyclerView widget, a view for efficiently displaying large
data sets by providing a limited window of data items.
v8 Support Library :
This library is designed to be used with Android 2.2 (API level 8) and
higher. This library provides specific feature sets and can be included in
your application independently from other libraries.
v8 renderscript library :
This library is designed to be used with Android (API level 8) and higher. It
adds support for the RenderScript computation framework. These APIs are
included in the android.support.v8.renderscript package.
v13 Support Library :
This library is designed to be used for Android 3.2 (API level 13) and
higher. It adds support for the Fragment user interface pattern with
the (FragmentCompat) class and additional fragment support classes.
For more information about fragments, see the Fragments developer
guide. For detailed information about the v13 Support Library APIs,
see the android.support.v13 package in the API reference.
ANDROID INTENT :
Android Intent is the message that is passed between components such as
activities, content providers, broadcast receivers, services etc.It is generally
used with startActivity() method to invoke activity. The dictionary
meaning of intent is intention or purpose. So, it can be described as the
intention to do action.
IMPLICIT INTENT:
Implicit Intent doesn't specifiy the component. In such case, intent
provides information of available components provided by the
system that is to be invoked.
EXAMPLE:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.javatpoint.com"));
startActivity(intent);
EXPLICIT INTENT:
Explicit Intent specifies the component. In such case, intent provides
the external class to be invoked.
EXAMPLE:
Intent i = new Intent(getApplicationContext(), ActivityT
wo.class);
startActivity(i);
ANDROID UI COMPONENTS:
A View is an object that draws something on the screen that the user can
interact with and a ViewGroup is an object that holds other View (and
ViewGroup) objects in order to define the layout of the user interface.
S.No. UI Control & Description
1 TextView :
This control is used to display text to the user.
2 EditText :
EditText is a predefined subclass of TextView that includes rich
editing capabilities.
3 AutoCompleteTextView :
The AutoCompleteTextView is a view that is similar to EditText,
except that it shows a list of completion suggestions
automatically while the user is typing.
4 Button :
A push-button that can be pressed, or clicked, by the user to
perform an action.
5 ImageButton :
An ImageButton is an AbsoluteLayout which enables you to
specify the exact location of its children. This shows a button
with an image (instead of text) that can be pressed or clicked by
the user.
6 CheckBox :
An on/off switch that can be toggled by the user. You should use
check box when presenting users with a group of selectable
options that are not mutually exclusive.
7 ToggleButton :
An on/off button with a light indicator.
8 RadioButton :
The RadioButton has two states: either checked or unchecked.
9 RadioGroup :
A RadioGroup is used to group together one or more
RadioButtons.
10 ProgressBar :
The ProgressBar view provides visual feedback about some
ongoing tasks, such as when you are performing a task in the
background.
11 Spinner :
A drop-down list that allows users to select one value from a set.
12 TimePicker :
The TimePicker view enables users to select a time of the day, in
either 24-hour mode or AM/PM mode.
13 DatePicker :
The DatePicker view enables users to select a date of the day.
Then finally create an instance of the Control object and capture it from the
layout,
FOR (I.E)
TextView myText = (TextView) findViewById(R.id.text_id);
VIEWS:
View is a basic building block of UI (User Interface) in android. A
view is a small rectangular box that responds to user inputs. Eg:
EditText, Button, CheckBox, etc.
ANDROID PICKERS :
Android provides controls for the user to pick a time or pick a date
as ready-to-use dialogs. Each picker provides controls for selecting
each part of the time (hour, minute, AM/PM) or date (month, day,
year).
For example, in a credit card form, a date picker would allow users to
enter or change the expiration date of their credit card.
ListView Attributes :
S.No Attribute & Description
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
When set to false, the ListView will not draw the divider before
each footer view. The default value is true.
6 android:headerDividersEnabled
When set to false, the ListView will not draw the divider after
each header view. The default value is true.
EXAMPLE :
ArrayAdapter adapter = new
ArrayAdapter<String>(this,R.layout.ListView,StringArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
ANDROID FRAGEMENTS :
A Fragment is a piece of an activity which enable more modular activity
design. It will not be wrong if we say, a fragment is a kind of sub-activity.
● A fragment has its own layout and its own behaviour with its own
life cycle callbacks.
● You can add or remove fragments in an activity while the activity is
running.
● You can combine multiple fragments in a single activity to build a
multi-pane UI.
● A fragment can be used in multiple activities.
Specialized Fragment :
The Fragments API provides other subclasses that encapsulate some
of the more common functionality found in applications. These
subclasses are:
ListFragment – This Fragment is used to display a list of items
bound to a datasource such as an array or a cursor.
The ListFragment is very similar in concept and functionality to
the ListActivity; it is a wrapper that hosts a ListView in a
Fragment.
DialogFragment – This Fragment is used as a wrapper around a
dialog. The Fragment will display the dialog on top of its Activity.
The DialogFragment is a Fragment that is used to display a
dialog object inside of a Fragment that will float on top of the
Activity's window.
PreferenceFragment – This Fragment is used to show
Preference objects as lists. To help manage preferences, the
Fragments API provides the PreferenceFragment subclass.
The PreferenceFragment is similar to the PreferenceActivity – it
will show a hierarchy of preferences to the user in a Fragment.
GALLERY VIEW :
In Android, Gallery is a view that can show items in a center-locked,
horizontal scrolling list, and hence the user can able to select a view, and
then the user-selected view will be shown in the center of the Horizontal
list. “N” number of items can be added by using the Adapter. The adapter is
a bridging component between the UI component and the data source.
android:src/
app:srcCompat To add the file path of the inserted image.
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageSwitcher>
Source code:
private ImageSwitcher imageSwitcher;
imageSwitcher =
(ImageSwitcher)findViewById(R.id.imageSwitcher1);
imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
return myView;
}
}
Android Grid View :
android:id
1
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
android:stretchMode
Defines how columns should stretch to fill the available empty
space, if any. This must be either of the values −
android:verticalSpacing
7 Defines the default vertical spacing between rows. This could be
in px, dp, sp, in, or mm.
Attribute Description
android:icon It is used to set the item's icon from the drawable folder.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.view.Menu;
import android.view.MenuItem; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override public boolean onCreateOptionsMenu(Menu menu)
{ getMenuInflater().inflate(R.menu.options_menu, menu);
return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item)
{ Toast.makeText(this, "Selected Item: " +item.getTitle(),
Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item: // do your code
return true;
R.id.upload_item: // do your code
return true;
case R.id.copy_item: // do your code
return true;
case R.id.print_item: // do your code
return true;
case R.id.share_item: // do your code
return true;
case R.id.bookmark_item: // do your code
return true;
default:
return super.onOptionsItemSelected(item); }
}}
ANDROID CONTEXT MENU :
The android.widget.AnalogClock and android.widget.DigitalCloc
k classes provides the functionality to display analog and digital
clocks.Android analog and digital clocks are used to show time in
android application.
XML CODE:
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="136dp"
android:layout_marginTop="296dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/analogClock1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="176dp"
android:layout_marginTop="84dp"
android:text="DigitalClock"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
JAVA CODE:
1. package example.javatpoint.com.analogdigital;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5.
6. public class MainActivity extends AppCompatActivity {
7.
8. @Override
9. protected void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.activity_main);
12. }
13. }
WEB VIEW:
Example:
1. WebView mywebview = (WebView) findViewById(R.id.webView1);
2. mywebview.getSettings().setJavaScriptEnabled(true);
3. mywebview.loadUrl("http://www.github.com/");
RECYCLER VIEW :
● RecyclerView is the ViewGroup that contains the views
corresponding to your data. It's a view itself, so you
add RecyclerView into your layout the way you would add any other
UI element.
● Each individual element in the list is defined by a view holder object.
When the view holder is created, it doesn't have any data associated
with it. After the view holder is created, the RecyclerView binds it to
its data. You define the view holder by
extending RecyclerView.ViewHolder.
● The RecyclerView requests those views, and binds the views to their
data, by calling methods in the adapter. You define the adapter by
extending RecyclerView.Adapter.