AMP Main PDF
AMP Main PDF
AMP Main PDF
This is the main activity. When you build and run the app, the system launches an instances of this
activity and loads its layout.
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.
Output:-
Practical-2: Programming Resources Android Resources: (Color, Theme, String, Drawable, Dimension,
Image).
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 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
Click ok
see Drawable
<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.
PRACTICAL-3B
14
TYBscIT
Sem VI
Advanced Mobile Programming Practicals
Right click in the .java file and select generate and override methods.
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
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
18
TYBscIT
Sem VI
Advanced Mobile Programming Practicals
Create Activities
Right Click on app -> New -> Activity-> Empty Activity
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>
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
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>
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
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;
@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;
}
}
}
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;
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
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>
In ActivityMain.java
package com.example.appbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
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;
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
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;
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;
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
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;
}}
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;
});
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>
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>
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;
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>
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
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
@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()); }
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
</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); }}
In NumberReceiver.java
package com.example.broadccast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
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); }}
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); } } }
}
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"; }
}
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
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;
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.
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