ANDROID - UI - Material by MR Mahesh at DurgaSoft
ANDROID - UI - Material by MR Mahesh at DurgaSoft
ANDROID - UI - Material by MR Mahesh at DurgaSoft
ANDROID
What is Android?
Android Architecture :
1. Android Application
2. Application Framework
5. Kernel – Linux
2. Application FrameWork:
This is the skeleton or framework which all android developers has to follow. The
developers can access all framework APIs an manage phone’s basic functions like resource
allocation, switching between processes or programs, telephone applications, and keeping track of
the phone’s physical location.
3. Libraries:
a. Core Libraries: The Android runtime layer which includes set of core java libraries and
DVM (Dalvik Virtual Machine) is also located in same layer.
b. Native Libraries: This layer consists of Android libraries written in C, C++, and used by
various system. These libraries tells the device how to handle different kinds of data and are
exposed to Android developers via Android Application framework. Some of these libraries
includes media , graphics, 3d,SQLite,web browser library etc.
This layer includes set of base libraries that are required for java libraries. Every Android
application gets its own instance of Dalvik virtual machine. Dalvik has been written so that a device
can run multiple VMs efficiently and it executes files in executable (.Dex) optimized for minimum
memory.
5. Kernel – Linux:
This layer includes Android’s memory management programs, security settings, power
management software and several drivers for hardware , file system access, networking and inter-
process-communication. The kernel also acts as an abstraction layer between hardware and the
rest of the software stack.
First Major
Android version
1.0 (API level 1) of Android is 1.5 (CupCake)
may 2009
Android 2.3.3–2.3.7 Gingerbread (API level 10)
Android
Android 1.11.6
(API(Donut)
level 2) Android 3.0 Honeycomb (API level 11)
Android
Android 1.52.1(Éclair)
Cupcake (API level 3) Android 3.1 Honeycomb (API level 12)
Android
Android 1.62.2(Froyo)
Donut (API level 4) Android 3.2 Honeycomb (API level 13)
Android
Android 2.02.3,2.3.2,2.3.3,….(GingarBread)
Eclair (API level 5) Android 4.0–4.0.2 Ice Cream Sandwich (API level 14)
Android
Android 2.0.1 Eclair (API level 6)
3.1,3.2,3.2.1,3.2.2,3.2.3….(HoneyComb) Android 4.0.3–4.0.4 Ice Cream Sandwich (API level 15)
Android
Android 2.14.0(ice Cream
Eclair (API level 7)Sandwitch) Android 4.1 Jelly Bean (API level 16)
Android 2.2–2.2.3 Froyo (API level 8) Android 4.2 Jelly Bean (API level 17)
Android 2.3–2.3.2 Gingerbread (API level 9) Android 4.3 Jelly Bean (API level 18)
Android 2.3.3–2.3.7 Gingerbread (API level 10) Android 4.4 KitKat (API level 19)
Android
2.12Features:
Android 3.1 Honeycomb (API level 12)
Application
level 14) framework enabling reuse and replacement of components
2.15 Android 4.0.3–4.0.4 Ice Cream Sandwich (API
Dalvik virtual machine optimized for mobile devices
level 15)
2.16
Optimized graphics
Android 4.1 Jelly Bean (API level 16)
2.19
Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC,
Android 4.4 KitKat (API level 19)
To Create , Test and build Android applications we need the following Softwares:
1. Eclipse IDE (is optional can use any other tool. But Eclipse is recommended ) + added with
Android Development plugin
The android app can be developed on any OS such as Windows, Linux, and MAC Os
The following are the resources that are required to set up into the system for android
application development
1. An IDE (Integrated Development Environment) : The Eclipse is the most prefered IDE. We
can download the Eclipse IDE from
The Android Development Tool(ADT) is the Eclipse plugin doing this. For this we want to
use the Install plugin option of the Eclipse IDE.
Alternatively we can get the ADT zip file downloaded from http://www.android.com and
manually copy the features and plugins folders into the eclipse root folder , which we got
extracting the ADT zip file.
3. In addition to the IDE and its plugin simplifying creating the android applications we want
to have an android SDK for building, and testing the android applications, we can download
the SDK from http://www.android.com After extracting the downloaded zip file for the SDK
we need to run the ‘SDK Manager’ whicj is found in the <android-sdk-home> this find &
install various available packages.
1. Activity
2. Service
3. BroadCast Receiver
The Following are the possible methods that we can override the Activity Object.
The onCreate() :
The onCreate() methos is used to implement initialization code like presenting the View for
the Activity.
The onStart():
The onStart() method allows writing the code executing on getting the activity foreground
The onRestart() :
The onRestart() method can initialize the resources that are closed in the onStop().
The onStop() :
The onStop() method can use the resources that are not required on paused activity. This
may even required to solve the start of the activity.
The onResume() :
The onResume() can create the resources and / or get the saved state of the activity to get
into foreground.
The onDestroy() : The onDestroy() can perform the finilizations for the activity.
Or
Creating an Activity:
onCreate()
onStart()
onResume()
At this stage ‘onPause()’ of this activity is invoked and then the other activity is
started. In case of the other activity is taking the complete screen the ‘onScreen()’ is invoked
getting the activity to background(stopped). Otherwise it gets paused.
This activity which is paused or stopped because of some other activity is foreground, is
now coming foreground because of the existing foreground activity is destroyed.
At this stage the ‘onResume()’ is the only method invoked in case of getting from paused.
onRestart()
onStart()
onResume()
The Activity while destroying from foreground invokes the following methods.
onPause()
onStop()
onDestroy()
Point to Note : The android system can destroy the stopped activity without a user request, just
invoking ‘onDestroy()’
For the first time want to take the following points to check
src
A folder that holds the Java source code
gen
This is where Android's build tools will place source code that they generate.
R.java" is a generated class which contains the text and the UI elements.
Android projects come with this free class 'R' which is short for Resource.
This is an automatically generated class that we can use to reference
resources in our project. We should not try to modify this class manually.
assets
A folder that holds other static files you wish packaged with the application
for deployment onto the device.
bin
In Eclipse, the ADT plugin incrementally builds our project as we make
changes to the source code. Eclipse outputs an .apk file automatically to the
bin folder of the project, so we do not have to do anything extra to generate
the .apk.
res
A folder that holds resources such as icons, graphic user interface (GUI)
layouts, etc. that are packaged with the compiled Java in the application.
AndroidMainFest.xml
An XML file describing the application being built and which components
(activities, services, and so on) are being supplied by the application. This file
is the foundation for any Android application. This is where you declare what
is inside your application. You also indicate how these pieces attach
themselves to the overall Android systems; for example, you can indicate
which activity or activities should appear on the device's main menu (a.k.a.
the launcher).
When you create your application, a starter manifest will be generated for
you automatically. For a simple application, offering a single activity and
nothing else, the auto generated manifest will probably work out fine, or
perhaps require a few minor modifications. But on the other spectrum, the
manifest file for the Android API demo suite is more that 1,000 lines long.
Your production applications will probably falls somewhere in the middle.
Most of the manifest will be describes detail in later chapters.
project.properties
By Mr. Mahesh Durga Soft Solutions , SR Nagar , Hyderabad
Property files used by. the
www.durgasoft.com
Ant build script. 9
Creating an Activity:
The first allows to set the View Object may be created in the program here. And the
later allows setting the view defined in the XML document.
The FirstActivity.java :
package com.durgasoft.android;
import com.durgasoft.android.R;
import ………….;
/** Called when the activity is first created. */ OUT PUT SCREEN
@Override
super.onCreate(savedInstanceState);
setContentView(tv);
*/
setContentView(R.layout.main);
The android application includes a generated resource file named ‘R’ which lists all the resources
defined with unique identity. And this xml View is also given with a unique id to refer in the
program. The id can be accessed using ‘R.layout.Main’.
Thus the activity can have this View set as shown below
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
It is found in most cases creating a complex view it is convenient to go for XML style
How ever we can get View created in xml into the activity and do necessary changes .
Example:
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.tv1);
Tv.setText(“New Text”); }
andrioid:id=”@+id/tv1”
The ‘+’ in the value of the attribute describes add the is resource into the ‘R’ file if
not available
The android application support multiple type of resource such as layouts, ids and
Strings
The String resource is can be added into ‘strings.xml’ file res/values folder
Eg:
<resource>
<String name=”text”> Text String Message </String>
</resource>
These resources are added into the R file for convenient access.
To access the resources in ‘xml’ documents we use the following syntax.
Syntax: @<type>/<name>
Eg: @String/test
R.<type>.<name>
We can use the following method for getting the string resource declared in the xml.
String s1=getResources().getString(R.String.test);
Similar to String we can have a Boolean resource .this is declared using <bool> tag.
Starting an Activity:
The activity with ACTION_MAIN, and CATEGORY_LAUNCHER is main activity to run
on accessing the application.
In addition we may want to start another activity from such main activity.
We use startActivity(Intent) method of the Activity to do this.
1. Explicit Intents
2. Implicit Intents
Explicit Intents:
super.onCreate(b);
setContentView(R.layout.main1.xml);
Lets consider the main.xml is the layout document and has the following tag.
<Button
………………………………….
Android:onClick=”myMethod”/>
The following method needs to added into the existence Activity
i.setComponentName(new ComponentName(getApplicationContext(),Activity2.class));
startActivity(i); }
<intent- filter>........................</>
Implicit Intent:
In Case if the Intent without a component is given , the f/w needs to use the
package manager conduct the following 3 tests to find the suitable activities
that can handle this intent.
o Action:
Here the intent action is match with the actions of the intent
filters of the activities.
If the intent doesn’t has an action(ies it is null) in such case
every activity with an intent filter atleast with one action will
passes this test.
Android includes multiple builtin actions such as ACTION_CALL
We can create our own action, It is recommended to use package
name as prefix for the action name. For example
com.durgasoft.android.GAME_START
o Data:
Once if the activity passes the action test then it is checked for
data test. Here the MIME type and / or path is matched to find is
the activity matching to handle the intent with the given data.
o Category:
The intent and intent filter can have multiple categories added.
All the categories describes in the intent should match the
intent filter categories passing this test.
Note:
Note:
Example
i.setAction(Intent.ACTION_DIAL);
startActivity(i);
This starts a phone dialer activity allowing us to dial the number and make a call.
i.setAction(Intent.ACTION_DIAL);
u.scheme(“tel”);
u.path(“1234567890”);
i.setData(u.build());
startActivity(i);
This starts the phone dialer activity with the number ‘1234567890’ entered
directly to make a call.
Alternative to ACTION_DIAL we can use ACTION_CALL for directly making a
call.
First Arg: Intent
Second Arg: int(specifies the request code, this is used diffentiate
between the multiple child activities that are possible to start from
the activity)
This code is used in ‘onActivityResult()’ method to find the result from which child
activity.
The child Activity can use the following method for sending the result.
Once the childActivity is finished then the following method of the parent activity is
invoked.
onActivityResult(int requestcode,int resultcode,Intent data)
Note:
In Case if the child activity finished without setting the result , the data will be null.
SecondActiivity.java:
package com.durgasoft.android;
import com.durgasoft.android.R;
import .................................................................;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Register the Activity2 class in the android manifest.xml file inside <application> tag
// <activity android:name=".Acitivity2"></activity>
i.setComponent(new ComponentName(getApplicationContext(),Activity2.class));
startActivity(i);
i.setAction(Intent.ACTION_DIAL);
startActivity(i);
Activity2.java
package com.durgasoft.android;
import --------------------------------------------;
super.onCreate(savedInstanceState);
setContentView(R.layout.main1); } }
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:onClick="MyMethod"
android:text="Explicit Intent"></Button>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:onClick="MyMethod1"
android:text="Implicit Intent" ></Button>
</LinearLayout>
Main1.xml
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is a Second Activity"
></TextView>
</LinearLayout>
</application>
</manifest>
setContentView(android.View.View)
setContentView(int ViewID)
It is recommended use XML style , which can simplify creating the View and allows
take the advantage of first design using click and drag supported by the IDE.
res/layouts folder
An XML document can describe only one View. Note the View here can be a
View group thus a tree of Views
Here we have an XML tag one for each concrete View type to use defining the
View.
Example:
<Button...../>
<TextView......../>
<EditView....../>
Example:
<!--Simple Button.xml-->
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=”Click Me Boss”
android:onClick=”Submit” />
To set this view as User Interface for the Activity we can have the following
code in the ‘onCreate()’ method of the activity.
setContentView(R.layout.simpleButton);
Note:
The android XML file names cannot have uppercase and special and white
space characters.
Example:
<!-- simple_linear_layout1.xml-->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A"></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B"></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C"></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="D"></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="E"></TextView>
</LinearLayout>
Is a subtype of LinearLayout
This supports arranging set of table row objects into the Layout. A Table Row
Object supports adding one or more Views including specifying column
index.
Example:
<!-- table_layout_View1.xml-->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="F"></TextView>
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="G"></TextView>
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="H" > </TextView>
</TableRow>
<TableRow >
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="I" ></TextView>
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="J" ></TextView>
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="K"></TextView>
</TableRow>
</TableLayout>
Example:
In onCreate() method:
tr.addView(tv1);
tr.addView(tv2);
tr.addView(tv3);
TableLayout tl=(TableLayout)findViewById(R.id.table1);
tl.addView(tr);
Note :
The column index starts with 0.
Note:
The column index starts with ‘0’
We can get the views and resource the views from the table
layout using the method as described below.
getChildAt(int rowindex)
removeView(View)
ThirdActivity.java:
package com.durgasoft.android;
import com.durgasoft.android.R;
import .........................................................................;
tr.addView(tv1);
tr.addView(tv2);
tr.addView(tv3);
TableLayout tl=(TableLayout)findViewById(R.id.table1);
tl.addView(tr);
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A" ></TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B" ></TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C" ></TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="D" ></TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="E" ></TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="---------------------Table Layout-------------------"
android:textStyle="bold" ></TextView>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<TextView android:layout_width="100px"
android:layout_height="wrap_content"
android:text="F" ></TextView>
<TextView android:layout_width="100px"
android:layout_height="wrap_content"
android:text="G" ></TextView>
<TextView android:layout_width="100px"
android:layout_height="wrap_content"
android:text="H" >
</TextView>
</TableRow>
<TextView android:layout_width="100px"
android:layout_height="wrap_content"
android:text="I" ></TextView>
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="J" ></TextView>
<TextView android:layout_width="100px"
android:layout_height="wrap_content"
android:text="K" >
</TextView>
</TableRow>
</TableLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="-----------------Table Row using Prog--------------"
android:textStyle="bold" ></TextView>
</TableRow>
</TableLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/table1" ></TableLayout>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me Boss"
android:onClick="Table" ></Button>
</LinearLayout>
In XML:
<Button android:text=”Submit”/>
Programatic:
In Case we use the following method of Button to set the listener for
onClick event.
setOnClickListener(View.onClickListener)
The View.OnClickListener is an interface that declares only one
method.
public void onClick(View v)
This method is invoked by the View component when a user clicks
the component.
We can create a class implementing this interface . most of the time
we prepare creating an anonymous class
Example:
Declarative :
This is simple ,here we want to use ‘android:onClick’ attribute
specifying the name of the method to listen for this event.
The method needs to be implemented in the content of this Buutton ,
with the following signature
public void <method-name> (View v)
In this case the components in the view can be placed with respect to the
other components in the view or with respect to this layout (ies parent)
view.
We can use the following attributes to configure the component align with
respect to the layout.
android:layout_alignParentBottom: specifies whether the component is
to be placed at the bottom of the layout. ‘true’ describes to place to the
Bottom.
android:layout_alignParentTop
android:layout_alignParentLeft
android:layout_alignParentRight
The following attributes can be used to place the component with respect to
the another components.
android:layout_below : specifies the id of the component below
which this component has to be arranged.
android:layout_toLeftOf
android:layout_toRightOf
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello This is from Relative Layout"
android:id="@+id/tv1" ></TextView>
<Button android:layout_below="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OnClick"
android:id="@+id/b1" > </Button>
<TextView android:layout_below="@+id/b1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the Activity from Relative Layout"
android:id="@+id/tv2" > </TextView>
<!-- Similarly we have
android:layout_toLeftOf="@+id/b1"
android:layout_toRightOf="@+id/b1"
android:layout_above="@+id/b1"-->
</RelativeLayout>
package com.durgasoft.android;
import com.durgasoft.android.R;
import ----------------------------------------------;
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
}
});
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello This is from Relative Layout"
android:id="@+id/tv1"
></TextView>
<TextView
android:layout_below="@+id/b1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the Activity from Relative Layout"
android:id="@+id/tv2"
>
</TextView>
<!-- Similarly we can have
android:layout_toLeftOf="@+id/b1"
android:layout_toRightOf="@+id/b1"
android:layout_above="@+id/b1"-->
</RelativeLayout>
The EditText:
This is a text field that allows taking the text content input from the
user.
<EditText............/>
Example:
<LinearLayout..................>
<Button android:onClick=”onClickB1”......./>
<EditText
android:layout_width="150px"
android:layout_height="wrap_content"
android:id="@+id/et1"></EditText>
</LinearLayout>
View ViewGroup
TextView LinearLayout
EditText RelativeLayout
Button TableLayout
ToggleButton FrameLayout
CheckBox ListView
RadioButton ScrollView
Spinner TableView
DatePicker RadioGroup
TimePicker ProgressBar
DigitalTime ImageButton
AnalogTime Image
The TextView autolink option ,this is used to have links automatically created for
the phone numbers , email ids and website address supporting us to click performing
necessary action. We use android:autolink attribute to configure this. This defaults to none,
and we can specify phone, email, web, map, or all.
The AutoCompleteTextView:
This is a subtype of EditView , this allows us get the help content
listing to choose the values from . we can use the following tag to
declare this component.
<AutoCompleteTextView………………../>
Example:
<AutoCompleteTextView
android:layout_width="150px"
android:layout_height="wrap_content"
android:id="@+id/act1" >
</AutoCompleteTextView>
In the Activity onCreate() method we can have the following code for
describing the autocomplete words.
String[] str=new
String[]{"SVU","OU","JNTUK","JNTUH","ANDHRA","SKU","YVU","VSU","PALAMURU","MAHA
TMA","OTHER"};
act.setThreshold(1);
act.setAdapter(aa);
<Toggle Button
android:layout_width="150px"
android:layout_height="wrap_content"
android:taxtOn=”yes”
android:textOff=”No”/>
The android:textOn specifies the text to display when this button Check
(ies on) and textOff for content to displaying on unchecked(ies off).
The CheckBox:
This is also a subtype of button allows us to take checked/unchecked
decisions from the user.
We use the following tag to declare this view.
<CheckBox
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="@+id/cb1"
android:text="MCA"
android:onClick="CheckBox"></CheckBox>
cb1=(CheckBox)findViewById(R.id.cb1);
cb2=(CheckBox)findViewById(R.id.cb2);
if(cb1.isChecked())
{
Toast.makeText(getApplicationContext(), "MCA is
Selectd",Toast.LENGTH_LONG).show();
}
else if(cb2.isChecked())
{
Toast.makeText(getApplicationContext(), "BTech is
Selectd",Toast.LENGTH_LONG).show();
}
}
<RadioGroup >
<RadioButton android:layout_width="70px"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:text="Male"
android:onClick="RadioBox" ></RadioButton>
<RadioButton android:layout_width="70px"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:text="Female"
android:onClick="RadioBox" ></RadioButton>
</RadioGroup>
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
if(rb1.isChecked())
{
Toast.makeText(getApplicationContext(), "male is
Selectd",Toast.LENGTH_LONG).show();
}
else if(rb2.isChecked())
{
Toast.makeText(getApplicationContext(), "Female is
Selectd",Toast.LENGTH_LONG).show();
}
}
The Spinner:
This is a list box type allowing us to choose between the given values.
We use the following tag for defining this type of View.
<Spinner
android:layout_width="150px"
android:layout_height="50px"
android:id="@+id/sp1" > </Spinner>
We can add the String array resources using the following tag.
<String-array name=”myarray”>
<item>Value1</item>
<item>Value2</item>
</String-array>
Now we want to set the values for the spinner we write the following code in
the activity class onCreate() method.
String values[]=getResources.getStringArray(R.array.myarray);
ArrayAdapter<String> aa=new
ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item,values);
Spinner sp1=(Spinner)findViewById(R.id.sp1);
sp1.setAdapter(aa);
android:entries=”@array/myarray”
This allows us define the view with large height like may be with many
components into it.
We use <ScrollView> tag to describe having the Scroll support.
The ListView:
Example:
<ListView
android:layout_width="150px"
android:layout_height="50px"
android:id=”@+id/mylist”/>
In the ‘onCreate()’ method of the activity we can have the following code for setting
a list of values.
ListView lv=(lv)findViewById(R.id.mylist);
String values[]={“value1”,”value2”};
ArrayAdapter<String> aa=new
ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item,va
lues);
lv.setAdapter(aa);
We can configure this listener to the ‘ListView’ this declares only one
method.
we can have a text filtering for the ListView we can be enable using the following
method of the ListView.
setTextFilterEnabled(Boolean)
we can use ListActivity as a utility for creating the Activity that just needs a
ListView as a View.
{
public void onCreate(Bundle b)
{
super.onCreate(b);
ListView lv=getListView();
……………………..
}
}
The GridView:
This is similar to the ListView , but supports arranging the elements into
rows and columns.
The following tag is used to define this View.
<GridView ....></>
We use android:numColumns attribute specifying the number of columns,
and the number of rows is decided decided based on the items.
Like done with the ListView we set the adapter for the items,and can set
thee ‘OnItemClickListenr’
The DatePicker:
This allows us to have a view that can take the date from the user. The
following tag is used to configure this view
<DatePicker .........../>
If the method is not invoked it by default sets the date to the system date.
The TimePicker:
Similar to the DatePicker this view allows us to select the time that includes
hours and minutes.
This is alsosupported with a listener to get the notification on the change in
the time, just like with DatePicker, this supports onTimeChangedListener
with one method described below.
Progress Bar:
This is the View that supports to describe the end user that some long
process is being taking up and you may want to wait for completing process.
We generally can categorize the Progress Bar into two types.
1.Indetermine ProgressBar
2. Determine ProgressBar
Indetermine ProgressBar:
In this case the progress bar doesn’t describes the status of the work
completed and/or the balance work, this is used in case if we are not known
like that amount of work is completed and how much still balance.
Determined ProgressBar:
The following tag is used to describe the progress bar, and by default
this is Indeterminent.
<ProgressBar.............../>
Example:
ProgressBar pb=(ProgressBar)findViewById(R.id.pb);
pb.setVisibility(ProgressBar.GONE);
requestWindowFeature(Window.FEATURE_INDETERMINE_PROGRESS)
(before setContentView(.............))
And the following method of the Activity can support us to show the
visibility of this feature.
setProgressBarIndeterminate Visibility(boolean)
requestWindowFeature(Window.FEATURE_PROGRESS);
And same like earlier we use the following method for setting the
visibility of this view.
setProgressBarVisibility(boolean)
In addition here we can set the progress using the following method
of activity.
setProgress(int)
FifthActivity.java
package com.durgasoft.android;
import ……………………………………………………;
*/
AutoCompleteTextView act;
act=(AutoCompleteTextView)findViewById(R.id.act1);
String[] str=new
String[]{"SVU","OU","JNTUK","JNTUH","ANDHRA","SKU","YVU","VSU","PALAMURU","MAHA
TMA","OTHER"};
act.setThreshold(1);
act.setAdapter(aa);
Spinner sp1=(Spinner)findViewById(R.id.sp1);
String[] str1=getResources().getStringArray(R.array.myarray);
}
public void CheckBox(View view)
{
CheckBox cb1,cb2;
if(cb1.isChecked())
{
Toast.makeText(getApplicationContext(), "MCA is
Selectd",Toast.LENGTH_LONG).show();
}
else if(cb2.isChecked())
{
Toast.makeText(getApplicationContext(), "BTech is
Selectd",Toast.LENGTH_LONG).show();
}
}
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
if(rb1.isChecked())
{
Toast.makeText(getApplicationContext(), "male is
Selectd",Toast.LENGTH_LONG).show();
}
else if(rb2.isChecked())
{
Toast.makeText(getApplicationContext(), "Female is
Selectd",Toast.LENGTH_LONG).show();
}
Button b1=(Button)findViewById(R.id.dp);
b1.setText(dayOfMonth+"-"+(monthOfYear+1)+"-
"+year);
else if(index==TIME_PICKER_DIALOG)
{
OnTimeSetListener tl=new OnTimeSetListener() {
Button b1=(Button)findViewById(R.id.tp);
b1.setText(hourOfDay+"-"+minute);
}
};
The Main.xml:
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the Name" > </TextView>
<EditText android:layout_width="150px"
android:layout_height="wrap_content"
android:id="@+id/et1" >
</EditText>
</TableRow>
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the University " >
</TextView>
<AutoCompleteTextView android:layout_width="150px"
android:layout_height="wrap_content"
android:id="@+id/act1" >
</AutoCompleteTextView>
</TableRow>
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select The Course " >
</TextView>
<Spinner android:layout_width="150px"
android:layout_height="50px"
android:id="@+id/sp1" > </Spinner>
</TableRow>
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select The Qualification " > </TextView>
<CheckBox
android:layout_width="50px" android:layout_height="wrap_content"
android:id="@+id/cb1"
android:text="MCA"
android:onClick="CheckBox" ></CheckBox>
</TableRow>
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
Example:
The dialog_layout describes the view for the dialog just as done for the
activity.
d.setOwnerActivity(MyActivity,this);
d.show(); // This will show the dialog
We use any of the following methods of Dialog to choose the dialog box.
Cancel();
Dismiss();
Alert Dialog
ProgressBar Dialog
DatePicker Dialog
TimePicker Dialog
The AlertDialog: This supports us to create a dialog that meets the most
common requirement of having a dialog for showing some alert, this includes
- A Optional Title
- Message Text
- Zero, one, two, or three buttons
- List of items
The following methods of this object allows us setting the details for the
dialog.
- setTitle(String)
- setMessage(String)
- setPositiveButton(String lable,DialogInterface.OnClickLIstener
listener)
- setNegiveeButton(String lable,DialogInterface.OnClickLIstener
listener)
- setNutralButton(String lable,DialogInterface.OnClickLIstener
listener)
- setItems(String[])
Once setting all the details , we can invoke create() method for the
AlertDialog object and further show() method for showing.
Note:
The Activity retains the state of the dialog even on cancelled /
dismiss of the dialog is requested to show again . However invoking
remove Dialog(int) will release the dialog state with the activity.
The Menu:
The following methods ‘Activity ‘ are invoked to create the
menu for the activity.
OnCreateOptionsMenu(menu):
This method is invoked for the first time the user clicks on the menu
for the activity.
Note: From Android 3.0 this is invoked at the time starting the Activity as the menu
here is always visible.
To Listen for the menu item click we can override the following method.
package com.durgasoft.android;
import ......................................................................;
showDialog(TIME_PICKER_DIALOG);
}
public Dialog onCreateDialog(int index){
if(index==BASIC_DIALOG){
final Dialog d=new Dialog(this);
d.setContentView(R.layout.dialog1_layout);
Button b=(Button)d.findViewById(R.id.b2);
By Mr. Mahesh Durga Soft Solutions , SR Nagar , Hyderabad . www.durgasoft.com 46
b.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
return d;
}else if (index==DATE_PICKER_DIALOG){
TimePickerDialog tpd=
new TimePickerDialog(
this,dl, 10, 12, false);
return tpd;
}else if (index == ALERT_DIALOG1){
AlertDialog.OnClickListener listener=
new AlertDialog.OnClickListener(){
AlertDialog.Builder ab=
new AlertDialog.Builder(this);
ab.setTitle("Alert Dialog Sample");
ab.setMessage("You opened AlertDialog");
ab.setPositiveButton("OK", listener);
//ab.setNegativeButton("Not OK", listener);
//ab.setNeutralButton("Neutral", listener);
return ab.create();
}else if (index == ALERT_DIALOG2){
AlertDialog.Builder ab=
new AlertDialog.Builder(this);
ab.setTitle("Alert Dialog Sample");
DialogInterface.OnClickListener listener=
new DialogInterface.OnClickListener() {
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
};
ab.setItems(items, listener);
return ab.create();
}else{
return null;
}
}
@Override
public boolean onCreateOptionsMenu(
Menu menu) {
MenuInflater mi=new MenuInflater(this);
mi.inflate(R.menu.menu1, menu);
return true;
}
@Override
public boolean onMenuItemSelected(
int featureId, MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
></TextView>
<Button android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button1Click"
android:text="Show Dialog">
</Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="150px"
<Button android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button2Click"
android:text="Set Date">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/time_et"
android:layout_width="100px"
android:layout_height="wrap_content">
</EditText>
<Button android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button3Click"
android:text="Show Time Dialog">
</Button>
</LinearLayout>
</LinearLayout>
dialog1_layout.xml:
<?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 android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a dialog box" ></TextView>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:id="@+id/et1" ></EditText>
<Button android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"></Button>
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></DatePicker></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@id/android:tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@id/android:tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</TabHost>
Menu1.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/m1"
android:title="Option1"></item>
<item android:id="@+id/m2"
android:title="Option2"></item>
<item android:id="@+id/m3"
android:title="Option3"></item>
<item android:id="@+id/m4"
android:title="Option4"></item>
<item android:id="@+id/m5"
android:title="File">
<menu >
<item android:id="@+id/m6"
android:title="Open"></item>
<item android:id="@+id/m7"
android:title="New"></item>
</menu>
</item>
</menu>
By Mr. Mahesh Durga Soft Solutions , SR Nagar , Hyderabad . www.durgasoft.com 51
By Mr. Mahesh Durga Soft Solutions , SR Nagar , Hyderabad . www.durgasoft.com 52