0% found this document useful (0 votes)
6 views191 pages

MAD - Exp 1 To 32 - Removed - Removed

The document provides a series of practical exercises and code examples related to Android layout managers, including GridLayout and TableLayout, as well as user input handling with EditText. It includes XML configurations for AndroidManifest and layout files, along with Java code for MainActivity to display student information and handle user login. The conclusion highlights the successful implementation of various layout types in Android development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views191 pages

MAD - Exp 1 To 32 - Removed - Removed

The document provides a series of practical exercises and code examples related to Android layout managers, including GridLayout and TableLayout, as well as user input handling with EditText. It includes XML configurations for AndroidManifest and layout files, along with Java code for MainActivity to display student information and handle user login. The conclusion highlights the successful implementation of various layout types in Android development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 191

Tejas Vaidya 1859

Practical Related Questions:

1. List different attributes which can be used by any layout manager.

Ans:
Different attributes which can be used by any layout manager are-

∑ android:id
∑ android:layout_width
∑ android:layout_height
∑ android:layout_marginTop
∑ android:layout_marginBottom
∑ android:layout_marginLeft
∑ android:layout_marginRight
∑ android:layout_gravity
∑ android:layout_weight

2. What is grid layout?

Ans:

The GridLayout is a layout manager that lays out a container's components in a


rectangular grid. The container is divided into equal-sized rectangles, and one
component is placed in each rectangle.

The grid is composed of a set of infinitely thin lines that separate the viewing
area into cells. Throughout the API, grid lines are referenced by grid indices. A
grid with N columns has N + 1 grid indices that run from 0 through N inclusive.
Regardless of how GridLayout is configured, grid index 0 is fixed to the leading
edge of the container and grid index N is fixed to its trailing edge (after padding
is taken into account).
Tejas Vaidya 1859

Exercise:
1. Write a program to display 10 students basic information in a table from
using TableLayout.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp6_1">
<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/Theme.Exp6_1">
<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>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@color/black">

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/table_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>

</HorizontalScrollView>
</ScrollView>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp6_1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private String[]
fname={"Tejas","Karan","Aniket","Rishi","Jeet","Avesh","Gaurav","Abhishek","Pratik
","Mrunal"};
private String[]
mname={"Devendra","Chandrashekhar","Vijay","Manohar","Sanjay","Ansarsab","Arun","A
shok","Kunal","Uttam"};
private String[]
lname={"Vaidya","Kadne","Chavan","Motiray","Rane","Momin","Telange","Goykar","Tive
rekar","Kandizod"};
private String[]
enroll={"18009334","18009341","18009344","18009345","18009349","18009343","1800932
1","18009339","18009347","18009353"};
private String[] course={"CO","CO","CO","CO","CO","CO","CO","CO","CO","CO"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

public void init() {


TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("First Name");
tv0.setTextColor(Color.RED);
tv0.setGravity(Gravity.CENTER);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText("Middle Name");
tv1.setTextColor(Color.RED);
tv1.setGravity(Gravity.CENTER);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText("Last Name");
tv2.setTextColor(Color.RED);
tv2.setGravity(Gravity.CENTER);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText("Enrollment No.");
tv3.setTextColor(Color.RED);
tv3.setGravity(Gravity.CENTER);
tbrow0.addView(tv3);
Tejas Vaidya 1859

TextView tv4 = new TextView(this);


tv4.setText("Course");
tv4.setTextColor(Color.RED);
tv4.setGravity(Gravity.CENTER);
tbrow0.addView(tv4);
stk.addView(tbrow0);
for (int i = 0; i < 10; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText(fname[i]);
t1v.setTextColor(Color.WHITE);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText(mname[i]);
t2v.setTextColor(Color.WHITE);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText(lname[i]);
t3v.setTextColor(Color.WHITE);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText(enroll[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
TextView t5v = new TextView(this);
t5v.setText(course[i]);
t5v.setTextColor(Color.WHITE);
t5v.setGravity(Gravity.CENTER);
tbrow.addView(t5v);
stk.addView(tbrow);
}

}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. Write a program, to display all the data types in object-oriented


programming in Frame Layout.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp6_2">

<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/Theme.Exp6_2">
<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>

activity_main.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="wrap_content"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="66dp"
android:text="Data Types in Object Oriented Programming"
android:textColor="#FF0000"
android:textSize="25dp"
android:textStyle="bold"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left|fill_vertical"
android:layout_marginTop="80dp"
android:text="Primitive"
android:textColor="@color/purple_700"
android:textStyle="bold"
android:textSize="20dp"
/>
Tejas Vaidya 1859

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:foregroundGravity="fill_horizontal|top"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1) Integer"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2) Float"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3) Characters"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4) Boolean"
/>

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|fill_vertical"
android:layout_marginTop="80dp"
android:text="Non-Primitive"
android:textColor="@color/purple_700"
android:textStyle="bold"
android:textSize="20dp" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="110dp"
android:layout_marginRight="20dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1) Class"
/>
Tejas Vaidya 1859

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2) Array"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3) Interface"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4) Object"
/>

</LinearLayout>

</FrameLayout>

MainActivity.java:

package com.example.exp6_2;

import androidx.appcompat.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);
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement frame


layout, table layout and relative layout.

Reference: www.javatpoint.com
Tejas Vaidya 1859

Exercise:
1. Write a program to accept username and password from the end user using Text
View and Edit Text.

AndroidManifest.xml:

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment7_1">

<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/Theme.Experiment7_1">
<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>

strings.xml:

<resources>
<string name="app_name">Experiment7_1</string>
<string name="uname">Username</string>
<string name="pass">Password</string>
<string name="login">Login</string>
</resources>

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"
android:padding="20dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Tejas Vaidya 1859

android:text="@string/login"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="35dp"
android:textAppearance="?android:textAppearanceLarge"/>

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/uname"
android:inputType="textPersonName"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"/>

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/pass"
android:importantForAutofill="no"
android:inputType="textPassword"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="175dp" />

<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="250dp"
android:text="@string/login" />

<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnlogin"
android:paddingStart="20dp"
android:paddingTop="20dp"
android:paddingEnd="20dp"
android:paddingBottom="20dp"
android:textColor="@android:color/black" />
</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java
package com.example.experiment7_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText editTextName;
Button btnlogin;
TextView textName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextName = (EditText) findViewById(R.id.editTextName);


btnlogin = (Button) findViewById(R.id.btnlogin);
textName = (TextView) findViewById(R.id.textName);

btnlogin.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
textName.setText("Hi " + name);
}
});

}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. Write a program to accept and display personal information of the student.

AndroidManifest.xml:

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment7_2" >

<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/Theme.Experiment7_2" >
<activity android:name=".MainActivity2" >
</activity>
<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>

strings.xml:
<resources>
<string name="app_name">Experiment7_2</string>
<string name="name">Name</string>
<string name="rollno">Roll No</string>
<string name="submit">Submit</string>
</resources>

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"
android:padding="20dp"
android:gravity="center"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Tejas Vaidya 1859

android:ems="10"
android:hint="@string/name"
android:inputType="textPersonName"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"/>

<EditText
android:id="@+id/editTextRollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/rollno"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="75dp" />

<Button
android:id="@+id/btnsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="150dp"
android:text="@string/submit" />

</RelativeLayout>

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

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textAppearance="?android:textAppearanceLarge"/>
Tejas Vaidya 1859

<TextView
android:id="@+id/rollno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/rollno"
android:textAppearance="?android:textAppearanceLarge"/>

</LinearLayout>

MainActivity.java:
package com.example.experiment7_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText editTextName, editTextRollno;


Button btnsubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextName = (EditText) findViewById(R.id.editTextName);


editTextRollno = (EditText) findViewById(R.id.editTextRollno);
btnsubmit = (Button) findViewById(R.id.btnsubmit);

btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String name= editTextName.getText().toString();


String rollno= editTextRollno.getText().toString();

Toast.makeText(MainActivity.this,"Name:"+name+"Roll No:"+rollno+"
is added",Toast.LENGTH_LONG).show();

Intent intent = new Intent(MainActivity.this,MainActivity2.class);


intent.putExtra("keyname",name);
intent.putExtra("keyrollno",rollno);
startActivity(intent);
}
});
}
}
Tejas Vaidya 1859

MainActivity2.java:
package com.example.experiment7_2;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

private TextView n,r;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

n = (TextView)findViewById(R.id.name);
r = (TextView)findViewById(R.id.rollno);

String name = getIntent().getStringExtra("keyname");


String rollno = getIntent().getStringExtra("keyrollno");

n.setText(name);
r.setText(rollno);

}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed program to implement TextView & EditText.

Reference: www.geeksforgeeks.com

Android Book by Prasanna Dixit


Tejas Vaidya 1859

Exercise:
1. Write a program to create a first display screen of any search engine using
AutoComplete Text View.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment8_1">

<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/Theme.Experiment8_1">
<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>

strings.xml:
<resources>
<string name="app_name">Experiment8_1</string>
<string name="heading">Six Semester Subjects</string>
</resources>

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"
android:padding="20dp"
tools:context=".MainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo" />
Tejas Vaidya 1859

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="200dp"
android:hint="Search"
android:text=""
android:drawableLeft="@drawable/search"/>

</RelativeLayout>

MainActivity.java
package com.example.experiment8_1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

String[] Anime
={"Lelouch","C.C","Dazai","Naruto","Tanjiro","Asta","Hikigaya","Eren","Deku",
"Shiore","Miku","Kirito","Kilua","Levi","Zenistu","Soma","Miyamura","Hori",
"Ash","Sung-jin-woo"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayAdapter<String> adapter = new ArrayAdapter<String>


(this,android.R.layout.select_dialog_item,Anime);

AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);
actv.setAdapter(adapter);
actv.setTextColor(Color.RED);

}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. Write a program to display all the subjects of sixth semester using Auto Complete
Text View.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment8_2">

<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/Theme.Experiment8_2">
<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>

strings.xml:
<resources>
<string name="app_name">Experiment8_2</string>
<string name="heading">Six Semester Subjects</string>
</resources>

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"
android:padding="20dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heading"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="35dp"
android:textAppearance="?android:textAppearanceLarge"/>
Tejas Vaidya 1859

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="175dp"
android:text="" />

</RelativeLayout>

MainActivity.java:
package com.example.experiment8_2;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

String[] subjects ={"Management","Programming with Python","Mobile Application


Development","Emerging Trends in Computer and Information","Web Based Application
Development Using PHP","Enterpreneurship Development","Capstone Project"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayAdapter<String> adapter = new ArrayAdapter<String>


(this,android.R.layout.select_dialog_item,subjects);

AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);
actv.setAdapter(adapter);
actv.setTextColor(Color.RED);

}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed program to implement Auto Complete Text View.

Reference: www.geeksforgeeks.com

Android Book by Prasanna Dixi


Exercise:
1. Write a program to create a toggle button to display ON/OFF Bluetooth
on the display Screen.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp9_1">

<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/Theme.Exp9_1">
<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>

selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/bluetooth_on"/>
<item android:state_checked="false"
android:drawable="@drawable/bluetooth_off"/>
</selector>

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"
android:gravity="center"
tools:context=".MainActivity">

<ImageView
android:id="@+id/i1"
android:layout_width="250dp"
android:layout_height="250dp"
/>

1
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginTop="30dp"
android:drawableStart="@drawable/selector"
/>

</LinearLayout>

MainActivity.java:
package com.example.exp9_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

ToggleButton toggle;
ImageView i1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toggle=findViewById(R.id.toggle);
i1=findViewById(R.id.i1);

i1.setImageDrawable(getResources().getDrawable(R.drawable.bluetooth_off));

toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String status = "Bluetooth : " + toggle.getText();


Toast.makeText(getApplicationContext(), status,
Toast.LENGTH_SHORT).show();

if(toggle.isChecked()){

i1.setImageDrawable(getResources().getDrawable(R.drawable.bluetooth_on));
}else{

i1.setImageDrawable(getResources().getDrawable(R.drawable.bluetooth_off));
}

}
});

}
}

2
Output:

3
2. Write a program to create a simple calculator.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp9_2">

<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/Theme.Exp9_2">
<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>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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:layout_marginTop="30dp"
android:layout_gravity="center"
android:text="Calculator"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#0090FF"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="380dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:orientation="vertical">

4
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeColor="#2196F3"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF">

<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="Enter First Number" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
app:boxStrokeColor="#2196F3"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF">

<EditText
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="Enter Second Number" />

</com.google.android.material.textfield.TextInputLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/orange"
android:text="+"
android:textSize="25sp"
/>

<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

5
android:layout_marginLeft="90dp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/orange"
android:text="-"
android:textSize="25sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/orange"
android:text="*"
android:textSize="25sp"/>

<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/orange"
android:text="/"
android:textSize="25sp"/>

</LinearLayout>

</LinearLayout>

<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/orangedark" />

</LinearLayout>

MainActivity.java:
package com.example.exp9_2;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

6
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText ed1,ed2;


private TextView t1;
private Button add,sub,mul,div;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.t1);
ed1=findViewById(R.id.ed1);
ed2=findViewById(R.id.ed2);
add=findViewById(R.id.b1);
sub=findViewById(R.id.b2);
mul=findViewById(R.id.b3);
div=findViewById(R.id.b4);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(ed1.getText().toString().equals("")||ed2.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"Please Enter
Number",Toast.LENGTH_SHORT).show();
}
else
{
ed1.getText().toString();
ed2.getText().toString();
double a1=Double.valueOf(ed1.getText().toString());
double a2=Double.valueOf(ed2.getText().toString());
double a3;
a3=a1+a2;
t1.setText("Addition: "+String.valueOf(a3));
}
}
});

sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(ed1.getText().toString().equals("")||ed2.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"Please Enter
Number",Toast.LENGTH_SHORT).show();
}
else
{
ed1.getText().toString();
ed2.getText().toString();
double a1=Double.valueOf(ed1.getText().toString());
double a2=Double.valueOf(ed2.getText().toString());
double a3;

7
a3=a1-a2;
t1.setText("Subtraction: "+String.valueOf(a3));
}
}
});

mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(ed1.getText().toString().equals("")||ed2.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"Please Enter
Number",Toast.LENGTH_SHORT).show();
}
else
{
ed1.getText().toString();
ed2.getText().toString();
double a1=Double.valueOf(ed1.getText().toString());
double a2=Double.valueOf(ed2.getText().toString());
double a3;
a3=a1*a2;
t1.setText("Multiplication: "+String.valueOf(a3));
}
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(ed1.getText().toString().equals("")||ed2.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"Please Enter
Number",Toast.LENGTH_SHORT).show();
}
else
{
ed1.getText().toString();
ed2.getText().toString();
double a1=Double.valueOf(ed1.getText().toString());
double a2=Double.valueOf(ed2.getText().toString());
double a3;
a3=a1/a2;
t1.setText("Division: "+String.valueOf(a3));
}
}
});
}
}

8
Output:

Conclusion: We successfully developed program to implement Button, Image


button & Toggle Button

Reference: www.geeksforgeeks.com

9
Tejas Vaidya 1859

Exercise:

1. Write a program to create a login form for a social networking site.


AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment10_1">

<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/Theme.Experiment10_1">
<activity android:name=".MainActivity3"></activity>
<activity android:name=".MainActivity2" />
<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>

login_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorAccent"
android:width="1dp"/>

<solid
android:color="@color/colorAccent"/>
</shape>
</item>

<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorAccentDark"
android:width="1dp"/>

<solid
android:color="@color/colorAccentDark"/>
</shape>
</item>
</selector>
Tejas Vaidya 1859

Rounded_login_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlue"
android:width="1dp"/>

<solid
android:color="@color/colorBlue"/>

<corners android:radius="24dp" />


</shape>

</item>

<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlueDark"
android:width="1dp"/>

<solid
android:color="@color/colorBlueDark"/>

<corners android:radius="24dp" />


</shape>
</item>
</selector>

sign_up_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlue"
android:width="1dp"/>

<solid android:color="@color/colorBlue"/>
</shape>
</item>

<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlueDark"
android:width="1dp"/>

<solid android:color="@color/colorBlueDark"/>
</shape>
</item>
</selector>
Tejas Vaidya 1859

strings.xml:
<resources>
<string name="app_name">Experiment10_1</string>
<string name="login">LOG IN</string>
<string name="signup">SIGN UP</string>
<string name="login_title">Log in to Snapchat</string>
<string name="login_form_username">Username</string>
<string name="login_form_password">Password</string>
<string name="welcome">Welcome to Snapchat</string>
</resources>

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"
android:background="@color/colorPrimary"
tools:context=".MainActivity">

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:weightSum="2">

<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/login"
android:onClick="onClick"
android:textColor="@color/colorWhite"
android:background="@drawable/login_button"
android:layout_weight="1"/>
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/signup"
android:textColor="@color/colorWhite"
android:background="@drawable/sign_up_button"
android:layout_weight="1"/>

</LinearLayout>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.experiment10_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.b1);
}

public void onClick(View view) {


Intent i1 = new Intent(this,MainActivity2.class);
startActivity(i1);
}
}

activity_main2.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"
android:orientation="vertical"
android:background="@color/colorPrimary"
tools:context=".MainActivity2">

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="150dp"
>
Tejas Vaidya 1859

<androidx.cardview.widget.CardView
android:layout_margin="64dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
app:cardCornerRadius="15dp"
app:cardMaxElevation="12dp"
android:background="#fff">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/login_title"
android:textAlignment="center"
android:textColor="@color/colorBlack"
android:textSize="25sp"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/login_form_username"
android:textSize="20sp" />

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Enter Usename"
android:inputType="text" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/login_form_password"
android:textSize="20sp" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Enter Password"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
Tejas Vaidya 1859

<Button
android:id="@+id/activity_main_loginButton"
android:layout_width="200dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:onClick="login"
android:background="@drawable/rounded_login_button"
android:text="@string/login"
android:textColor="@color/colorWhite"
android:layout_marginTop="20dp"
android:textSize="18sp"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

</RelativeLayout>

MainActivity2.java:
package com.example.experiment10_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}

public void login(View view) {

TextView uid = (TextView)findViewById(R.id.e1);


TextView upass = (TextView)findViewById(R.id.e2);

String id = uid.getText().toString();
String pass = upass.getText().toString();

if(id.equals("Tejas") && pass.equals("1859"))


{
Intent i2 = new Intent(this,MainActivity3.class);
i2.putExtra("id",uid.getText().toString());
startActivity(i2);
}
Tejas Vaidya 1859

else
{
Toast.makeText(this,"Invalid Username and
Password",Toast.LENGTH_SHORT).show();
}

}
}

activity_main3.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"
android:background="@color/colorPrimary"
tools:context=".MainActivity3">

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/colorBlack"
android:textStyle="bold"/>

<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/colorBlack"
android:textStyle="bold"/>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity3.java:
package com.example.experiment10_1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity3 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);

TextView t3 = (TextView)findViewById(R.id.t3);

Bundle b = getIntent().getExtras();
String data_receive= b.getString("id");
t3.setText("Hi "+data_receive.toUpperCase());

}
}

Output:
Tejas Vaidya 1859
Tejas Vaidya 1859

2. Write a program to create a login form for student registration system.


AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment10_2">

<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/Theme.Experiment10_2">
<activity android:name=".MainActivity2"></activity>
<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>

register_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke
android:color="@color/purple_500"
android:width="1dp"/>

<solid
android:color="@color/purple_500"/>

<corners android:radius="24dp" />


</shape>
</item>

<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/purple_700"
android:width="1dp"/>

<solid
android:color="@color/purple_700"/>

<corners android:radius="24dp" />


</shape>
</item>
</selector>
Tejas Vaidya 1859

activity_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"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="@string/h1"
android:gravity="center"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="#FF1100"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="107dp"
app:boxStrokeColor="#2196F3"
app:helperText="Required*"
app:helperTextTextColor="#F80000"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF">

<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_marginTop="40dp"
android:hint="Enter Name" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="107dp"
app:boxStrokeColor="#2196F3"
app:helperText="Required*"
app:helperTextTextColor="#F80000"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF"
>
Tejas Vaidya 1859

<EditText
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_marginTop="40dp"
android:hint="Enter Roll No" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout3"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="107dp"
app:boxStrokeColor="#2196F3"
app:helperText="Required*"
app:helperTextTextColor="#F80000"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF"
>

<EditText
android:id="@+id/ed3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_marginTop="40dp"
android:hint="Enter Enrollment No" />

</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

<Button
android:id="@+id/b1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:height="60dp"
android:text="@string/register"
android:background="@drawable/register_btn"
app:icon="@drawable/register"
app:iconGravity="textStart"
app:iconTint="#FFFFFF" />

</LinearLayout>

strings.xml:
<resources>
<string name="app_name">Experiment10_2</string>
<string name="h1">Student Registration Form</string>
<string name="register">Register</string>
</resources>
Tejas Vaidya 1859

MainActivity.java:
package com.example.experiment10_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText ed1,ed2,ed3;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText ed1 = findViewById(R.id.ed1);


EditText ed2 = findViewById(R.id.ed2);
EditText ed3 = findViewById(R.id.ed3);
Button b1= findViewById(R.id.b1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (TextUtils.isEmpty(ed1.getText().toString())){
ed1.setError("Name is Required");
}
if (TextUtils.isEmpty(ed2.getText().toString())){
ed2.setError("Roll No is Required");
}
if (TextUtils.isEmpty(ed3.getText().toString())){
ed3.setError("Enrollment No is Required");
}
else {

Toast.makeText(MainActivity.this,"Proceed..",Toast.LENGTH_SHORT).show();
Intent i1 = new Intent(MainActivity.this,
MainActivity2.class);
i1.putExtra("name", ed1.getText().toString());
i1.putExtra("rollno", ed2.getText().toString());
i1.putExtra("enroll", ed3.getText().toString());
startActivity(i1);
}
}
});
}
}
Tejas Vaidya 1859

activity_main2.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"
android:gravity="center"
tools:context=".MainActivity2">

<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:textStyle="bold"
android:textSize="20sp"
/>

<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:text="RollNo"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:textStyle="bold"
android:textSize="20sp"
/>

<TextView
android:id="@+id/t3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"

android:gravity="center"
android:text="Enrollment No"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:textStyle="bold"
android:textSize="20sp"
/>

</LinearLayout>
Tejas Vaidya 1859

MainActivity2.java:
package com.example.experiment10_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

TextView t1,t2,t3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

TextView t1=(TextView)findViewById(R.id.t1);
TextView t2=(TextView)findViewById(R.id.t2);
TextView t3=(TextView)findViewById(R.id.t3);

Intent iGet = getIntent();


t1.setText(iGet.getStringExtra("name"));
t2.setText(iGet.getStringExtra("rollno"));
t3.setText(iGet.getStringExtra("enroll"));

}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859
Tejas Vaidya 1859

Practical Related Questions:

1. Name the different methods of Checkbox.


Ans: There are many inherited methods of View, TextView, and Button classes
in the CheckBox class. Some of them are as follows:

Method Description

public boolean isChecked() Returns true if it is checked otherwise false.


public void Changes the state of the CheckBox.
setChecked(boolean status)

2. List different attributes of Checkbox.


Ans:

Sr.No Attribute & Description

android:autoText
1 If set, specifies that this TextView has a textual input method and automatically
corrects some common spelling errors.

android:drawableBottom
2
This is the drawable to be drawn below the text.

android:drawableRight
3
This is the drawable to be drawn to the right of the text.

android:editable
4
If set, specifies that this TextView has an input method.

android:text
5
This is the Text to display.

6 android:background
This is a drawable to use as the background.

android:contentDescription
7
This defines text that briefly describes content of the view.
Tejas Vaidya 1859

android:id
8
This supplies an identifier name for this view.

android:onClick
9 This is the name of the method in this View's context to invoke when the view is
clicked.

android:visibility
10
This controls the initial visibility of the view.

3. Write xml tag to create a checkbox named “Android”.


Ans:
<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"/>
Tejas Vaidya 1859

Exercise:
1. Write a program to show 5 checkboxes and toast selected checkboxes.

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp11">

<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/Theme.Exp11">
<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>

activity_main.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:textColor="#f00"
android:textSize="24sp"
android:textStyle="bold"
/>

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="vertical">
Tejas Vaidya 1859

<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/c"
android:textColor="@color/purple_700"
android:textSize="20sp" />

<CheckBox
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/c_plus"
android:textColor="@color/purple_700"
android:textSize="20sp"/>

<CheckBox
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/java"
android:textColor="@color/purple_700"
android:textSize="20sp"/>

<CheckBox
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/python"
android:textColor="@color/purple_700"
android:textSize="20sp" />

<CheckBox
android:id="@+id/c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/kotlin"
android:textColor="@color/purple_700"
android:textSize="20sp" />

</LinearLayout>

</RelativeLayout>
Tejas Vaidya 1859

strings.xml:
<resources>
<string name="app_name">Exp11</string>
<string name="c">C</string>
<string name="c_plus">C++</string>
<string name="java">JAVA</string>
<string name="python">PYTHON</string>
<string name="kotlin">KOTLIN</string>
<string name="title">Select Your Programming language:</string>
</resources>

MainActivity.java:
package com.example.exp11;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {

CheckBox c,c_plus,java,python,kotlin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

c = (CheckBox) findViewById(R.id.c1);
c.setOnClickListener(this);
c_plus = (CheckBox) findViewById(R.id.c2);
c_plus.setOnClickListener(this);
java = (CheckBox) findViewById(R.id.c3);
java.setOnClickListener(this);
python = (CheckBox) findViewById(R.id.c4);
python.setOnClickListener(this);
kotlin = (CheckBox) findViewById(R.id.c5);
kotlin.setOnClickListener(this);
}

@Override
public void onClick(View view) {

switch (view.getId()) {
case R.id.c1:
if (c.isChecked())
Toast.makeText(getApplicationContext(), "C is Selected",
Toast.LENGTH_LONG).show();
break;
case R.id.c2:
if (c_plus.isChecked())
Toast.makeText(getApplicationContext(), "C++ is Selected",
Tejas Vaidya 1859

Toast.LENGTH_LONG).show();
break;
case R.id.c3:
if (java.isChecked())
Toast.makeText(getApplicationContext(), "JAVA is Selected",
Toast.LENGTH_LONG).show();
break;
case R.id.c4:
if (python.isChecked())
Toast.makeText(getApplicationContext(), "PYTHON is Selected",
Toast.LENGTH_LONG).show();
break;
case R.id.c5:
if (kotlin.isChecked())
Toast.makeText(getApplicationContext(), "KOTLIN is Selected",
Toast.LENGTH_LONG).show();
break;
}
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement CheckBox.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:


1. Write xml tag to create a Radio button.
Ans:

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Female"
android:layout_marginTop="20dp"
android:checked="false"
android:textSize="20dp" />

2. Write the purpose of Radio Button.


Ans:
∑ RadioButton is a two states button which is either checked or unchecked.
If a single radio button is unchecked, we can click it to make checked
radio button. Once a radio button is checked, it cannot be marked as
unchecked by user.
∑ RadioButton is generally used with RadioGroup. RadioGroup contains
several radio buttons, marking one radio button as checked makes all
other radio buttons as unchecked.

3. List Different methods of Radio Button.

Ans:

1. check(id)
2. clearCheck( )
3. getCheckedRadioButtonId( )
4. setOnCheckedChangeListener
Tejas Vaidya 1859

Exercise:
1. Write a program to show the following output. First two radio buttons
are without using radio button group and next two radio buttons are
using radio group. Note the change between these two. Also toast which
button has been selected.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp12">

<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/Theme.Exp12">
<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>

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

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:textSize="22dp"
android:text="Single Radio Buttons" />

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
Tejas Vaidya 1859

android:text="Radio Button 1"


android:layout_marginTop="20dp"
android:textSize="20dp" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:layout_marginTop="10dp"
android:textSize="20dp" />

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#B8B894" />

<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:textSize="22dp"
android:text="Radio button inside RadioGroup" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">

<RadioButton
android:id="@+id/radioMale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Male"
android:layout_marginTop="10dp"
android:checked="false"
android:textSize="20dp" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Female"
android:layout_marginTop="20dp"
android:checked="false"
android:textSize="20dp" />
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:id="@+id/button"
android:onClick="onclickbuttonMethod"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp12;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button button;
RadioButton genderradioButton;
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
}
public void onclickbuttonMethod(View v){
int selectedId = radioGroup.getCheckedRadioButtonId();
genderradioButton = (RadioButton) findViewById(selectedId);
if(selectedId==-1){
Toast.makeText(MainActivity.this,"Nothing selected",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,genderradioButton.getText()+" is
Selected", Toast.LENGTH_SHORT).show();
}

}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement Radio Button


and Radio Group.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. State different methods to update the percentage of progress displayed.


Ans:
You can update the percentage of progress displayed by using the
setProgress(int) method, or by calling incrementProgressBy(int) to increase
the current progress completed by a specified amount. By default, the progress
bar is full when the progress value reaches 100. You can adjust this default by
setting the android:max attribute.

2. Write an xml tag for the determinate progress bar.


Ans:

<ProgressBar
android:id="@+id/circularProgressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="50" />

3. List different progress bar styles provided by the system.


Ans:

1) Widget.ProgressBar.Horizontal
2) Widget.ProgressBar.Small
3) Widget.ProgressBar.Large
4) Widget.ProgressBar.Inverse
5) Widget.ProgressBar.Small.Inverse
6) Widget.ProgressBar.Large.Inverse
Tejas Vaidya 1859

Exercise:
1. Write a program to display circular progress bar.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp13_1">

<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/Theme.Exp13_1">
<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>

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

<ProgressBar
android:id="@+id/circularProgressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="300dp"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:layout_centerInParent="true"
android:progressDrawable="@drawable/circular"
android:secondaryProgress="100" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/whitecircle"
android:layout_centerInParent="true"/>
Tejas Vaidya 1859

<TextView
android:id="@+id/t1"
android:layout_width="250dp"
android:layout_height="250dp"
android:gravity="center"
android:text="25%"
android:layout_centerInParent="true"
android:textColor="@color/purple_700"
android:textSize="20sp" />

</RelativeLayout>

circular.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/secondaryProgress">
<shape
android:innerRadiusRatio="6"
android:shape="ring"
android:thicknessRatio="20.0"
android:useLevel="true">

<gradient
android:centerColor="#999999"
android:endColor="#999999"
android:startColor="#999999"
android:type="sweep" />
</shape>
</item>

<item android:id="@android:id/progress">
<rotate
android:fromDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270">

<shape
android:innerRadiusRatio="6"
android:shape="ring"
android:thicknessRatio="20.0"
android:useLevel="true">
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<gradient
android:centerColor="#00FF00"
android:endColor="#00FF00"
android:startColor="#00FF00"
android:type="sweep" />
</shape>
</rotate>
</item>
</layer-list>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp13_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar;


private TextView progressText;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressBar = findViewById(R.id.circularProgressbar);
progressText = findViewById(R.id.t1);

final Handler handler = new Handler();


handler.postDelayed(new Runnable() {
@Override
public void run() {
if (i <= 100) {
progressText.setText("" + i);
progressBar.setProgress(i);
i++;
handler.postDelayed(this, 200);
} else {
handler.removeCallbacks(this);
}
}
}, 200);
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. Write a program to show the following output.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp13_2">

<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/Theme.Exp13_2">
<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>

strings.xml:
<resources>
<string name="app_name">Exp13_2</string>
<string name="b1">download file</string>
</resources>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp13_2;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
btnStartProgress = findViewById(R.id.button);
btnStartProgress.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {

progressBar = new ProgressDialog(v.getContext());


progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();

progressBarStatus = 0;
fileSize = 0;

new Thread(new Runnable() {


public void run() {
while (progressBarStatus < 100) {

progressBarStatus = doOperation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
Tejas Vaidya 1859

if (progressBarStatus >= 100) {


try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
}
});
}

public int doOperation() {

while (fileSize <= 10000) {


fileSize++;
if (fileSize == 1000) {
return 10;
} else if (fileSize == 2000) {
return 20;
} else if (fileSize == 3000) {
return 30;
} else if (fileSize == 4000) {
return 40;
} else if (fileSize == 5000) {
return 50;
} else if (fileSize == 6000) {
return 60;
} else if (fileSize == 7000) {
return 70;
} else if (fileSize == 8000) {
return 80;
} else if (fileSize == 9000) {
return 90;
}
}
return 100;
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement Progess Bar.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. List all attributes of Image View.


Ans:

∑ android:scaleType
∑ android:src
∑ android:tint
∑ android:tintMode
∑ android:adjustViewBounds
∑ android:baseline
∑ android:baselineAlignBottom
∑ android:cropToPadding
∑ android:maxHeight
∑ android:maxWidth

2. Write steps to add following string array to grid view. static final String
[] example= new String {“A”, “B”, “C”, “D”, “E”};
Ans:
1) In your activity_main.xml file add grid layout as follow:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>

2) In your java code create a String array


static final String[] example = new String ={"A","B","C","D","E"};

3) Get the grid layout by its id


GridView gridview;
gridView = (GridView) findViewById(R.id.gridView1);
Tejas Vaidya 1859

4) Create an array adapter with the array.


ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);

5) Now set the adapter in the grid layout


gridView.setAdapter(adapter);

3. Describe android:stretchMode attribute of Grid view in detail.


Ans:

android:stretchMode : Defines how columns should stretch to fill the


available empty space, if any. This must be either of the values-

∑ none : Stretching is disabled.


∑ spacingWidth : The spacing between each column is stretched.
∑ columnWidth : Each column is stretched equally.
∑ spacingWidthUniform: The spacing between each column is uniformly
stretched..
Tejas Vaidya 1859

Exercise:
1. Write a program to show the following output. Use appropriate view for
the same.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp14_1">

<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/Theme.Exp14_1">
<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>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent" />

</LinearLayout>

strings_xml:

<resources>
<string name="app_name">ListView</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
Tejas Vaidya 1859

<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>

MainActivity.java:
package com.example.exp14_1;

import androidx.appcompat.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.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ListView listView;
TextView textView;
String[] listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView=(ListView)findViewById(R.id.listView);
listItem = getResources().getStringArray(R.array.array_technology);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
listItem);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long l) {
String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,
Toast.LENGTH_SHORT).show();

}
});
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. Write a program to display an image using Image View and Button


named as “Change Image”. Once you click on button another image
should get displayed.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp14_2">

<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/Theme.Exp14_2">
<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>

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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:src="@drawable/ic1"
android:id="@+id/img1"
app:tint="#00FF00" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Change Image"
android:id="@+id/button"/>
</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp14_2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Button b1;
ImageView iv;
boolean flag;
int images[]={R.drawable.ic1,R.drawable.ic2,R.drawable.ic3};
int i=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

iv=(ImageView) findViewById(R.id.img1);
b1=(Button) findViewById(R.id.button);

flag=true;

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(images[i]);
i++;
if(i==3)
i=0;
}
});
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

3. Write a program to display 15 buttons using grid view.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp14_3">

<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/Theme.Exp14_3">
<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>

activity_main.xml:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>

MainActivity.java:
package com.example.exp14_3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
Tejas Vaidya 1859

public class MainActivity extends Activity {


GridView gridView;
static final String[] numbers = new String[] {
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"10","11","12","13","14","15"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gridView = (GridView) findViewById(R.id.gridView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,


android.R.layout.simple_list_item_1, numbers);

gridView.setAdapter(adapter);

gridView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int
position,long id) {
Toast.makeText(getApplicationContext(),((TextView)
view).getText(), Toast.LENGTH_LONG).show();

}
});
}

}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

4. Write a program to display a text view using vertical scroll view.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp14_4">

<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/Theme.Exp14_4">
<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>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Vertical ScrollView example"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />

<ScrollView android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
Tejas Vaidya 1859

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 10" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 11" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 12" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 13" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 14" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 15" />
Tejas Vaidya 1859

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 16" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 17" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 18" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 19" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 20" />

</LinearLayout>

</ScrollView>

</RelativeLayout>

MainActivity.java:
package com.example.exp14_4;

import androidx.appcompat.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);
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement List View,


Grid View, Image View & Scroll View.

Reference: www.javatpoint.com
Tejas Vaidya 1859

Practical Related Questions:

1. List all predefined constants to specify the overall positioning of the


Toast. Which method is used to change the positioning of a Toast
message on the screen?
Ans: An Android Toast is a small message displayed on the screen, similar to
a tool tip or other similar popup notification. A Toast is displayed on top of
the main content of an Sactivity, and only remains visible for a short time
period.
You can change the positioning on the screen of a Toast message using the
setGravity() method. Here is a Toast setGravity().

Example:
Toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTALLY, 0, 0);

2. List two constants of Toast class.


Ans:

1) public static final int LENGTH_LONG: displays view for the long
duration of time.
2) public static final int LENGTH_SHORT: displays view for the short
duration of time.
Tejas Vaidya 1859

Exercise:
1. Write a program to display following Output.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp15_1">

<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/Theme.Exp15_1">
<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>

activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
Tejas Vaidya 1859

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World, Toast Example"
android:textColor="#FF0000"
android:textSize="25sp"
android:textStyle="bold"/>

<Button
android:id="@+id/b1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginTop="20dp"
android:textColor="#fff"
android:textSize="20sp" />

</LinearLayout>

MainActivity.java:
package com.example.exp15_1;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button customToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customToast = (Button) findViewById(R.id.b1);
customToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)
findViewById(R.id.toast_layout_root));
TextView toastTextView = (TextView)
layout.findViewById(R.id.toastTextView);

toastTextView.setText("Message for you: \n You have got mail!");


Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
}
Tejas Vaidya 1859

custom_toast.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DAAA"
android:orientation="horizontal"
android:padding="8dp">

<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>

Output:
Tejas Vaidya 1859

2. WAP to display following output.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp15_2">

<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/Theme.Exp15_2">
<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>

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">
Tejas Vaidya 1859

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:layout_marginLeft="165dp"
android:text="Pizza"/>

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="165dp"
android:text="Coffee"/>

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="165dp"
android:text="Burger" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:layout_gravity="center"
android:text="Order" />

</LinearLayout>

MainActivity.java:
package com.example.exp15_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


CheckBox pizza,coffe,burger;
Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
Tejas Vaidya 1859

pizza=(CheckBox)findViewById(R.id.checkBox);
coffe=(CheckBox)findViewById(R.id.checkBox2);
burger=(CheckBox)findViewById(R.id.checkBox3);
buttonOrder=(Button)findViewById(R.id.button);

buttonOrder.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(pizza.isChecked()){
result.append("\nPizza 100Rs");
totalamount+=100;
}
if(coffe.isChecked()){
result.append("\nCoffe 50Rs");
totalamount+=50;
}
if(burger.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
result.append("\nTotal: "+totalamount+"Rs");

Toast.makeText(getApplicationContext(), result.toString(),
Toast.LENGTH_LONG).show();
}

});
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement Custom Toast


Alert.

Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. Write an xml TimePicker tag with all its attributes.


Ans:
TimePicker Tag:
<TimePicker
android:id="@+id/timePicker"
android:layout_width="241dp"
android:layout_height="167dp"
android:timePickerMode="spinner" />

Android TimePicker Control Attributes:


∑ android:id: It is used to uniquely identify the control
∑ android:timePickerMode: It is used to specify timepicker mode, either
spinner or clock
∑ android:background: It is used to set the background color for the date
picker.
∑ android:padding: It is used to set the padding for left, right, top or bottom of
the date picker

2. List and explain the methods of TimePicker class.


Sr. No Method & description

1
is24HourView()
This method returns true if this is in 24 hour view else false
2
isEnabled()
This method returns the enabled status for this view
3
setCurrentHour(Integer currentHour)
This method sets the current hour
4
setCurrentMinute(Integer currentMinute)
This method sets the current minute
5
setEnabled(boolean enabled)
This method set the enabled state of this view
6
setIs24HourView(Boolean is24HourView)
This method set whether in 24 hour or AM/PM mode
7
setOnTimeChangedListener(TimePicker.OnTimeChangedListener
onTimeChangedListener)
This method Set the callback that indicates the time has been adjusted by the user
Tejas Vaidya 1859

3. List and explain the 5 methods of DatePicker class.


Ans:

Sr.No Method & description

1
getDayOfMonth()
This method gets the selected day of month

2
getMonth()
This method gets the selected month

3
getYear()
This method gets the selected year

4
setMaxDate(long maxDate)
This method sets the maximal date supported by this DatePicker in milliseconds
since January 1, 1970 00:00:00 in getDefault() time zone

5
setMinDate(long minDate)
This method sets the minimal date supported by this NumberPicker in milliseconds
since January 1, 1970 00:00:00 in getDefault() time zone

6
setSpinnersShown(boolean shown)
This method sets whether the spinners are shown

7
updateDate(int year, int month, int dayOfMonth)
This method updates the current date

8
getCalendarView()
This method returns calendar view

9
getFirstDayOfWeek()
This Method returns first day of the week
Tejas Vaidya 1859

Exercise:
1. Write a program to display circular progress bar.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp16_1">

<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/Theme.Exp16_1">
<activity android:name=".MainActivity2"></activity>
<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>

strings_xml:
<resources>
<string name="app_name">Exp16_1</string>
<string name="am_pm">AM/PM</string>
<string name="_24_hours">24 Hours</string>
</resources>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
Tejas Vaidya 1859

android:layout_alignParentBottom="true"
android:layout_marginEnd="146dp"
android:layout_marginBottom="546dp"
android:text="@string/am_pm"
android:textColor="@color/black"
android:textSize="36sp" />

<TimePicker
android:id="@+id/timePicker"
android:layout_width="241dp"
android:layout_height="167dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="83dp"
android:layout_marginBottom="358dp"
android:timePickerMode="spinner" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="133dp"
android:layout_marginBottom="263dp"
android:text="@string/_24_hours"
android:textColor="@color/black"
android:textSize="36sp" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="241dp"
android:layout_height="167dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="89dp"
android:layout_marginBottom="83dp"
android:timePickerMode="spinner" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="140dp"
android:layout_marginBottom="18dp"
android:text="TimePicker" />

</RelativeLayout>

</LinearLayout>

MainActivity.java:
package com.example.exp16_1;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
Tejas Vaidya 1859

import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
picker.setIs24HourView(true);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
openActivity2();
}
});
}
private void openActivity2() {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}

activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="675dp">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:layout_marginBottom="34dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Change Time" />
Tejas Vaidya 1859

<TimePicker
android:id="@+id/timePicker"
android:layout_width="390dp"
android:layout_height="446dp"
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />

</RelativeLayout>

</LinearLayout>

MainActivity2.java:
package com.example.exp16_1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity2 extends AppCompatActivity {

TextView textview1;
TimePicker timepicker;
Button changetime;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textview1=(TextView)findViewById(R.id.textView1);
timepicker=(TimePicker)findViewById(R.id.timePicker);
timepicker.setIs24HourView(true);
changetime=(Button)findViewById(R.id.button1);
textview1.setText(getCurrentTime());
changetime.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
textview1.setText(getCurrentTime());
}
});
}
public String getCurrentTime(){
String currentTime="Current Time:
"+timepicker.getCurrentHour()+":"+timepicker.getCurrentMinute();
return currentTime;
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. WAP to display following output. Select and display date and time on
click of “select date” and “select time” respectively.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp16_2">

<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/Theme.Exp16_2">
<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>

activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
Tejas Vaidya 1859

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_date"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"
android:id="@+id/btn_date"
android:layout_alignBottom="@+id/in_date"
android:layout_toRightOf="@+id/in_date"
android:layout_toEndOf="@+id/in_date" />

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_time"
android:layout_marginTop="20dp"
android:layout_below="@+id/in_date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:id="@+id/btn_time"
android:layout_marginTop="20dp"
android:layout_below="@+id/btn_date"
android:layout_alignLeft="@+id/btn_date"
android:layout_alignStart="@+id/btn_date" />

</RelativeLayout>

MainActivity.java:
package com.example.exp16_2;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
Tejas Vaidya 1859

Button btnDatePicker, btnTimePicker;


EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, mMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDatePicker=(Button)findViewById(R.id.btn_date);
btnTimePicker=(Button)findViewById(R.id.btn_time);
txtDate=(EditText)findViewById(R.id.in_date);
txtTime=(EditText)findViewById(R.id.in_time);
btnDatePicker.setOnClickListener(this);
btnTimePicker.setOnClickListener(this);

}
@Override
public void onClick(View v) {

if (v == btnDatePicker) {

final Calendar c = Calendar.getInstance();


mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this,


new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) +


"-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == btnTimePicker) {

final Calendar c = Calendar.getInstance();


mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(this,


new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {

txtTime.setText(hourOfDay + ":" + minute);


}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

Conclusion: We successfully developed a program to implement Date and


Time Picker.
Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya` 1859

Practical Related Question:

2. Give the hierarchy of the directory structure where you store activity.
Tejas Vaidya` 1859

Exercise:

1. Write a program to create a Hello World Activity using all lifecycles


methods to display messages using Log.d.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment17">

<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/Theme.Experiment17">
<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>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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" />

</androidx.constraintlayout.widget.ConstraintLayout>
Tejas Vaidya` 1859

MainActivity.java:
package com.example.experiment17;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}
Tejas Vaidya` 1859

Output:
Tejas Vaidya` 1859

Conclusion:

We successfully developed a program to create an activity.

Reference:

www.geeksforgeeks.com
Tejas Vaidya 1859

Exercise:
1. Write a program to create a text field and a button “Navigate”. When
you enter www.google.com and press navigate button it should open
goggle page.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp18_1">

<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/Theme.Exp18_1">
<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>

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"
android:gravity="center"
tools:context=".MainActivity">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:boxStrokeColor="#2196F3"
app:helperText="Required*"
app:helperTextTextColor="#F80000"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF">
Tejas Vaidya 1859

<EditText
android:id="@+id/ed1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="@string/ed1"
android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#0000FF"
android:text="@string/b1"/>

</LinearLayout>

strings.xml:
<resources>
<string name="app_name">Exp18_1</string>
<string name="b1">NAVIGATE</string>
<string name="ed1">Enter Valid URL</string>
</resources>

MainActivity.java:
package com.example.exp18_1;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText txtLink;
Button btnOpenLink;
String defaultLink;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
defaultLink = "http://"
txtLink = (EditText) findViewById(R.id.ed1);
Tejas Vaidya 1859

btnOpenLink = (Button) findViewById(R.id.b1);


btnOpenLink.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String link = txtLink.getText().toString();
if(!TextUtils.isEmpty(link)){
Uri uri = Uri.parse(defaultLink+link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}else{
txtLink.setError("Enter URL");
}
}
});
}
}

Output:
Tejas Vaidya 1859

2. Write a program to create button “Start Dialer”. When u click on this


button it should open the phone dialer.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp18_2">
<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"
android:supportsRtl="true"
android:theme="@style/Theme.Exp18_2">
<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>

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"
android:gravity="center"
tools:context=".MainActivity">

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:boxStrokeColor="#2196F3"
app:helperText="Required*"
app:helperTextTextColor="#F80000"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF">

<EditText
android:id="@+id/ed1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="@string/ed1"
android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>
Tejas Vaidya 1859

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#0000FF"
android:onClick="Call"
android:text="@string/b1"/>

</LinearLayout>

strings.xml:
<resources>
<string name="app_name">Exp18_2</string>
<string name="ed1">Enter Phone Number</string>
<string name="b1">START DIALER</string>
</resources>

MainActivity.java:
package com.example.exp18_2;

import android.content.Intent;
import android.net.Uri;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void Call(View v)


{

EditText e = (EditText)findViewById(R.id.ed1);
Toast.makeText(this, "clicked", Toast.LENGTH_LONG).show();
Uri u = Uri.parse("tel:" + e.getText().toString());
Intent i = new Intent(Intent.ACTION_DIAL, u);
try
{
startActivity(i);
}
catch (SecurityException s)
{
Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG).show();
}
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

3. Write a program to create 2 screens. First screen will take one number
input from the user. After a click on Factorial button, second screen will
open and it should display factorial of the same number. Also specify
which type of intent you will use in this case.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp18_3">

<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/Theme.Exp18_3">
<activity android:name=".MainActivity2"></activity>
<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>

strings.xml:
<resources>

<string name="app_name">Exp18_3</string>
<string name="ed1">Enter Number</string>
<string name="b1">FACTORIAL</string>
<string name="result">Factorial is:</string>

</resources>

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"
android:gravity="center"
tools:context=".MainActivity">
Tejas Vaidya 1859

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLayout1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:boxStrokeColor="#2196F3"
app:helperText="Required*"
app:helperTextTextColor="#F80000"
app:hintTextColor="#2196F3"
app:placeholderTextColor="#FFFFFF">

<EditText
android:id="@+id/ed1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="@string/ed1"
android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#0000FF"
android:text="@string/b1"/>

</LinearLayout>

MainActivity.java:
package com.example.exp18_3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText ed1;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ed1=findViewById(R.id.ed1);
b1=findViewById(R.id.b1);
Tejas Vaidya 1859

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new
Intent(getApplicationContext(),MainActivity2.class);
i.putExtra("number",ed1.getText().toString());
startActivity(i);
}
});
}
}

activity_main2.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"
android:gravity="center"
tools:context=".MainActivity2">

<TextView
android:id="@+id/result"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Factorial is: "
android:textSize="25sp"
android:textColor="#FF0000"
android:textStyle="bold"/>

</LinearLayout>

MainActivity2.java:
package com.example.exp18_3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
result= findViewById(R.id.result);
Intent i2= getIntent();
int num=Integer.parseInt(i2.getStringExtra("number"));
int fact=1;
Tejas Vaidya 1859

for(int i=1;i<=num;i++)
{
fact*=i;
}
result.append(fact+"");
}
}

Explicit type of intent is use in this program.

Output:

Conclusion: we successfully developed a program to implement new


activity using explicit intent and implicit intent.
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:


1. Differentiated between Activity Intent and Broadcasting Intent.
Ans:
Activity Intent Broadcasting Intent

Android Intent is the message that Broadcast intents are a mechanism


is passed between components such by which an intent can be issued for
as activities, content providers, consumption by multiple components
broadcast receivers, services etc. on an Android system.
It is generally used with A broadcast receiver is implemented
startActivity() method to invoke as a subclass of BroadcastReceiver
activity, broadcast receivers etc. class and overriding the onReceive()
method .

2. Draw Broadcast Receiver Lifecycle.


Ans:

3. List the System Events related to Broadcast Receivers.


Ans:
∑ android.intent.action.BATTERY_LOW : Indicates low battery condition
on the device.
∑ android.intent.action.BOOT_COMPLETED : This is broadcast once,
after the system has finished booting
∑ android.intent.action.CALL : To perform a call to someone specified by
the data
∑ android.intent.action.DATE_CHANGED : The date has changed
∑ android.intent.action.REBOOT : Have the device reboot
∑ android.net.conn.CONNECTIVITY_CHANGE : The mobile network or
Wi-Fi connection is changed(or reset)
Tejas Vaidya 1859

Exercise:
1. Write a program to demonstrate all the system broadcast messages.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver">

<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/Theme.BroadcastReceiver">
<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="MyReceiver">
<intent-filter>
<action android:name="MyAction" />
</intent-filter>
</receiver>

</application>

</manifest>

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"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Brodcast"
android:textStyle="bold"
Tejas Vaidya 1859

android:textColor="#FF0000"
android:textAppearance="?android:textAppearanceLarge"/>

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="broadcastIntent"
android:layout_marginTop="10dp"
android:text="Brodcast Intent"/>

</LinearLayout>

MainActivity.java:
package com.example.broadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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 broadcastIntent(View view) {


Intent i=new Intent(this,MyReceiver.class);
i.setAction("MyAction");
sendBroadcast(i);
}
}

MyReciever.java:
package com.example.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Intent Detected",Toast.LENGTH_LONG).show();
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to implement Broadcast


Receiver.

Reference: www.javatpoint.com
Tejas Vaidya 1859

Practical Related Questions:


1. List the best practices for accessing and using sensors.
Ans:
∑ Before using the sensor coordinate system, confirm the default orientation
mode of the device and check for the orientation of the x and y axes.
∑ Check the availability, range, minimum delay, reporting modes, and
resolution of the sensor before using it.
∑ Do not block or do heavy processing on the OnSensorChanged() method.
Your app might miss callbacks or go into ANR (Application Not
Responding) mode. The app might even crash in the worst cases if this
callback is blocked.
∑ Every registration of the event listener should be paired with the
unregistration of the same listener. This should be done at the right time and
place. (More on this, in the next chapter .
∑ Avoid using deprecated sensors and any of the deprecated APIs.
∑ Never write any kind of application logic based on the delay between the
sensor events. Always use the timestamp from the sensor event to do your
time-related calculations.
∑ If some sensors are mandatory for your application to function, then use the
uses-feature filter in the Manifest.xml file and change the required value to
true. Check your application and its sensor behavior on more than one
device, as the sensor values and range may vary with different devices.

2. Differentiate between Sensor Class and Sensor Manager Class.


Ans:
Sensor Class Sensor Manager Class

This class is used to create an instance This is used to get access to various
of a specific sensor. SensorEvent: sensors present in the device to use it
This class is used to find the details of according to need.
the sensor events .
Sensors can be used to monitor the SensorManager can be used to access
threedimensional device movement or the device's sensors SensorManager
change in the environment of the lets you access the device's sensors
device.
Tejas Vaidya 1859

Exercise:
1. Write a program to change the background color when device is
shuffled.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp22_1">

<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/Theme.Exp22_1">
<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>

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Shake to switch color" />

</RelativeLayout>

MainActivity.java:
package com.example.exp22_1;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
Tejas Vaidya 1859

import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener{


private SensorManager sensorManager;
private boolean isColor = false;
private View view;
private long lastUpdate;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);


lastUpdate = System.currentTimeMillis();
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}

private void getAccelerometer(SensorEvent event) {


float[] values = event.values;

float x = values[0];
float y = values[1];
float z = values[2];

float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);

long actualTime = System.currentTimeMillis();

Toast.makeText(getApplicationContext(),String.valueOf(accelationSquareRoot)+" "+
SensorManager.GRAVITY_EARTH,Toast.LENGTH_SHORT).show();

if (accelationSquareRoot >= 2)
{

if (actualTime - lastUpdate < 200) {


return;
}
lastUpdate = actualTime;
if (isColor) {
view.setBackgroundColor(Color.GREEN);

} else {
view.setBackgroundColor(Color.RED);
}
Tejas Vaidya 1859

isColor = !isColor;
}
}

@Override
protected void onResume() {
super.onResume();

sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACC
ELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}

Output:
Tejas Vaidya 1859

2. Write a program to display the list of sensors supported by the mobile


device.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp22_2">

<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/Theme.Exp22_2">
<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>

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:transitionGroup="true">

<TextView
android:text="Sensor "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="30dp"
android:textColor="#FF0000"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All Sensors Supported by the Mobile Device"
android:layout_marginTop="10dp"
android:id="@+id/textView1"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="20sp" />
Tejas Vaidya 1859

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/textView2"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />

</RelativeLayout>

MainActivity.java:
package com.example.exp22_2;

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.List;
import android.hardware.Sensor;

public class MainActivity extends Activity {


TextView tv1=null;
private SensorManager mSensorManager;
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv1 = (TextView) findViewById(R.id.textView2);


tv1.setVisibility(View.GONE);

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);


List<Sensor> mList= mSensorManager.getSensorList(Sensor.TYPE_ALL);

for (int i = 1; i < mList.size(); i++) {


tv1.setVisibility(View.VISIBLE);
tv1.append("\n" + mList.get(i).getName() + "\n" +
mList.get(i).getVendor() + "\n" + mList.get(i).getVersion());
}
}
}
Tejas Vaidya 1859

Output:

Conclusion: We have successfully develop a program to implement sensors.

Reference: www.geeksforgeek.com
Tejas Vaidya 1859

Practical Related Questions:

1. List all the methods related to camera class.


Ans:
∑ autoFocus(Camera.AutoFocusCallback cb)
∑ cancelAutoFocus()
∑ enableShutterSound(boolean enabled)
∑ getCameraInfo(int cameraId, Camera.CameraInfo cameraInfo)
∑ getNumberOfCameras()
∑ lock()
∑ open()
∑ reconnect()
∑ startFaceDetection()
∑ startSmoothZoom(int value)

2. Explain the method that is used to detect the face.


Ans:
findFaces(Bitmap bitmap, Face[] faces) method finds all the faces found in a
given Bitmap.
Tejas Vaidya 1859

Exercise:
1. Write a program to capture an image and display it using image view.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp23_1">

<uses-permission android:name="android.permission.CAMERA" />


<uses-feature android:name="android.hardware.camera2"/>

<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/Theme.Exp23_1">
<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>

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"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:id="@+id/camera_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="125dp"
android:text="Camera" />

<ImageView
android:layout_marginTop="70dp"
android:layout_width="350dp"
android:layout_height="450dp"
android:id="@+id/click_image"
android:layout_marginBottom="10dp"/>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp23_1;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private static final int pic_id = 123;


Button camera_open_id;
ImageView click_image_id;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

camera_open_id = (Button)findViewById(R.id.camera_button);
click_image_id = (ImageView)findViewById(R.id.click_image);

camera_open_id.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
Intent camera_intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera_intent, pic_id);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)


{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == pic_id) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
click_image_id.setImageBitmap(photo);
}
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

2. Write a program to record a video using various camera methods.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp23_2">
<uses-feature android:name="android.hardware.camera2"/>
<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/Theme.Exp23_2">
<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>

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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Record Video"
android:onClick="recordVideo"/>

<VideoView
android:id="@+id/videoView"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp23_2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

VideoView videoView;
int REQUEST_CODE_VIDEO_CAPTURE = 2607;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoView = findViewById(R.id.videoView);

MediaController mediaController = new MediaController(this);


mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
}

public void recordVideo(View view) {

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);


if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_CODE_VIDEO_CAPTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable
Intent data) {
if (requestCode == REQUEST_CODE_VIDEO_CAPTURE && resultCode == RESULT_OK)
{
Uri videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to build camera.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:


1. Name the methods which are used to enable and disable Bluetooth
adapter.

Ans:
∑ boolean enable() enables the bluetooth adapter if it is disabled.
∑ boolean isEnabled() returns true if the bluetooth adapter is enabled.
∑ boolean disable() disables the bluetooth adapter if it is enabled.

2. Explain the purpose of ACTION_REQUEST_DISCOVERABLE Constant.

Ans: The ACTION_REQUEST_DISCOVERABLE constant is used for turn on


discovering of Bluetooth .

3. List the uses of setName(String name)method.


Ans:
∑ Set the friendly Bluetooth name of the local Bluetooth adapter.
∑ This name is visible to remote Bluetooth device.
Tejas Vaidya 1859

Exercise:
1. Write a program to turn on, get visible, list devices and turn off
Bluetooth.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp24" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.BLUETOOTH" />


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<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/Theme.Exp24">
<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>

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

<TextView android:text="Bluetototh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:textStyle="bold"
android:textColor="#FF0000"
android:textSize="25sp"/>
Tejas Vaidya 1859

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="90dp"
android:onClick="on"
android:text="TURN_ON" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:onClick="visible"
android:text="GET VISIBLE" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="27dp"
android:onClick="list"
android:text="List Devices" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="28dp"
android:onClick="off"
android:text="TURN_OFF" />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1"
android:layout_marginRight="30dp"
android:layout_below="@+id/textView2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"
android:id="@+id/textView2"
android:textColor="#FF0000"
android:textSize="25dp"
Tejas Vaidya 1859

android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/listView"
android:layout_alignStart="@+id/listView" />

</RelativeLayout>

MainActivity.java:
package com.example.exp24;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends Activity {


Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);

BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}

public void on(View v){


if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned
on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on",
Toast.LENGTH_LONG).show();
}
}
Tejas Vaidya 1859

public void off(View v){


BA.disable();
Toast.makeText(getApplicationContext(), "Turned off"
,Toast.LENGTH_LONG).show();
}

public void visible(View v){


Intent getVisible = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}

public void list(View v){


pairedDevices = BA.getBondedDevices();

ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());


Toast.makeText(getApplicationContext(), "Showing Paired
Devices",Toast.LENGTH_SHORT).show();

final ArrayAdapter adapter = new


ArrayAdapter(this,android.R.layout.simple_list_item_1, list);

lv.setAdapter(adapter);
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program for providing Bluetooth


Connectivity.

Reference: www.javatpoint.com
Tejas Vaidya 1859

Practical Related Questions

1. List the basic methods used in an android AsyncTaskclass.


Ans:
∑ doInBackground()
∑ onPreExecute()
∑ onPostExecute()
∑ onProgressUpdate()

2. Differentiate between AsyncTask and Services.


Ans:

Service AsyncTask
Task with no UI, but ∑ Relatively long task (UI thread
shouldn’t be too long. Use blocking) with a need to
threads within service for communicate with main
When to use? long tasks. thread.
∑ For tasks parallel use multiple
instances OR Executor.
Trigger Call to method Call to method execute()
onStartService()
Triggered Any Thread Main Thread
From (thread)
Runs On Main Thread Worker thread. However, main
(thread) thread methods may be invoked
in between to publish progress.
Limitations/ May block main thread. ∑ One instance can only be
Drawbacks executed once
∑ Must be created and executed
from the Main thread.

3. Name the method used, if a process takes a long time to do its work?
Ans:

∑ doInBackground(Params)
Tejas Vaidya 1859

Exercise:
1. Write a program to insert data in SQLite database using AsyncTask.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp26">

<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/Theme.Exp26">
<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>

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Data"
android:textSize="30sp"
android:textColor="#FF0000"
android:textStyle="bold"
android:layout_marginBottom="30dp"/>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="350dp"
android:layout_height="57dp"
android:hint="Name"
android:layout_gravity="center"
android:inputType="text"
android:id="@+id/editText_name" />
</com.google.android.material.textfield.TextInputLayout>
Tejas Vaidya 1859

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="350dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:id="@+id/editText_surname"
android:layout_gravity="center"
android:hint="Surnmae"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="350dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:id="@+id/editText_Marks"
android:layout_gravity="center"
android:hint="Marks"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

<Button
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="Add Data"
android:id="@+id/button_add"
android:backgroundTint="#2196F3"
android:textColor="@color/white"
android:layout_below="@+id/editText_Marks" />

<Button
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:text="View All"
android:backgroundTint="#2196F3"
android:textColor="@color/white"
android:id="@+id/button_viewAll" />

</LinearLayout>

MainActivity.java:
package com.example.exp26;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Tejas Vaidya 1859

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


DatabaseHelper myDb;
EditText editName,editSurname,editMarks ,editTextId;
Button btnAddData;
Button btnviewAll;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

editName = (EditText)findViewById(R.id.editText_name);
editSurname = (EditText)findViewById(R.id.editText_surname);
editMarks = (EditText)findViewById(R.id.editText_Marks);
//editTextId = (EditText)findViewById(R.id.editText_id);
btnAddData = (Button)findViewById(R.id.button_add);
btnviewAll = (Button)findViewById(R.id.button_viewAll);

AddData();
viewAll();
}

public void AddData() {


btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted =
myDb.insertData(editName.getText().toString(),
editSurname.getText().toString(),
editMarks.getText().toString() );
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data
Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Inserted",Toast.LENGTH_LONG).show();
}
}
);
}

public void viewAll() {


btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
Tejas Vaidya 1859

StringBuffer buffer = new StringBuffer();


while (res.moveToNext()) {
buffer.append("Id :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");
buffer.append("Surname :"+ res.getString(2)+"\n");
buffer.append("Marks :"+ res.getString(3)+"\n\n");
}

// Show all data


showMessage("Data",buffer.toString());
}
}
);
}

public void showMessage(String title,String Message){


AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}

DatabaseHelper.java:
package com.example.exp26;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {


public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
Tejas Vaidya 1859

onCreate(db);
}

public boolean insertData(String name,String surname,String marks) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}

public Cursor getAllData() {


SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}

public boolean updateData(String id,String name,String surname,String marks) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}

public Integer deleteData (String id) {


SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully performed Async task using SQLite.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. Explain the use of equals() function.


Ans: The java string equals() method compares the two given strings based on
the content of the string. If any character is not matched, it returns false. If all
characters are matched, it returns true.

2. List the important functions which are related to GUI component


“Button”.
Ans: On a button we can perform different actions or events like click event,
pressed event, touch event etc. Android buttons are GUI components which are
sensible to taps (clicks) by the user. When the user taps/clicks on button in an
Android app, the app can respond to the click/tap.

3. State the uses of Toast message.


Ans: Android Toast is used to show notification for a particular interval of time
at the bottom of the screen by default. Toast message doesn't block the user
interaction and auto disappears after a timeout.
Tejas Vaidya 1859

Exercise:
1. Write a program to the login form & display login Successful /
Unsuccessful toast message.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp_27">

<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/Theme.Exp_27">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<activity android:name=".MainActivity2"/>
</application>

</manifest>

strings_xml:

<resources>
<string name="app_name">Exp_27</string>
<string name="login">LOG IN</string>
<string name="login_title">Log in to Snapchat</string>
<string name="login_form_username">Username</string>
<string name="login_form_password">Password</string>
<string name="welcome">Welcome to Snapchat</string>
</resources>

rounded_login_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlue"
android:width="1dp"/>
<solid
android:color="@color/colorBlue"/>
<corners android:radius="24dp" />
</shape>
</item>
Tejas Vaidya 1859

<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlueDark"
android:width="1dp"/>
<solid
android:color="@color/colorBlueDark"/>
<corners android:radius="24dp" />
</shape>
</item>
</selector>

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"
android:orientation="vertical"
android:background="@color/colorPrimary"
tools:context=".MainActivity">

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="150dp">

<androidx.cardview.widget.CardView
android:layout_margin="64dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
app:cardCornerRadius="15dp"
app:cardMaxElevation="12dp"
android:background="#fff">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/login_title"
Tejas Vaidya 1859

android:textAlignment="center"
android:textColor="@color/colorBlack"
android:textSize="25sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/login_form_username"
android:textSize="20sp" />

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Enter Usename"
android:inputType="text" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/login_form_password"
android:textSize="20sp" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">

<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Enter Password"
android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/activity_main_loginButton"
android:layout_width="200dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:onClick="login"
android:background="@drawable/rounded_login_button"
android:text="@string/login"
android:textColor="@color/colorWhite"
android:layout_marginTop="20dp"
android:textSize="18sp"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp_27;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void login(View view) {

TextView uid = (TextView)findViewById(R.id.e1);


TextView upass = (TextView)findViewById(R.id.e2);

if (TextUtils.isEmpty(uid.getText().toString())||
TextUtils.isEmpty(upass.getText().toString()))
{
Toast.makeText(MainActivity.this, "Login Unsuccessful",
Toast.LENGTH_SHORT).show();
}
else
{
Intent i1 = new Intent(MainActivity.this, MainActivity2.class);
i1.putExtra("id", uid.getText().toString());
startActivity(i1);
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();
}
}
}

activity_main2.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"
android:background="@color/colorPrimary"
tools:context=".MainActivity2">
Tejas Vaidya 1859

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/colorBlack"
android:textStyle="bold"/>

<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/colorBlack"
android:textStyle="bold"/>

</RelativeLayout>

MainActivity2.java:
package com.example.exp_27;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

TextView t3 = (TextView)findViewById(R.id.t3);

Bundle b = getIntent().getExtras();
String data_receive= b.getString("id");
t3.setText("Hi "+data_receive.toUpperCase());

}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

Conclusion: We have successfully sample application with login module.

Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. Explain validation of user input?


Ans:
∑ Validation is a process to ensure that the value entered by the user is
within the accepted boundaries of the application. For example, if a user
enters a name then the programmer validates the edit text in such a way
that it consists of only letters but no numeric values.

∑ Input Validation eliminates the errors that can be done by user while
giving inputs to our app. For example if we want to get the user’s email
we can check the entered email is a valid email or not before storing it
inside the database.

2. List and explain various GUI components used to design the login form
with validation.
Ans:

1) Button:

∑ A button consists of text or an icon (or both text and an icon) that
communicates what action occurs when the user touches it.

∑ Button represents a push button. A Push buttons can be clicked, or


pressed by the user to perform an action. There are different types of
buttons used in android such as CompoundButton, ToggleButton,
RadioButton. On a button we can perform different actions or events like
click event, pressed event, touch event etc.

∑ To specify an action when the button is pressed, set a click listener on the
button object in the corresponding activity code.

2) Text View:
∑ A standard read-only text label that support strings formatting, multiline
display, and automatic word wrapping.
∑ A Text View is a entire text editor, however the basic class is configured
to not allow editing.
Tejas Vaidya 1859

3) Edit Text:
∑ A EditText is an overlay over TextView that configures itself to be
editable. An editable text entry box that accepts multiline entry, word-
wrapping, and hint text.
∑ It is the predefined subclass of TextView that includes rich editing
capabilities.
∑ If a user enters a name then the programmer validates the edit text in such
a way that it consists of only letters but no numeric values.

4) Image View:
∑ In Android, ImageView class is used to display an image file in application.
Image file is easy to use but hard to master in Android, because of the
various screen sizes in Android devices.

5) ProgressBar:
∑ In android, ProgressBar is a user interface control which is used to indicate
the progress of an operation.

3. Differentiate between Text View and Edit Text View.


Ans:

Text View Edit text View


TextView is TextField for showing EditText is Input Type/Field for
text inputting text

TextView is the widget used when EditText used when you want the
you want the user to View the Text user to be able to edit the text.
(such as a label, etc)

TextView is just like Label tag Edittext is input type.

EditText is used when you expect an EditText provides the behavior for
input from the user. user input (display keyboard, paste,
position indicator, etc).
TextView is can't changable by the Edit text View is changable by the
user user
Tejas Vaidya 1859

Exercise:
1. Write a program to the login form with necessary validations like length
of username & password, empty text fields, count of unsuccessful login
attempts. Display the login Successful / Unsuccessful toast message.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp_28">
<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/Theme.Exp_28">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"/>
</application>

</manifest>

strings_xml:

<resources>
<string name="app_name">Exp_28</string>
<string name="login">LOG IN</string>
<string name="login_title">Log in to Snapchat</string>
<string name="login_form_username">Username</string>
<string name="login_form_password">Password</string>
<string name="welcome">Welcome to Snapchat</string>
</resources>

rounded_login_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlue"
android:width="1dp"/>
<solid
android:color="@color/colorBlue"/>
<corners android:radius="24dp" />
</shape>
</item>
Tejas Vaidya 1859

<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlueDark"
android:width="1dp"/>
<solid
android:color="@color/colorBlueDark"/>
<corners android:radius="24dp" />
</shape>
</item>
</selector>

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"
android:orientation="vertical"
android:background="@color/colorPrimary"
tools:context=".MainActivity">

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="150dp">

<androidx.cardview.widget.CardView
android:layout_margin="64dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
app:cardCornerRadius="15dp"
app:cardMaxElevation="12dp"
android:background="#fff">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/login_title"
Tejas Vaidya 1859

android:textAlignment="center"
android:textColor="@color/colorBlack"
android:textSize="25sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/login_form_username"
android:textSize="20sp" />

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Enter Usename"
android:inputType="text" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/login_form_password"
android:textSize="20sp" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">

<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Enter Password"
android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/activity_main_loginButton"
android:layout_width="200dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:onClick="login"
android:background="@drawable/rounded_login_button"
android:text="@string/login"
android:textColor="@color/colorWhite"
android:layout_marginTop="20dp"
android:textSize="18sp"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp_28;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

public void login(View view) {

TextView uid = (TextView)findViewById(R.id.e1);


TextView upass = (TextView)findViewById(R.id.e2);

if(TextUtils.isEmpty(uid.getText().toString()))
{
uid.setError("Enter UserName");
}

if(TextUtils.isEmpty(upass.getText().toString()))
{
upass.setError("Enter Password");
}

if
(TextUtils.isEmpty(uid.getText().toString())||TextUtils.isEmpty(upass.getText().to
String())) {
count+=1;
Toast.makeText(MainActivity.this, "Login Unsuccessful \n Attempt:
"+count,Toast.LENGTH_SHORT).show();
}
else
{
Intent i1 = new Intent(MainActivity.this, MainActivity2.class);
i1.putExtra("id", uid.getText().toString());
startActivity(i1);
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();

}
}
}
Tejas Vaidya 1859

activity_main2.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"
android:background="@color/colorPrimary"
tools:context=".MainActivity2">

<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="70dp"
android:src="@drawable/snapchat"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/colorBlack"
android:textStyle="bold"/>

<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/colorBlack"
android:textStyle="bold"/>

</RelativeLayout>

MainActivity2.java:
package com.example.exp_28;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView t3 = (TextView)findViewById(R.id.t3);
Bundle b = getIntent().getExtras();
String data_receive= b.getString("id");
t3.setText("Hi "+data_receive.toUpperCase());
}
}
Tejas Vaidya 1859

Output:
Tejas Vaidya 1859

Conclusion: We have successfully created login application where you will


have to validate username and password till the username and password is not
validated, login button should remain disabled.

Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. Explain the use of SmsManager Class.


Ans:
Manages SMS operations such as sending data, text, and pdu SMS messages.
Get this object by calling the static method SmsManager.getDefault().

2. List changes that are need to be done in AndroidManifest.XML file to


send and receive messages.
Ans:
∑ To Send SMS:
<uses-permission android:name="android.permission.SEND_SMS" />

∑ To Receive SMS:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS"
/>
Tejas Vaidya 1859

Exercise:
1. Write a program to send and receive SMS, make use of following GUI.

A) Send SMS

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp_29a">

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<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/Theme.Exp_29A">
<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>

activity_main.xml:

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textStyle="bold"
android:text="@string/mobile_no" />
Tejas Vaidya 1859

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/textView1"
android:ems="10"
android:layout_toRightOf="@+id/textView1" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FF0000"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:layout_alignBottom="@+id/editText2"
android:text="@string/message" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textMultiLine" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="48dp"
android:text="@string/send_sms" />

</RelativeLayout>

</LinearLayout>

MainActivity.java:
package com.example.exp_29a;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


Tejas Vaidya 1859

EditText mobileno,message;
Button sendsms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mobileno=(EditText)findViewById(R.id.editText1);
message=(EditText)findViewById(R.id.editText2);
sendsms=(Button)findViewById(R.id.button1);
sendsms.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String no=mobileno.getText().toString();
String msg=message.getText().toString();
Intent intent=new
Intent(getApplicationContext(),MainActivity.class);
PendingIntent
pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(no, null, msg, pi,null);
Toast.makeText(getApplicationContext(), "Message Sent
successfully!", Toast.LENGTH_LONG).show();
} });
}
}
Output:
Tejas Vaidya 1859

B) Receive Message:

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp29_1">

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>

<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/Theme.Exp29_1">
<receiver
android:name=".SMSBReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<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>

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"
tools:context=".MainActivity"
android:padding="10dp">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
android:id="@+id/SMSList" />

</LinearLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp29_1;

import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private static MainActivity inst;


ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter arrayAdapter;

public static MainActivity instance() {


return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

smsListView = (ListView) findViewById(R.id.SMSList);

arrayAdapter = new ArrayAdapter<String>(this,


android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);

// Add SMS Read Permision At Runtime


// Todo : If Permission Is Not GRANTED
if(ContextCompat.checkSelfPermission(getBaseContext(),
"android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {

// Todo : If Permission Granted Then Show SMS


refreshSmsInbox();

} else {
Tejas Vaidya 1859

// Todo : Then Set Permission


final int REQUEST_CODE_ASK_PERMISSIONS = 123;
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);
}

}
public void refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor =
contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext());
}

public void updateList(final String smsMessage) {


arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String[] smsMessages = smsMessagesList.get(pos).split("\n");
String address = smsMessages[0];
String smsMessage = "";
for (int i = 1; i < smsMessages.length; ++i) {
smsMessage += smsMessages[i];
}

String smsMessageStr = address + "\n";


smsMessageStr += smsMessage;
Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}

SMSBReceiver.java:
package com.example.exp29_1;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
Tejas Vaidya 1859

import android.telephony.SmsMessage;
import android.widget.Toast;

class SMSBReceiver extends BroadcastReceiver{


public static final String SMS_BUNDLE = "pdus";

public void onReceive(Context context, Intent intent) {


Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage =
SmsMessage.createFromPdu((byte[]) sms[i]);

String smsBody =
smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();

smsMessageStr += "SMS From: " + address + "\n";


smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr,
Toast.LENGTH_SHORT).show();

//this will update the UI with message


MainActivity inst = MainActivity.instance();
inst.updateList(smsMessageStr);
}
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully develop a program to send and receive SMS.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. Why it becomes necessary to have inbuilt email module in mobile


applications.

Ans: Before starting Email Activity, you must know Email functionality with
intent, Intent is carrying data from one component to another component with-in
the application or outside the application.

∑ To send an email from your application, you don’t have to implement an


email client from the beginning, but you can use an existing one like the
default Email app provided from Android, Gmail, Outlook, K-9 Mail etc. For
this purpose, we need to write an Activity that launches an email client, using
an implicit Intent with the right action and data.
∑ In android, we can easily send an email from our android application using
existing email clients such as GMAIL, Outlook, etc. instead of building an
email client from scratch.

∑ Generally, the Intent object in android with proper action (ACTION_SEND)


and data will help us to launch the available email clients to send an email in
our application.

2. List the extra fields that can be used in an application to send emails.

Ans:
Action:
∑ Data URI Scheme
∑ MIME Type
∑ Extras- CC, BCC, To, Subject, Text, Stream
Tejas Vaidya 1859

Exercise:
1. Write a program to send email.
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp30">
<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/Theme.Exp30">
<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>

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:id="@+id/txtSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<Button
android:id="@+id/btnSend"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Send" />
</LinearLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp30;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText eTo;


private EditText eSubject;
private EditText eMsg;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTo = (EditText)findViewById(R.id.txtTo);
eSubject = (EditText)findViewById(R.id.txtSub);
eMsg = (EditText)findViewById(R.id.txtMsg);
btn = (Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new
String[]{eTo.getText().toString()});
it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString());
it.putExtra(Intent.EXTRA_TEXT,eMsg.getText());
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"Choose Mail App"));
}
});
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully developed a program to send Email.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. List the names of map type and write the syntax to change it.
Ans:
Map Types and it’s Syntax are as follow:

1. Normal
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
2. Hybrid
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
3. Satellite
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

4. Terrain
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

5.None

2. Name the methods used to enable and disable zoom feature.


Ans:
1. To Enable:
setZoomControlsEnabled(true)
2.To Disable:
setZoomControlsEnabled(false)
Tejas Vaidya 1859

Exercise:
1. Write a program to locate user’s current location.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp31">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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/Theme.Exp31">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />
<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>

strings_xml:
<resources>
<string name="app_name">Exp31</string>
<string name="google_api_key">AIzaSyD4KfDcLVjVPatpIKtrPXot024Z4UHVZQ8</string>
</resources>

activity_main.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>
Tejas Vaidya 1859

MainActivity.java:
package com.example.exp31;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.internal.ICameraUpdateFactoryDelegate;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
fetchLastLocation();
}

private void fetchLastLocation() {

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
Tejas Vaidya 1859

ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {

if(location!=null){
currentLocation = location;
Toast.makeText(getApplicationContext(), "Latitude:
"+currentLocation.getLatitude()+"\n"+"Longitude: "+currentLocation.getLongitude(),
Toast.LENGTH_SHORT).show();
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(MainActivity.this);
}
}
});
}

@Override
public void onMapReady(GoogleMap googleMap) {
LatLng latLng = new
LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
MarkerOptions options = new MarkerOptions().position(latLng).title("I am
Here");
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,5));
googleMap.addMarker(options);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_CODE:
if(grantResults.length>0 &&
grantResults[0]==PackageManager.PERMISSION_GRANTED){
fetchLastLocation();
}
break;
}
}
}
Tejas Vaidya 1859

Output:

Conclusion: We successfully deployed map-based application.

Reference: www.geeksforgeeks.com
Tejas Vaidya 1859

Practical Related Questions:

1. Explain the ways to add Markers on the Google Map.


Ans:
Step 1: Create a New Project
Step 2: Generating an API key for using Google Maps-
For adding this key in our app navigate to the values folder >
google_maps_api.xml file and at line 23 you have to add your API key in the
place of YOUR_API_KEY.
Step 3: Adding a custom marker in Google Maps-
For adding a custom marker to Google Maps navigate to the app > res >
drawable > Right-Click on it > New > Vector Assets and select the icon which
we have to show on your Map.
Step 4: Working with the MapsActivity.java file
Go to the MapsActivity.java file and refer to the following code.
Step 5: Now run your app and see the output of the app.

2. Write the syntax for method which is used to add compass in Google
Map.
Ans:
∑ UiSettings.setCompassEnabled(boolean)
Tejas Vaidya 1859

Exercise:
1. Write a program to draw a route between two locations.

AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp32">

<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/Theme.Exp32">
<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>

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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_source"
android:hint="Enter Source Location"
android:padding="12dp"
android:background="@android:drawable/editbox_background"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_destination"
android:hint="Enter Destination Location"
android:padding="12dp"
android:layout_marginTop="8dp"
android:background="@android:drawable/editbox_background"/>
Tejas Vaidya 1859

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_track"
android:text="Display Track"
android:layout_marginTop="16dp"/>

</LinearLayout>

MainActivity.java:
package com.example.exp32;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText etSource,etDestination;
Button btTrack;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSource = findViewById(R.id.et_source);
etDestination = findViewById(R.id.et_destination);
btTrack = findViewById(R.id.bt_track);
btTrack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = etSource.getText().toString().trim();
String sDestination = etDestination.getText().toString().trim();
if(sSource.equals("") && sDestination.equals("")){
Toast.makeText(getApplicationContext(),"Enter both
location",Toast.LENGTH_SHORT).show();
}else {
DisplayTrack(sSource,sDestination);
}
}
});
}
private void DisplayTrack(String sSource, String sDestination) {
try {
Uri uri =
Uri.parse("https://www.google.co.in/maps/dir/"+sSource+"/"+sDestination);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setPackage("com.google.android.apps.maps");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}catch (ActivityNotFoundException e){
Tejas Vaidya 1859

Uri uri =
Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.m
aps");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}

Output:

Conclusion: We successfully deployed map-based application.

Reference: www.geeksforgeeks.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy