MAD - Exp 1 To 32 - Removed - Removed
MAD - Exp 1 To 32 - Removed - Removed
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
Ans:
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" />
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;
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();
}
}
}
Tejas Vaidya 1859
Output:
Tejas Vaidya 1859
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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Tejas Vaidya 1859
Output:
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:
<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" />
</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:
<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;
EditText editTextName;
Button btnlogin;
TextView textName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
AndroidManifest.xml:
<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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Name:"+name+"Roll No:"+rollno+"
is added",Toast.LENGTH_LONG).show();
MainActivity2.java:
package com.example.experiment7_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
@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);
n.setText(name);
r.setText(rollno);
}
}
Tejas Vaidya 1859
Output:
Reference: www.geeksforgeeks.com
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" />
</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;
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);
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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);
actv.setAdapter(adapter);
actv.setTextColor(Color.RED);
}
}
Tejas Vaidya 1859
Output:
Reference: www.geeksforgeeks.com
<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" />
</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;
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) {
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" />
</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;
@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:
Reference: www.geeksforgeeks.com
9
Tejas Vaidya 1859
Exercise:
<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" />
</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"/>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/colorBlueDark"
android:width="1dp"/>
<solid
android:color="@color/colorBlueDark"/>
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.b1);
}
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
String id = uid.getText().toString();
String pass = upass.getText().toString();
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;
@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
<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" />
</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"/>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:color="@color/purple_700"
android:width="1dp"/>
<solid
android:color="@color/purple_700"/>
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;
EditText ed1,ed2,ed3;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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;
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);
}
}
Tejas Vaidya 1859
Output:
Tejas Vaidya 1859
Tejas Vaidya 1859
Method 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.
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" />
</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;
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:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
<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" />
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" />
</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
<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;
}
}
Tejas Vaidya 1859
Output:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
<ProgressBar
android:id="@+id/circularProgressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="50" />
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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.circularProgressbar);
progressText = findViewById(R.id.t1);
Output:
Tejas Vaidya 1859
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" />
</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;
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) {
progressBarStatus = 0;
fileSize = 0;
progressBarStatus = doOperation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
Tejas Vaidya 1859
Output:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
∑ 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>
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" />
</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;
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
<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" />
</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;
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
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" />
</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
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
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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Tejas Vaidya 1859
Output:
Reference: www.javatpoint.com
Tejas Vaidya 1859
Example:
Toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTALLY, 0, 0);
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" />
</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;
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
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" />
</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;
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:
Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya 1859
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
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" />
</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;
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" />
</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;
@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) {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Output:
Tejas Vaidya 1859
2. Give the hierarchy of the directory structure where you store activity.
Tejas Vaidya` 1859
Exercise:
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" />
</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;
@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:
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" />
</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;
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
@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
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" />
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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" />
</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;
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;
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+"");
}
}
Output:
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" />
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
MyReciever.java:
package com.example.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Intent Detected",Toast.LENGTH_LONG).show();
}
}
Tejas Vaidya 1859
Output:
Reference: www.javatpoint.com
Tejas Vaidya 1859
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" />
</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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
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);
Toast.makeText(getApplicationContext(),String.valueOf(accelationSquareRoot)+" "+
SensorManager.GRAVITY_EARTH,Toast.LENGTH_SHORT).show();
if (accelationSquareRoot >= 2)
{
} 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
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" />
</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;
Output:
Reference: www.geeksforgeek.com
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">
<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" />
</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;
@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);
}
});
}
Output:
Tejas Vaidya 1859
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" />
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;
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);
@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:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
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.
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" />
<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" />
</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;
@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);
}
lv.setAdapter(adapter);
}
}
Tejas Vaidya 1859
Output:
Reference: www.javatpoint.com
Tejas Vaidya 1859
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;
@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();
}
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;
@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);
}
Output:
Reference: www.geeksforgeeks.com
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" />
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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:
<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;
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
Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya 1859
∑ 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.
∑ 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.
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)
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
Reference: www.javatpoint.com
www.geeksforgeeks.com
Tejas Vaidya 1859
∑ 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" />
</manifest>
activity_main.xml:
<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;
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" />
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} else {
Tejas Vaidya 1859
}
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 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];
}
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;
String smsBody =
smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
Output:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
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.
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;
Output:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
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
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" />
</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;
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();
}
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:
Reference: www.geeksforgeeks.com
Tejas Vaidya 1859
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" />
</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;
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:
Reference: www.geeksforgeeks.com