Вы находитесь на странице: 1из 90

Practical-1: Introduction to Android, Introduction to Android Studio IDE, Application Fundamentals:

1. In Android Studio, create new project version(3.4.2):


a.) If you don’t have a project opened, in the Welcome to Android Studio window, click Start a
new Android Studio Project.

b.) If you have a project opened, select File>Project.


2. In the Choose your project screen, select Empty Activity and click next.

3. In the Configure your project screen, enter the following:-


a.) Name- My Application
b.) Package name - com.example.myapplication
c.) Click on Finish.

After some processing, Android Studio opens the IDE.


First, be sure the Project window is open (select View > Tool Windows > project) and the Android view
is selected from the drop-down list at the top of that window.

App > java > com.example.myapplication > MainActivity.java


2
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

This is the main activity. When you build and run the app, the system launches an instances of this
activity and loads its layout.

App > res > layout > activity_main.xml


This XML file defines the layout for the activity’s UI. It contains a Textview element with the text
“Hello world”.

App > Manifest > AndroidManifest.xml


The manifest file describes the fundamental characteristics of the app and defines each of its
components.
Gradle Scripts > build.gradle
Two files with this name: one for the “project” and one for the “app” module.
RUN ON AN EMULATOR:
1. Launch the Android Virtual Device Manager by selecting Tools > Android > AVD Manager, or
by clicking the AVD manager icon in the toolbar.
2. In the Your Virtual Device screen, click Create Virtual Device.

3. In the Select Hardware screen, select a phone device, such as pixel or nexus, and then click
next.

4. In the System Image screen, click Download for one of the recommended system images.
Agree to the terms to complete the download.
4
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

5. After the download is complete, select the system image from the list and click next.
6. On the next screen, leave all the configuration settings as they are and click Finish.

7. Back in the Your Virtual Devices screen, select the device created and click.
LAUNCH THIS AVD IN THE EMULATOR:
While the emulator starts up, close the Android Virtual Device Manager Window and return, run
the app.
1. Once the emulator is booted up, Click the app module in the project window and the select
Run > Run ‘app’.

2. In the Select Deployment Target window, select the emulator and Click OK.
3. Android studio install the app on the emulator and starts it.

OPEN THE LAYOUT EDITOR:


1. In Android Studio’s Project window, open app > res > layout > activity_main.xml.
2. Click the Design Tab at the bottom of the bottom.

3. Click Show Blueprint so only the blueprint layout is visible.


6
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

4. The tooltip in the toolbar should read Hide Constraint.


5. Click Default Margins in the toolbar and select 16.

Output:-
Practical-2: Programming Resources Android Resources: (Color, Theme, String, Drawable, Dimension,
Image).

1. Define new color properties in color.xml


Go to app > res > values > color.xml.

In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorRed"
android:text="sers" />

<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorBlue"
android:text="aws" />

</LinearLayout>
8
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

In app > res > values > styles.xml add


<resources>
<!-- Base application theme. -->
<style name="BlueTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#00D0EF</item>
<item name="android:textColor">#000000</item>
</style>
</resources>
String.xml
App > res > values > String.xml
<resources>
<string name="app_name">My Application</string>
<String name="ParaHead">sers</String>
<String name="Description">aws</String>
</resources>

In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorRed"
android:text="@String/ParaHead" />

<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorBlue"
android:text="@String/Description" />
</LinearLayout>
10
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

DRAWABLE AND IMAGES:-


Right Click on drawable directory and paste three images.
Select destination.

Click ok

see Drawable

App > res > layout > activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="107dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="93dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/square" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="186dp"
android:layout_height="166dp"
android:layout_marginStart="141dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="65dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView1"
app:srcCompat="@drawable/star" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="157dp"
android:layout_height="162dp"
android:layout_marginStart="204dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:srcCompat="@drawable/triangle" />
</android.support.constraint.ConstraintLayout>
12
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

DIMENSION
App > res > values > dimens.xml
In dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">35dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">26sp</dimen>
</resources>

In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="normal"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="123dp" />
<TextView
android:id="@+id/t2"
android:layout_width="@dimen/textview_width"
android:layout_height="@dimen/textview_height"
android:textSize="@dimen/font_size"
android:text="modified"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
Practical-3A: Programming Activities and fragments
Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle of fragments and multiple fragments.

1. Create New Project.


In MainActivity.java
App > java > com.example.myapplication
In MainActivity.java
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
String tag="lifecycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,"in the onCreate() event");
}
public void onStart(){
super.onStart();
Log.d(tag,"in the onstart() event");
}
public void onResume(){
super.onResume();
Log.d(tag,"in the onRestart() event");
}
public void onPause(){
super.onPause();
Log.d(tag,"in the onPause() event");
}
public void onStop(){
super.onStop();
Log.d(tag,"in the onStop() event");
}
public void onDestroy(){
super.onDestroy();
Log.d(tag,"in the onDestroy() event");
}}

PRACTICAL-3B
14
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Create new project.


Add new Fragment
Java > New > Fragment >Fragment(Blank)
Click on finish.

Right click in the .java file and select generate and override methods.

Select the following in the override methods:-


1. onAttach(context:Context):void.
2. onCreate(savedinstanceState.Bundle):void.
3. onViewCreated(view.View:savedinstanceState.Bundle):void.
4. onActivityCreated(savedinstanceStatesBundle):void
5. onStart():void
6. onResume():void
7. onPause():void
8 .onStop():void
9. onDestroy():void
10. onDestroyview():void

In BlankFragemnt2.java
package com.example.myapplication;

import android.arch.lifecycle.Lifecycle;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment2 extends Fragment {
String tag="lifecycle";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
@Override
public Lifecycle getLifecycle() {
return super.getLifecycle();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(tag,"event");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(tag,"event");
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(tag,"event");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(tag,"event");
}
@Override
16
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

public void onStart() {


super.onStart();
Log.d(tag,"event");
}
@Override
public void onResume() {
super.onResume();
Log.d(tag,"event");
}
@Override
public void onPause() {
super.onPause();
Log.d(tag,"event");
}
@Override
public void onStop() {
super.onStop();
Log.d(tag,"event"); Output:
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(tag,"event");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(tag,"event");
}
public BlankFragment2() {
}
}
In fragment_blank_fragment2.xml

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


<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"
tools:context=".BlankFragment2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/check_log_cat" />

</FrameLayout>

In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/sd1"
android:layout_width="match_parent"
android:layout_height="match_parent">
class="com.example.myapplication.BlankFragment2"
tools:layout="@layout/fragmement_blank_fragment2"
</fragment>
</android.support.constraint.ConstraintLayout>

In mainActivity.java
package com.example.myapplication;

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);
}
}
18
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Practical 4: Programs related to different Layouts


Coordinate, Linear, Relative, Table, Absolute, Frame, List View, Grid View.

Create Activities
Right Click on app -> New -> Activity-> Empty Activity

Activity Name: RelativeActivity


Layout Name: activity_relative

In activity_relative.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RelativeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblComments"
android:text="Comments"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true />
<EditText
android:layout_width="fill_parent"
android:layout_height="170px"
android:id="@+id/txtComments"
android:textSize="180sp"
android:layout_alignLeft="@id/lblComments"
android:layout_below="@id/lblComments"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="175px"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:text="Save"
android:layout_alignRight="@id/txtComments"
android:layout_below="@id/txtComments" />
<Button
android:layout_width="210px"
android:layout_height="wrap_content"
android:id="@+id/btnCancel"
android:text="Cancel"
android:layout_alignLeft="@id/txtComments"
android:layout_below="@id/txtComments" />
</RelativeLayout>

Activity Name: LinearHorizontalActivity


Layout Name: activity_linear_horizontal

In activity__linear_horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".LinearHorizontalActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="Button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="Button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:text="Button4"/>
</LinearLayout>
20
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Activity Name: LinearVerticalActivity


Layout Name: activity_vertical_horizontal

In activity__vertical_horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LinearVerticalActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="Button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="Button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:text="Button4"/>
</LinearLayout>
Activity Name: TableActivity
Layout Name: activity_table

In activity_table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TableActivity">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="30dp"
android:text="Row 1 Column 1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="100dp"
android:text="Row 1 Column 2"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="30dp"
android:text="Row 2 Column 1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="100dp"
android:text="Row 2 Column 2"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="30dp"
android:text="Row 3 Column 1"/>
22
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="100dp"
android:text="Row 3 Column 2"/>
</TableRow>
<TableRow>
<TextView
android:width="120dp"
android:text="User Name"/>
<EditText
android:width="200dp"
android:id="@+id/txtUserName" />
</TableRow>
<TableRow>
<TextView
android:text="Password"/>
<EditText
android:id="@+id/txtPassword"
android:password="true"/>
</TableRow>
<TableRow>
<TextView />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/chkRememberPassword"
android:text="Remember Password"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonSignin"
android:text="Log In"
/>
</TableRow>
</TableLayout>
Activity Name: ScrollActivity
Layout Name: activity_scroll

In activity_scroll
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScrollActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 1"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 2"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 3"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 4"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 5"
android:textSize="40dp"/>
<TextView
24
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 6"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 7"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 8"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 9"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 10"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 11"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 12"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 13"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 14"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 15"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 16"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 17"
android:textSize="40dp"/>
</LinearLayout>
</ScrollView>

Activity Name: AbsoluteActivity


Layout Name: activity_absolute

In activity_absolute.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="match_parent"
android:layout_height="match_parent"
tools:context=".AbsoluteActivity">
<Button
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_x="50px"
android:layout_y="30px"/>
<Button
android:layout_width="188dp"
android:layout_height="wrap_content"
26
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:text="Button2"
android:layout_x="200px"
android:layout_y="550px"/>
<Button
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_x="350px"
android:layout_y="740px"/>
<Button
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="Button4"
android:layout_x="12px"
android:layout_y="361px"/>
</AbsoluteLayout>

Activity Name:ListViewActivity
Layout Name: activity_list_view
In activity_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListViewActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvt"
android:text="List View"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv"
android:layout_below="@id/tvt"></ListView>
</RelativeLayout>

In ListViewActivity.java
package com.example.layout;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewActivity extends Activity {
ListView l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
final String[] s ={"Android","iOS","Windows","Symbian","Blackberry","OS","J2ME"};
l=findViewById(R.id.lv);
ArrayAdapter ada = new
ArrayAdapter(ListViewActivity.this,android.R.layout.simple_list_item_1,s);
l.setAdapter(ada);
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListViewActivity.this,"Item Clicked :
"+position,Toast.LENGTH_LONG).show();
Toast.makeText(ListViewActivity.this,"Item "+s[position],Toast.LENGTH_SHORT).show();
}
});
}
}
28
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Activity Name: GridvActivity


Layout Name: activity_gridv

In activity_gridv.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GridvActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Grid View"
android:gravity="center"/>
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gridview"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"></GridView>
</LinearLayout>

In GridvActivity.java
package com.example.layout;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class GridvActivity extends Activity {


Integer[]
imageIDs={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launch
er,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridv);
GridView gridView=findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),"pic "+(position+1),Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter{
private Context context;
public ImageAdapter(Context c){
context=c;
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView ;
if(convertView==null){
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
30
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

}else{
imageView=(ImageView)convertView; }
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
}

Activity Name: FrameActivity


Layout Name: activity_frame
In activity_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FrameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblComments"
android:text="Hello World"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/lblComments"
android:layout_below="@id/lblComments"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
</RelativeLayout>
In activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demnonstration"
android:layout_gravity="center" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Relative Layout "
android:id="@+id/btnrelative"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Linear Layout(Horizontal) "
android:id="@+id/btnlinearH"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Linear Layout(Vertical) "
android:id="@+id/btnlinearV"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Table Layout "
android:id="@+id/btnTable"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
32
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:text="ScrollView "
android:id="@+id/btnScrollV"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Absolute"
android:id="@+id/btnAbsolute"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ListView "
android:id="@+id/btnListView"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GridView "
android:id="@+id/btnGrid"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Frame "
android:id="@+id/btnFrame"
android:layout_gravity="center"/>
</LinearLayout>

In MainActivity.java
package com.example.layout;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnrelative = (Button)findViewById(R.id.btnrelative);
Button btnlinearH = (Button)findViewById(R.id.btnlinearH);
Button btnlinearV = (Button)findViewById(R.id.btnlinearV);
Button btnTable = (Button)findViewById(R.id.btnTable);
Button btnScrollV = (Button)findViewById(R.id.btnScrollV);
Button btnAbsolute = (Button)findViewById(R.id.btnAbsolute);
Button btnListView = (Button)findViewById(R.id.btnListView);
Button btnGrid = (Button)findViewById(R.id.btnGrid);
Button btnFrame = (Button)findViewById(R.id.btnFrame);

btnrelative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),RelativeActivity.class);
startActivity(i);
} });
btnlinearH.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),LinearHorizontalActivity.class);
startActivity(i);
} });
btnlinearV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),LinearVerticalActivity.class);
startActivity(i);
} });
btnTable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),TableActivity.class);
startActivity(i);
} });

btnScrollV.setOnClickListener(new View.OnClickListener() {
@Override
34
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

public void onClick(View v) {


Intent i = new Intent(getApplicationContext(),ScrollActivity.class);
startActivity(i);
} });
btnAbsolute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), AbsoluteActivity.class);
startActivity(i);
} });
btnListView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),ListViewActivity.class);
startActivity(i);
} });
btnGrid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),GridvActivity.class);
startActivity(i);
} });
btnFrame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),FrameActivity.class);
startActivity(i);
} }); }}
Practical 5a : Programming UI elements - AppBar
1.Create an AppBar
I. Remove the default Action Bar .
Go to values -> styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">


Changle this line to
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

II. Add new Layout Go to res -> layout


Right click on layout folder and add a layout “toolbar_layout”
Select Root Element as Toolbar from library support

In toolbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:id="@+id/toolBar" >
</android.support.v7.widget.Toolbar>

In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/toolbar_layout"/>
</LinearLayout>

Setting the toolbar as default AppBar


36
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

In ActivityMain.java
package com.example.appbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {


private Toolbar tbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbar=findViewById(R.id.toolBar);
setSupportActionBar(tbar);
}
}
2. Add menus to AppBar
Go to https://materials.io/tools/icons/ and download sample icons
And rename to ic_search
Copy and paste the icon to res -> mipmap folder
Select hdpi
Right click on res folder. Add New xml file “app_bar_menu”

In app_bar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="Setting"
android:icon="@mipmap/ic_setting"
app:showAsAction="ifRoom"
/>
<item
android:title="About"
android:id="@+id/action_about"
app:showAsAction="never"
/>
</menu>
To add this menu in AppBar
In MainActivity.java add a new method onCreateOptioMenu()

package com.example.appbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private Toolbar tbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbar=findViewById(R.id.toolBar);
setSupportActionBar(tbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.app_bar_menu,menu);
return true;}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_search:
Toast.makeText(this,"Search menu click",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_about:
Toast.makeText(this,"About menu click",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
38
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Practical 5b: Programming UI elements- Fragments


Remove Hello World label
Add a frame layout as container and set margin to 24dp and set the id to fragment_container

Add 3 Fragments
Right click app folder. Go to New -> Fragment-> Fragment(Blank)

fragement_first_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".First_Fragment">

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Open Second Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="124dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="First Fragement"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.234" />
</android.support.constraint.ConstraintLayout>

In First_Fragment.java
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class First_Fragment extends Fragment {
private Button button;
public First_Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first_layout,container,false);
button=view.findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.fragmentManager.beginTransaction().replace(R.id.fragment_container,new
Second_Fragment(),null).addToBackStack(null).commit();
}
});
return view; }}
40
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

fragement_second_layout.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Second_Fragment">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Second Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Open Third Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />
</android.support.constraint.ConstraintLayout>
In SecondFragment.java
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Second_Fragment extends Fragment {
private Button button;
public Second_Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_second_layout,container,false);
button=view.findViewById(R.id.b2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.fragmentManager.beginTransaction().replace(R.id.fragment_container,new
Third_Fragment(),null).addToBackStack(null).commit();
} });
return view;
}}

fragement_third_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Third_Fragment">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
42
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Third Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

In MainActivity.java
package com.example.fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
public static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager=getSupportFragmentManager();
if(findViewById(R.id.fragment_container)!=null){
if(savedInstanceState!=null){
return;
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
First_Fragment ff = new First_Fragment();
fragmentTransaction.add(R.id.fragment_container,ff,null);
fragmentTransaction.commit();
}
}
}
Practical 5c: Programming UI elements -UI Components
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnSave"
android:text="Save"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/ib"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/el"
android:text="Edit Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb1"
android:text=" Auto Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cbst"
android:text=" "
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
44
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

<RadioButton
android:id="@+id/rb1"
android:text="Option 1 "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rb2"
android:text="Option 2 "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<ToggleButton
android:id="@+id/tb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

In ActivityMain.java

package com.example.uicomponent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


Button bsave;
ImageButton imgb;
CheckBox cb1,cbst;
RadioButton rb1,rb2;
EditText etext;
RadioGroup radioGroup;
ToggleButton tbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bsave=findViewById(R.id.btnSave);
imgb=findViewById(R.id.ib);
cb1=findViewById(R.id.cb1);
cbst=findViewById(R.id.cbst);
rb1=findViewById(R.id.rb1);
rb2=findViewById(R.id.rb2);
etext=findViewById(R.id.el);
radioGroup=findViewById(R.id.rg);
tbutton=findViewById(R.id.tb);
bsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DisplayToast(" Save Button Clicked ");
} });
imgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DisplayToast(" Image Button Clicked ");
} });
cb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
DisplayToast(" Checkbox Option1 is Checked");}
else{
DisplayToast(" Checkbox Option1 is Unchecked");
} } });
cbst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
DisplayToast(" Checkbox Option2 is Checked");}
else{
DisplayToast(" Checkbox Option2 is Unchecked");
} } });
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
46
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


if(rb1.isChecked()){
DisplayToast(" RadioButton 1 is Checked");
}else{
DisplayToast(" RadioButton 1 is Unchecked");
} } });
tbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tbutton.isChecked()){
DisplayToast(" Toggle Button is ON");
}
else{
DisplayToast(" Toggle Button is OFF");
} } });
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
} }); }
public void DisplayToast(String s){
Toast.makeText(this,s, Toast.LENGTH_SHORT).show();
}
}
Practical 6a: Programming menus

mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:id="@+id/newb"
android:title="Bookmarks"/>
<item
android:id="@+id/search"
android:title="Search"/>
<item
android:id="@+id/share"
android:title="Share"/>
<item
android:id="@+id/save"
android:title="Save"/>
<item
android:id="@+id/delete"
android:title="Delete"/>
<item
android:id="@+id/exit"
android:title="Exit"/>
</menu>

MainActivity.java
package com.example.menu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
48
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

setContentView(R.layout.activity_main); }
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.mymenu,menu);
return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.newb:
Toast.makeText(this,"New Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.share:
Toast.makeText(this,"Share Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(this,"Delete Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.save:
Toast.makeText(this,"Save Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.search:
Toast.makeText(this,"Search Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.exit:
Toast.makeText(this,"Exit Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this,"Default",Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Practical 6b : Programming dialog
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_dialog"
android:text="Click to display an AlertDialog"
android:onClick="onClickDialog"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_progressdialog"
android:text="Click to display a ProgressDialog"
android:onClick="onClickProgressDialog"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_datedialog"
android:text="Click to display an DatePickerDialog"
android:onClick="onClickDateDialog"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_timedialog"
android:text="Click to display an TimePickerDialog"
android:onClick="onClickTimeDialog"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView_date"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
50
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:layout_height="wrap_content"
android:id="@+id/textView_time"
android:textSize="50dp"/>
</LinearLayout>
In MainACtivity.java
package com.example.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


CharSequence[] items ={"Android","Security","Cloud"};
boolean[] itemsChecked = new boolean[items.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickDialog(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This is a dialog with simple Text ...");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"OK Clicked",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"CANCEL Clicked",Toast.LENGTH_SHORT).show();
} });
builder.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int id, boolean isChecked) {
Toast.makeText(getBaseContext(),items[id]+(isChecked? "checeked!" :
"unchecked"),Toast.LENGTH_SHORT).show();
} });
AlertDialog dialog = builder.create();
builder.show(); }
public void onClickProgressDialog(View v){
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setMessage("Loading ....... ");
pDialog.incrementProgressBy(20);
pDialog.setButton(Dialog.BUTTON_POSITIVE, "StopProgress", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pDialog.dismiss();
} });
pDialog.show(); }
public void onClickDateDialog(View v){
final int mYear, mMonth, mDay;
final TextView DateDisplay =(TextView)findViewById(R.id.textView_date);
DatePickerDialog dateDialog;
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay= c.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog.OnDateSetListener mDateSetListener = new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
DateDisplay.setText(new StringBuilder().append("Date : ").append(month + 1).append("-
").append(day).append("-").append(year));
} };
dateDialog = new DatePickerDialog(this , mDateSetListener,mYear,mMonth,mDay);
52
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

dateDialog.show(); }
public void onClickTimeDialog(View v){
final int mHour,mMinute;
final TextView TimeDisplay =(TextView)findViewById(R.id.textView_time);
TimePickerDialog timeDialog;
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute= c.get(Calendar.MINUTE);
TimePickerDialog.OnTimeSetListener mTimeSetListener = new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
TimeDisplay.setText(new StringBuilder().append("Time :
").append(hour).append(":").append(minute));
} };
timeDialog= new TimePickerDialog(this,mTimeSetListener,mHour,mMinute,true);
timeDialog.show();
}}
Practical 6c : Dialog Fragments
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dfragbutton"
android:text="DIALOG FRAGMENT"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/alertdfragbutton"
android:text="ALERT FRAGMENT"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This a dialog fragment"
android:layout_centerInParent="true"
android:padding="10dp"/>
</RelativeLayout>
Create a new layout dialogfragment.xml

In dialogfragment.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
54
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:text="This is dialog fragment"/>


</RelativeLayout>

Create new java Class:DFragment.java


Right Click on app->New->Java Class->Enter class name as “DFragment”->Click OK

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import com.example.dialogfragment.R;
public class DFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
View rootView=inflater.inflate(R.layout.dialogfragment,container,false);
getDialog().setTitle("DialogFragment Test");
return rootView;
}}

Create new java class :AlertDFragment.java


Right Click on app->New->Java Class->Enter class name as “AlertDFragment”->Click OK

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;
import android.content.DialogInterface;
public class AlertDFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
return new AlertDialog.Builder(getContext())
.setIcon(android.R.mipmap.sym_def_app_icon)
.setTitle("Alert DialogFragment")
.setMessage("Alert DialogFragment Tutorial")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(),"Ok Clicked",Toast.LENGTH_LONG).show();
} })
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext()," Cancel Clicked",Toast.LENGTH_LONG).show();
}
}).create(); }}

In mainactivity.java
package com.example.dialogfragment;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button dfragbutton;
Button alertdfragbutton;
FragmentManager fm=getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dfragbutton=(Button)findViewById(R.id.dfragbutton);
alertdfragbutton=(Button)findViewById(R.id.alertdfragbutton);
dfragbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DFragment dFragment=new DFragment();
dFragment.show(fm,"Dialog Fragment");
}
56
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

});
alertdfragbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDFragment alertdFragment=new AlertDFragment();
alertdFragment.show(fm,"Alert Dialog Fragment");
} });
}
}
Practical 7
Aim : Programs on Intents,Events,Listeners and Adapters
The Android Intent Class, Using Events and Event Listeners
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter a value:"
android:id="@+id/editText_text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/button_click"
android:layout_centerVertical="true"
android:onClick="showntext"/>
</LinearLayout>

Create a new layout


In newpage_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".newpage">
<TextView
58
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView_view"
android:textSize="20dp"/>
</android.support.constraint.ConstraintLayout>

Create new Empty Activity


In newpage.java activity
package com.example.intentclass;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class newpage extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newpage);
TextView tv1=(TextView)findViewById(R.id.textView_view);
String myvalue=getIntent().getExtras().getString("my key");
tv1.setText("Value = "+myvalue);
} }

In MainActivity.java
package com.example.intentclass;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void showntext(View view)
{
EditText ed=(EditText)findViewById(R.id.editText_text);
String msg=ed.getText().toString();
Intent in=new Intent(this,newpage.class);
in.putExtra("my key",msg);
startActivity(in);
} }
60
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Practical 8A
Aim : Program on Services
In AndroidManifest.xml

In Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStart"
android:text="Start Service"
android:onClick="startService"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStop"
android:onClick="stopService"
android:text="Stop Service"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnStart"
/>
</android.support.constraint.ConstraintLayout>

Create new Java Class

In MyServices.java
Extend this class with Service.Right Click and Select Generate to auto generate the methods
62
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Select Methods to Override

Make necessary changes in the self generated code.


In MyServices.java
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyServices extends Service {
@Override
public void onCreate() {
super.onCreate(); }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"Services Started..",Toast.LENGTH_LONG).show();
return START_STICKY; }
@Override
public void onDestroy() {
Toast.makeText(this,"Services Destroyed..",Toast.LENGTH_LONG).show(); }
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null; }}

In MainActivity.java
package com.example.androidservices;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void startService(View view) {
Intent intent=new Intent(this,MyServices.class);
startService(intent); }
public void stopService(View view) {
64
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Intent intent=new Intent(this,MyServices.class);


stopService(intent); }}

Enable Developer option to see running processes


Practical 8B
Aim : Program on Notification
In Activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/btn_noti"
android:onClick="displayNotification"
android:text="Notify"/>
</android.support.constraint.ConstraintLayout>

On click of button (btn_noti) a function displayNotification() will be called.


In MainActivity.java
package com.example.notification;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public final String CHANNEL_ID="personal_notification";
public final int NOTIFICATION_ID=001;
66
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void displayNotification(View view) {
createNotificationChannel();
Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_LONG).show();
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Simple Notification");
builder.setContentText("This is a test Notification");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat
notificationManagerCompat=NotificationManagerCompat.from(this);

notificationManagerCompat.notify(NOTIFICATION_ID,builder.build()); }

private void createNotificationChannel() {


if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
CharSequence name="Personal Notification";
String description="This is Description";
int importance=NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel=new
NotificationChannel(CHANNEL_ID,name,importance);
notificationChannel.setDescription(description);

NotificationManager
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel); } }}
Practical 8C
Aim : Programs on notification and broadcast receivers
In the AndroidManifest.xml
Add uses-permisson and receiver

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadccast">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<receiver android:name=".NumberReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
68
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

</application>
</manifest>

In the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+1 650-555-6789"
android:id="@+id/txtmsg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

In mainactivity.java
package com.example.broadccast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
private static MainActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance=this; }
public static MainActivity getInstance() {
return instance; }
public void myMethod(String str){
textView=(TextView)findViewById(R.id.txtmsg); }}

Create a new class NumberReceiver

In NumberReceiver.java
package com.example.broadccast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

public class NumberReceiver extends BroadcastReceiver {


static String allnumber;
@Override
public void onReceive(Context context, Intent intent){
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number=intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
allnumber=allnumber+"\n"+number;
MainActivity.getInstance().myMethod(allnumber); } }}
70
TYBscIT
Sem VI
Advanced Mobile Programming Practicals
Practical 9
Aim : Database Programming with SQLite
In acivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/luser"
android:layout_alignParentTop="true"
android:text="Username:"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/txtusername"
android:hint="Enter Username:"
android:inputType="textShortMessage" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lpass"
android:layout_marginTop="50dp"
72
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

android:text="Password:"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/txtpass"
android:hint="Enter Password:"
android:inputType="textShortMessage" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login"
android:layout_marginTop="50dp"
android:onClick="login_fun"
android:text="Login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/drop"
android:layout_marginTop="50dp"
android:onClick="drop_fun"
android:text="Drop"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/register"
android:layout_marginTop="50dp"
android:onClick="register_fun"
android:text="Register"/>
</LinearLayout>
In mainActivity.java
package com.example.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
class MyDB extends SQLiteOpenHelper{
MyDB(Context c){
super(c,"logindb",null,1); }
@Override
public void onCreate(SQLiteDatabase db) {
String str="create table login (username text , password text)";
db.execSQL(str); }
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
String dr="drop table if exists login";
db.execSQL(dr);
onCreate(db); }}

public class MainActivity extends AppCompatActivity {


MyDB mdb;
EditText userName,password;
Button bregister,bdrop,blogin;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName=(EditText)findViewById(R.id.txtusername);
password=(EditText)findViewById(R.id.txtpass);
blogin=(Button)findViewById(R.id.login);
bregister=(Button)findViewById(R.id.register);
bdrop=(Button)findViewById(R.id.drop);
mdb=new MyDB(this);
}
74
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

public void login_fun(View v) {


String un=String.valueOf(userName.getText());
String pas=String.valueOf(password.getText());
db=mdb.getReadableDatabase();
String q="Select * from login where username='"+un+"' and password='"+pas+"'";
try {
Cursor c=db.rawQuery(q,null);
if(c.getCount()==0) {
Toast.makeText(getApplicationContext(),"Username or password wrong or user doesn't
exists",Toast.LENGTH_SHORT).show();
}
else {
while (c.moveToNext()) {
String uname=c.getString(0);
String passw=c.getString(1);
Toast.makeText(getApplicationContext(),"Username:"+uname+"\n
Password:"+passw,Toast.LENGTH_LONG).show();
if(un.contentEquals(uname)&&pas.contentEquals(passw)){
Toast.makeText(getApplicationContext(),"Welcome
user:"+un,Toast.LENGTH_LONG).show();
c.close(); } } } }
catch (SQLiteException sqle) {
sqle.printStackTrace(); }
}
public void register_fun(View v){
String un=String.valueOf(userName.getText());
String pas=String.valueOf(password.getText());
db=mdb.getWritableDatabase();
String q="insert into login values('"+un+'"'+pas+"'";
db.execSQL(q);
Toast.makeText(getApplicationContext(),"User registered",Toast.LENGTH_LONG).show();
}
public void drop_fun(View v){
db=mdb.getWritableDatabase();
mdb.onUpgrade(db,1,2);
Toast.makeText(getApplicationContext(),"All Users Deleted",Toast.LENGTH_SHORT).show();
}
}
Practical 10-a
Aim : Programming threads.

In Acitivity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="4dip" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="startProgress" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

In ActivityMain.java
package com.megabytes.test.prac10a;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
76
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar bar;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
bar = (ProgressBar)findViewById(R.id.progressBar1); }
public void startProgress(View view){
bar.setProgress(0);
new Thread(new Task()).start();
new Thread(new Task2()).start(); }
class Task implements Runnable{
@Override
public void run() {
for(int i=0;i<=10;i++){
final int value = i;
try{
Thread.sleep(1000); }
catch (InterruptedException e){
e.printStackTrace(); }
bar.setProgress(value); } } }

class Task2 implements Runnable{


@Override
public void run() {
for(int i=0;i>=10;i--){
final int value = i;
try{ Thread.sleep(1000); }
catch (InterruptedException e){ e.printStackTrace(); }
textView.setText(String.valueOf(value));
}
}
}
}
Practical 10 b
In acivityMain.java
package com.example.Prac;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
private int stopLoop=30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView); }
public void timer(View view){
final Handler handler= new Handler();
handler.post(new Runnable() {
@Override
public void run() {
if(stopLoop>0){
stopLoop--;
textView.setText("Time: "+stopLoop);
handler.postDelayed(this,1000); }
else{ }
} }); }
public void nonTimer(View view){
int i=0;
for(i=0;i<3;i++){
textView.setText("Time: "+i);
try{
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
78
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

}
In Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:text="Count"
android:layout_width="234dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bStartTimer"
android:layout_width="236dp"
android:layout_height="wrap_content"
android:text="Start Timer Count"
android:onClick="timer"/>
<Button
android:id="@+id/bStartNonTimer"
android:layout_width="234dp"
android:layout_height="wrap_content"
android:text="Start Non Timer Count"
android:onClick="nonTimer"
/>
</LinearLayout>
Practical 10 c
In mainActivity.java
package com.example.prac;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyTask myTask = new MyTask(MainActivity.this,textView,button);
myTask.execute();
button.setEnabled(false);
} }); }
}
Create a new class.
In MyTask.java

package com.megabytes.test.prac10c;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Button;
import android.widget.TextView;
public class MyTask extends AsyncTask {
80
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Context context;
Button button;
TextView textView;
ProgressDialog progressDialog;
MyTask(Context context, TextView textView, Button button) {
this.context = context;
this.textView = textView;
this.button = button; }
@Override
protected Object doInBackground(Object[] object) {
int i=0;
synchronized (this) {
while((i<10)){
try{
wait(1000);
i++;
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace(); } } }
return "Download Finish"; }

protected void onPreExecute(){


progressDialog=new ProgressDialog(context);
progressDialog.setTitle("Download in progress");
progressDialog.setMax(10);
progressDialog.setProgressStyle(0);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show(); }
protected void onPostExecute(String result){
textView.setText(result);
button.setEnabled(true); }
protected void onProgressUpdate(Integer... values){
int progress = values[0];
progressDialog.setProgress(progress);
textView.setText("Download in Progress"); }

}
In Acivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.31" />
</android.support.constraint.ConstraintLayout>
82
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Practical No. 11a


Programming Media API

Code:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.youtube.player.YouTubePlayerView>
</android.support.constraint.ConstraintLayout>

MainActivity.java
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;

public class MainActivity extends AppCompatActivity {


YouTubePlayerView playerView;
String API_KEY="AIzaSyClwDm2pjTvU85sDZNcCfXDoLF_QPmPP0Q";
String Code="p3HR9QDMj18";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView=(YouTubePlayerView)findViewById(R.id.player);
playerView.initialize(API_KEY,new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer
youTubePlayer, boolean b) {
if (!b) {
youTubePlayer.cueVideo(Code);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult
youTubeInitializationResult) {
Toast.makeText(getApplicationContext(), "HI", Toast.LENGTH_SHORT).show();
}
});
} }
84
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

Practical 11b
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.373" />
</android.support.constraint.ConstraintLayout>

In MainAcitvity.java
package com.example.prac;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callintent=new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse("tel:5556"));
if(ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
return; }
startActivity(callintent); } }); } }

In Android_MAnifext.xml
Give Call_Phone permission.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prac">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
86
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

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>

Output:
Practical no. 12
1. Download the YouTube Android Player Api.
2. Copy – Paste it in the libs folder. App>libs>YouTubeApi.jar

3. Go to console.developers.google.com
Login to Your Google Account
Click on Add Project.
Click on Credentials.

4. Go to the Gradle Panel.


Run the Prac>app>signingReport file to create SHA1 fingerprint.
88
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

5. Enter the SHA! Fingerprint of your project and the packagae name of the project. And save
it.

In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/player"
android:layout_height="match_parent"
android:layout_width="match_parent" >
</com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>

In mainAcitvity.java
package com.megabytes.test.prac12;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
public class MainActivity extends AppCompatActivity {
YouTubePlayerView playerView;
String API_KEY="AIzaSyDO7F_Zo-3Rrm9fJdsNsjnxE6fopVb9rLs";
String code="p3HR9QDMj18";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView=(YouTubePlayerView)findViewById(R.id.player);
playerView.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer
youTubePlayer, boolean b) {
youTubePlayer.cueVideo(code); }
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(getApplicationContext(),"HI",Toast.LENGTH_LONG).show();
} }); }}

In Android_Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prac">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
90
TYBscIT
Sem VI
Advanced Mobile Programming Practicals

<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Вам также может понравиться