Android App Development Guide
Android App Development Guide
1. Text View 2. Button 3. Image Button 4. Toggle Button 5.Radio Button 6. Radio Group
Button. 7. Custom Toast alert 8.Scroll View
14. What is the fragment? How fragments can be created and explain any one methods
with appropriate code to create fragment ?
1
4 Marks Question Answers
2
<TableRow>
<TextView
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_column="2" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_column="2" />
</TableRow>
</TableLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>
3
2. Explain following files:
1. Main Activity File 2. Manifest File 3. String File 4. layout file?
Ans: 1. The Main Activity File: The main activity code is a Java file MainActivity.java. This
is the actual application file which ultimately gets converted to a Dalvik executable and runs
your application. Following is the default code generated by the application wizard for Hello
World! application:
package com.example.helloworld;
import android.support.v7.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);
}
}
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder.
The onCreate() method is one of many methods that are figured when an activity is loaded.
2. The Manifest File: Whatever component you develop as a part of your application, you must
declare all its components in a manifest.xml which resides at the root of the application project
directory. This file works as an interface between Android OS and your application, so if you do
not declare your component in this file, then it will not be considered by the OS. For example, a
default manifest file will look like as following file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ProgramDemo.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4
Following is the list of tags which you will use in your manifest file to specify different
Android application components:
<activity>elements for activities
<service> elements for services
<receiver> elements for broadcast receivers
<provider> elements for content providers
3. The Strings File: The strings.xml file is located in the res/values folder and it contains all the
text that your application uses. For example, the names of buttons, labels, default text, and
similar types of strings go into this file. This file is responsible for their textual content. For
example, a default strings file will look like as following file:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
4. The Layout File: The activity_main.xml is a layout file available in res/layout directory, that
is referenced by your application when building its interface. You will modify this file very
frequently to change the layout of your application. For your "Hello World!" application, this file
will have following content related to default layout:
<RelativeLayout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
This is an example of simple RelativeLayout. The TextView is an Android control used to build
the GUI and it have various attributes like android:layout_width, android:layout_height etc
which are being used to set its width and height etc.. The @string refers to the strings.xml file
located in the res/values folder. Hence, @string/hello_world refers to the hello string defined in
the strings.xml file, which is "Hello World!"
5
3. Develop an android application using absolute layout.
Ans: Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<!--Setting up TextViews-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="300px" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="120px"
android:layout_y="350px" />
</AbsoluteLayout>
colors.xml
<resources>
<color name="colorPrimary">#0F9D58</color>
<color name="colorPrimaryDark">#16E37F</color>
<color name="colorAccent">#03DAC5</color>
</resources>
mainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView heading, subHeading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Referencing the TextViews
6
heading = (TextView) findViewById(R.id.heading);
subHeading = (TextView) findViewById(R.id.subHeading);
// Setting text dynamically
heading.setText("Computer Science Portal");
subHeading.setText("GeeksForGeeks");
}
}
7
Android Fragment: it is the part of activity, it is also known as sub-activity. There can be more
than one fragment in an activity. Fragments represent multiple screen inside one activity.
Android fragment lifecycle is affected by activity lifecycle because fragments are included in
activity. Each fragment has its own life cycle methods that is affected by activity life cycle
because fragments are embedded in activity. The FragmentManager class is responsible to make
interaction between fragment objects.
Basically fragments are divided as three stages as shown below.
Single frame fragments: Single frame fragments are using for hand hold devices like mobiles,
here we can show only one fragment as a view.
List fragments: fragments having special list view is called as list fragment
Fragments transaction: Using with fragment transaction. we can move one fragment to another
fragment.
8
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
strings.xml File:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">Settings</string>
<string name="reminder">Enter your name</string>
</resources>
9
7. Custom Toast alert: Toast is a small popup notification that is used to display information
about the operation which we performed in our app. The Toast will show the message for a small
period of time and it will disappear automatically after a timeout.
8. Scroll View: ScrollView is a view group that is used to make vertically scrollable views. A
scroll view contains a single direct child only. In order to place multiple views in the scroll view,
one needs to make a view group (like LinearLayout) as a direct child and then we can define
many views inside it.
10
9. Develop an android application using frame layout.
Ans: MainActivity.java File:
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml File:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="250px"
android:layout_width="250px"/>
<TextView
android:text="Frame Demo"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
strings.xml File:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">demo</string>
<string name="action_settings">Settings</string>
</resources>
11
10. Explain the use of List View and grid view.
Ans: A GridView is a type of AdapterView that displays items in a two-dimensional scrolling
grid. Items are inserted into this grid layout from a database or from an array. The adapter is used
for displaying this data, setAdapter() method is used to join the adapter with GridView. The
main function of the adapter in GridView is to fetch data from a database or array and insert each
piece of data in an appropriate item that will be displayed in GridView. This is how the
GridView structure looks like.
A ListView is a type of AdapterView that displays a vertical list of scroll-able views and each
view is placed one below the other. Using adapter, items are inserted into the list from an array
or database. For displaying the items in the list method setAdaptor() is used. setAdaptor()
method conjoins an adapter with the list. Android ListView is a ViewGroup that is used to
display the list of items in multiple rows and contains an adapter that automatically inserts the
items into the list. The main purpose of the adapter is to fetch data from an array or database and
insert each item that placed into the list for the desired result. So, it is the main source to pull
data from strings.xml file which contains all the required strings in Java or XML files.
12
DatabaseDemoActivity.java
package com.DataBaseDemo;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DataBaseDemoActivity extends Activity {
/** Called when the activity is first created. */
SQLiteDatabase db;
Button btnInsert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnInsert=(Button)findViewById(R.id.button1);
try{
db=openOrCreateDatabase("StudentDB",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("Create Table Temp(id integer,name text)");
}catch(SQLException e)
{
}
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText eid=(EditText) findViewById(R.id.editText1);
EditText ename=(EditText)findViewById(R.id.editText2);
ContentValues values=new ContentValues();
values.put("id", eid.getText().toString());
values.put("name", ename.getText().toString());
13
if((db.insert("temp", null, values))!=-1)
{
Toast.makeText(DataBaseDemoActivity.this, "Record Successfully Inserted", 2000).show();
}
else
{
Toast.makeText(DataBaseDemoActivity.this, "Insert Error", 2000).show();
}
eid.setText("");
ename.setText("");
Cursor c=db.rawQuery("SELECT * FROM temp",null);
c.moveToFirst();
while(!c.isAfterLast())
{
Toast.makeText(DataBaseDemoActivity.this,c.getString(0)+ ""+c.getString(1),1000).show();
c.moveToNext();
}
c.close();
}
});
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
db.close();
super.onStop();
}
}
14
12. Explain the concept of geocoding and reverse geocoding.
Ans: We can achieve this by using Geocoder class. Finding the geographical coordinates
(latitude and longitude) of a given address or location is called Geocoding. opposite of
geocoding where a pair of latitude and longitude is converted into an address or location called
as Reverse Geocode. Geocode or Reverse Geocode is achieved by importing the proper package.
import android.location.Geocoder;
The geocoding or reverse geocoding operation needs to be done on a separate thread and should
never be used on the UI thread as it will cause the system to display an Application Not
Responding (ANR) dialog to the user.
Example:
Geocoder ge= new Geocoder(context);
if(gc.isPresent()){
List<Address> list = gc.getFromLocationName("155 Park Theater, Palo Alto, CA", 1);
Address address = list.get(0);
double lat = address.getLatitude();
double Ing = address.getLongitude():
}
For reverse Geocode:
Geocoder gc = new Geocoder(context);
if(gc.isPresent()){
List<address> list = gc.getFromLocation(37.42279,122.08506,1);
Address address list.get(0);
StringBuffer str = new StringBuffer(); str.append("Name: " + address.getLocality() + "\n");
str.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
str.append("Admin Area: " + address.getAdminArea() + "\n");
str.append("Country: " + address.getCountryName() + "\n");
str.append("Country Code: " + address.getCountryCode() + "\n");
String strAddress = str.toString();
}
15
Components of Location-Based Services in Android:
The classes and the interfaces present in the Location Framework acts as the essential
components for LBS. Those components are as follows:
LocationManager Class: It is used to get Location Service access from the system.
LocationListener Interface: It receives updates from the Location Manager class.
LocationProvider: It is the class that provides us with the location for our devices.
Location Class: Its objects carry information about the location. The information includes
latitude, longitude, accuracy, altitude, and speed.
Location Object: The location objects carry the location of your device. The place is in the form
of latitude and longitude. On the location object, you can apply the below methods. The below
methods help you to get location and other information regarding the location.
1. float distanceTo(Location destination): It gives the approximate distance between our current
location and the destination location.
2. float getAccuracy(): It gives us the accuracy of our location in metres.
3. double getAltitude(): It gives us the altitude of our place above sea level.
4. double getLatitude(): It gives the latitude coordinate of our place in degrees.
14. What is the fragment? How fragments can be created and explain any one
methods with appropriate code to create fragment?
Ans: Android Fragment is the part of activity, it is also known as sub-activity. There can be
more than one fragment in an activity. Fragments represent multiple screen inside one activity.
Android fragment lifecycle is affected by activity lifecycle because fragments are included in
activity. Each fragment has its own life cycle methods that is affected by activity life cycle
because fragments are embedded in activity. The FragmentManager class is responsible to make
interaction between fragment objects.
Android Fragment Lifecycle Methods:
1) onAttach(Activity): it is called only once when it is attached with activity.
2) onCreate(Bundle): It is used to initialize the fragment.
3) onCreateView(LayoutInflater, ViewGroup, Bundle): creates and returns view hierarchy.
4) onActivityCreated(Bundle): It is invoked after the completion of onCreate() method.
5) onViewStateRestored(Bundle): It provides information to the fragment that all the saved
state of fragment view hierarchy has been restored.
6) onStart(): makes the fragment visible.
7) onResume(): makes the fragment interactive.
8) onPause(): is called when fragment is no longer interactive.
9) onStop(): is called when fragment is no longer visible.
10) onDestroyView(): allows the fragment to clean up resources.
11) onDestroy(): allows the fragment to do final clean up of fragment state.
12) onDetach(): It is called immediately prior to the fragment no longer being associated with its
activity.
16
Example:
activity_main.xml File:
<?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="fill_parent"
android:layout_height="fill_parent"
tools:context="example.javatpoint.com.fragmentexample.MainActivity">
<fragment
android:id="@+id/fragment1"
android:name="example.javatpoint.com.fragmentexample.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment2"
android:name="example.javatpoint.com.fragmentexample.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
fragment_fragment1.xml
<FrameLayout 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:background="#F5F5DC"
tools:context="example.javatpoint.com.fragmentexample.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
fragment_fragment2.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
17
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0FFFF"
tools:context="example.javatpoint.com.fragmentexample.Fragment2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
MainActivity.java
package example.javatpoint.com.fragmentexample;
import android.support.v7.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);
}
}
Fragment1.java
package example.javatpoint.com.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
18
Fragment2.java
package example.javatpoint.com.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
}
19
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>
Output:
20
16. Describe Android ecosystem.
Ans: ecosystem is nothing but the relationship between Users, Developers/Programmers, and
Hardware equipment makers, the Android ecosystem is nothing but the mutual dependence
between Users, Developers, and equipment makers. they are independent of each other so one
cannot exist without the other.
The main three block of the android ecosystem is as follow:
1. Android User (Users buy handsets and software Application): Android users have more
space for customizability for their android devices. Android users are smarter than other users
and they are perceived to have greater levels of support. Android users are also more likely to
prefer saving their cost and love the openness of the platform also they like to customize their
device. Android users are fancier to prefer saving money and also android user like customizing
their android handset/device
2. Developers (sell Application): Android Developers are the professional software developer in
designing applications as well as developing applications for Android. Some of the following
tasks where an android developer can play his role in the development of android apps: Design
and build advanced applications for the android platform Collaborate and define with
development teams for design and deliver new cool features. Troubleshoot and fix bugs in new
and existing applications for Users. Evaluate and implement new development tools to work with
outside data sources and APIs.
3. Equipment Maker: Android equipment’s are available in the market in a huge amount.
Smartwatches: A smartwatch is a handheld, wearable device that closely relates a wristwatch or
other time device. In addition to telling time, many smartwatches are wireless connectivity
oriented such as Bluetooth capable. The traditional watch becomes, in effect, a wireless
Bluetooth technology extending the capabilities of the wearer's smartphone to the watch.
Smart TV: An Android TV box is a small computer that plugs into any TV and gives the user
the ability to stream content, locally and online. Apps can be downloaded from the Google Play
Store, installed, and do most anything a standard computer can do from streaming videos to
writing an email.
Smart Speakers: Smark speakers are booming in the market now, Smark speakers like Google
Home, Alexa, We can control our android device via voice using these smart speakers.
E-Reader: E-Reader is a device used for reading e-books, digital newspapers, other reading
stuff.
21