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

ANDROID LAB 3RD SEMESTER MSC

1. Program for simple Registration form

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

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="117dp">

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:text="Registration Form"
android:textColor="#E91E63" />

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 1


ANDROID LAB 3RD SEMESTER MSC

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Details"
android:layout_marginBottom="10dp"
android:textColor="#F44336" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:layout_marginBottom="10dp"
android:textColor="#F44336" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last name"
android:layout_marginBottom="10dp"
android:textColor="#F44336" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth"
android:layout_marginBottom="10dp"
android:textColor="#F44336" />

<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 2


ANDROID LAB 3RD SEMESTER MSC

android:layout_marginLeft="30dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:layout_marginBottom="10dp"
android:textColor="#F44336" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
</RadioGroup>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Hobbies"

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 3


ANDROID LAB 3RD SEMESTER MSC

android:textColor="#F44336" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Playing" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Watching TV" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Singing" />
</LinearLayout>
</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:text="Submit"
android:textColor="#E91E63" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 4


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.regestration;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {

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

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.regestration">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>
</manifest>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 5


ANDROID LAB 3RD SEMESTER MSC

output

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 6


ANDROID LAB 3RD SEMESTER MSC

2. Write a program for calculator.

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResultId"
android:editable="false"
android:gravity="right"/>

<TableLayout
android:id="@+id/tableId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtResultId">
<TableRow>
<Button
android:id="@+id/btnNum7Id"
android:text="7"
android:layout_height="wrap_content"
android:layout_width="wrap_content"

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 7


ANDROID LAB 3RD SEMESTER MSC

android:layout_weight="1"/>
<Button
android:id="@+id/btnNum8Id"
android:text="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnNum9Id"
android:text="9"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnClearId"
android:text="clear"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>

</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum4Id"
android:text="4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnNum5Id"
android:text="5"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 8


ANDROID LAB 3RD SEMESTER MSC

<Button
android:id="@+id/btnNum6Id"
android:text="6"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnDivId"
android:text="/"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum1Id"
android:text="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnNum2Id"
android:text="2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnNum3Id"
android:text="3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnMulId"

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 9


ANDROID LAB 3RD SEMESTER MSC

android:text="*"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum0Id"
android:text="0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnAddId"
android:text="+"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnSubId"
android:text="-"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnEqualId"
android:text="="
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 10


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java

package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {

private EditText txtResult;


private int result=0;
private String inStr="0";
private char lastOperator= ' ';

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

txtResult = (EditText)findViewById(R.id.txtResultId);
txtResult.setText("0");

((Button)findViewById(R.id.btnNum0Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum1Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum2Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum3Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum4Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum5Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum6Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum7Id)).setOnClickListener(this);

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 11


ANDROID LAB 3RD SEMESTER MSC

((Button)findViewById(R.id.btnNum8Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum9Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnAddId)).setOnClickListener(this);
((Button)findViewById(R.id.btnSubId)).setOnClickListener(this);
((Button)findViewById(R.id.btnMulId)).setOnClickListener(this);
((Button)findViewById(R.id.btnDivId)).setOnClickListener(this);
((Button)findViewById(R.id.btnClearId)).setOnClickListener(this);
((Button)findViewById(R.id.btnEqualId)).setOnClickListener(this);
}
public void onClick(View view){
switch (view.getId()){
//Number Buttons '0' to '9'
case R.id.btnNum0Id:
case R.id.btnNum1Id:
case R.id.btnNum2Id:
case R.id.btnNum3Id:
case R.id.btnNum4Id:
case R.id.btnNum5Id:
case R.id.btnNum6Id:
case R.id.btnNum7Id:
case R.id.btnNum8Id:
case R.id.btnNum9Id:

String inDigit=((Button)view).getText().toString();
if(inStr.equals("0")){
inStr = inDigit;
}
else{
inStr+=inDigit;
}
txtResult.setText(inStr);
if(lastOperator=='='){
result='0';
lastOperator = ' ';

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 12


ANDROID LAB 3RD SEMESTER MSC

}
break;

case R.id.btnAddId:
compute();
lastOperator='+';
break;
case R.id.btnSubId:
compute();
lastOperator='-';
break;
case R.id.btnMulId:
compute();
lastOperator='*';
break;
case R.id.btnDivId:
compute();
lastOperator='/';
break;
case R.id.btnEqualId:
compute();
lastOperator='=';
break;
case R.id.btnClearId:
result=0;
inStr="0";
lastOperator = ' ';
txtResult.setText("0");
break;

}
}
private void compute()
{

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 13


ANDROID LAB 3RD SEMESTER MSC

int inNum = Integer.parseInt(inStr);


inStr = "0";
if (lastOperator == ' '){
result = inNum;
}
else if(lastOperator =='+'){
result += inNum;
}
else if(lastOperator =='-'){
result -= inNum;
}
else if(lastOperator =='*'){
result *= inNum;
}
else if(lastOperator =='/'){
result /= inNum;
} else if(lastOperator =='='){
result = inNum;
}
txtResult.setText(String.valueOf(result));
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 14


ANDROID LAB 3RD SEMESTER MSC

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 15


ANDROID LAB 3RD SEMESTER MSC

3. Write a program for concatenation of two strings using


startActivity for result and toast.
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">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity>">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="click to move" />
</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 16


ANDROID LAB 3RD SEMESTER MSC

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

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"> <requestFocus/>

</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
</LinearLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 17


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.concatandtoast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent in=new Intent(MainActivity.this,second.class);
startActivityForResult(in,1);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK&&requestCode==1)
{
String msg=data.getStringExtra("SOME_DATA");
Toast toast=Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG);
toast.show();
}
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 18


ANDROID LAB 3RD SEMESTER MSC

Second.java

package com.example.concatandtoast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class second extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
final EditText text=(EditText)findViewById(R.id.editText);
final EditText text1=(EditText)findViewById(R.id.editText2);
Button btn=(Button)findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
Intent intent=new Intent(second.this,MainActivity.class);
String msg1=text.getText().toString();
String msg2=text1.getText().toString();
intent.putExtra("SOME_DATA",msg1+""+msg2);
setResult(RESULT_OK,intent);
finish();
}
});
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 19


ANDROID LAB 3RD SEMESTER MSC

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.concatandtoast">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second"></activity>
</application>
</manifest>
OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 20


ANDROID LAB 3RD SEMESTER MSC

4. Write a program for implementing existing content provider.


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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="66dp"
android:layout_marginLeft="32dp">
<requestFocus/>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="16dp"
android:onClick="onClick"
android:text="Button" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 21


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.contentprovider;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
Button search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (Button)findViewById(R.id.button1);
}

@Override
public void onClick(View view){
final EditText name=(EditText)findViewById(R.id.editText1);
final String searchContact = name.getText().toString();
displayContacts(searchContact);
}

public void displayContacts( String searchContact){


if (searchContact == null || searchContact.equals(" ")){

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 22


ANDROID LAB 3RD SEMESTER MSC

Toast.makeText(this, "Empty Name,cannot find contact",


Toast.LENGTH_SHORT).show();
return;
}
Cursor cur = getContentResolver()
.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0){
while ((cur.moveToNext()){
String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
String name =cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String emailAddress = null;
String phoneNo = null;
if (searchContact.equalsIgnoreCase(name)){
If(Integer.parseInt(cur.getString(
cur.getColumnIndex(
ContactsContract.Contacts
.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone
.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone
.CONTACT_ID + "=?", new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
break;
}
pCur.close();
}
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone
.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.
CONTACT_ID + "=?", new String[]{id}, null);

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 23


ANDROID LAB 3RD SEMESTER MSC

while (emails.moveToNext()) {
emailAddress = emails.getString(emails.getColumnIndex(
ContactsContract.CommonDataKinds.Email.DATA));
break;
}
emails.close();
cur.close();
String msg = "Name" + name + ",Phone No" + phoneNo + "\n"
msg += ",Email id:" + emailAddress;
Toast.makeText(MainActivity.this, msg,
Toast.LENGTH_SHORT).show();
return;
}
}
Toast.makeText(this, "couldnt find contact" + searchContact,
Toast.LENGTH_SHORT).show();
}
}
}

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contentprovider">
<uses-permission
android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission
android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 24


ANDROID LAB 3RD SEMESTER MSC

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

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 25


ANDROID LAB 3RD SEMESTER MSC

5. Write a program for Database.


MainActivity.java
package com.example.database;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private String tableName = DBHelper.tableName;
private SQLiteDatabase newDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
}

private void displayResultList(){


TextView tView = new TextView(this);
tView.setText("this data is retrived from database");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,results));
getListView().setTextFilterEnabled(true);
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 26


ANDROID LAB 3RD SEMESTER MSC

private void openAndQueryDatabase(){


try {
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM "
+tableName+ " where Age>=20 LIMIT 6",null);
if(c!=null){
if(c.moveToFirst()){
do {
String firstName =
c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
results.add("Name:" +firstName+ ",Age:" +age);
}while (c.moveToNext());
}
}
}catch (SQLiteException se)
{
Log.e(getClass().getSimpleName(),"coluld not create or open database");
}finally {
if(newDB!=null)
newDB.execSQL("DELETE FROM " +tableName);
newDB.close();
}
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 27


ANDROID LAB 3RD SEMESTER MSC

DBHelper.java
package com.example.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {


public SQLiteDatabase DB;
public String DBPath;
public static String DBName = "sample";
public static final int version = '1';
public static Context currentContext;
public static String tableName = "Resource";
public DBHelper(Context context){
super(context,DBName,null,version);
currentContext = context;
DBPath = "/data/data" +context.getPackageName()+ "/databases";
createDatabase();
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
}
private void createDatabase()
{
DB = currentContext.openOrCreateDatabase(DBName,0,null);
DB.execSQL("CREATE TABLE IF NOT EXISTS " +tableName+
"(LastName VARCHAR,FirstName VARCHAR, "
+" country VARCHAR, Age INNT(3));");

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 28


ANDROID LAB 3RD SEMESTER MSC

DB.execSQL("INSERT INTO "+tableName+" values('M','sneha','India',25);");


DB.execSQL("INSERT INTO "+tableName+" values('c','ranju','India',20);");
DB.execSQL("INSERT INTO "+tableName+"values('p','thanatos','Argentina',25);");
DB.execSQL("INSERT INTO "+tableName+" values('v','preacher','omen',25);");
}
private boolean checkDbExists()
{
SQLiteDatabase checkDB = null;
try{
String myPath = DBPath + DBName;
checkDB = SQLiteDatabase.openDatabase(
myPath,null,SQLiteDatabase.OPEN_READONLY);
}catch (SQLiteException e){
}
if(checkDB!=null){
checkDB.close();
}
return checkDB!=null?true:false;
}
} OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 29


ANDROID LAB 3RD SEMESTER MSC

6. Write a program for fragment.


Acitvity_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">
<fragment
android:id="@+id/lm_fragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/pm_fragment"
android:name="com.example.frag.PM_Fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"/>
</LinearLayout>

Lm_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#7bae16">

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 30


ANDROID LAB 3RD SEMESTER MSC

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/landscape_message"
android:textColor="#000000"
android:textSize="20px"/>
</LinearLayout>

Pm_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#666666"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/TextView2"
android:text="@string/portrait_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20px"/>
</LinearLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 31


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
LM_Fragment ls_fragments=new LM_Fragment();
fragmentTransaction.replace(android.R.id.content,ls_fragments);
}
else{
PM_Fragment pm_fragemts = new PM_Fragment();
fragmentTransaction.replace(android.R.id.content,pm_fragemts);
}
fragmentTransaction.commit();
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 32


ANDROID LAB 3RD SEMESTER MSC

Lm_fragment.java
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class LM_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState){
return inflater.inflate(R.layout.lm_fragment,container,false);
}
}

pm_fragment.java
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class PM_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.pm_fragment, container, false);
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 33


ANDROID LAB 3RD SEMESTER MSC

OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 34


ANDROID LAB 3RD SEMESTER MSC

7. Write a program for gallery.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent">
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp">
</Gallery>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="80dp"
android:layout_below="@+id/gallery1"
android:src="@drawable/pic1"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 35


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.gallery;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Integer[] imageIDs={R.drawable.pic1,R.drawable.pic2};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery g=(Gallery)findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Toast.makeText(getBaseContext()," ",
Toast.LENGTH_SHORT).show();
ImageView iv=(ImageView)findViewById(R.id.imageView1);
iv.setImageResource(imageIDs[position]);
}
});
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 36


ANDROID LAB 3RD SEMESTER MSC

public class ImageAdapter extends BaseAdapter{


private Context context;
private int i;
public ImageAdapter(Context c)
{
context=c;
}
@Override
public int getCount()
{
return imageIDs.length;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View covertview, ViewGroup parent)
{
ImageView imageView=new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150,120));
imageView.setBackgroundResource(i);
return imageView;
}
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 37


ANDROID LAB 3RD SEMESTER MSC

OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 38


ANDROID LAB 3RD SEMESTER MSC

8.Write a program for implicit intents with chooser (Launch


map,Launch market,Send Email).

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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/LaunchMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LaunchMap"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="16dp"
android:onClick="process"/>
<Button
android:id="@+id/LaunchMarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LaunchMarket"
android:layout_toRightOf="@+id/LaunchMap"
android:onClick="process"/>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 39


ANDROID LAB 3RD SEMESTER MSC

<Button
android:id="@+id/sendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SendEmail"
android:layout_toRightOf="@+id/LaunchMarket"
android:onClick="process"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.implicitchooser;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void process(View view)
{
Intent i=null,chooser=null;
if(view.getId()==R.id.LaunchMap)
{
i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("geo:12.948936.77.572913"));
chooser=Intent.createChooser(i,"launch maps");
startActivity(chooser);
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 40


ANDROID LAB 3RD SEMESTER MSC

if(view.getId()==R.id.LaunchMarket)
{
i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=com.whatsapp"));
chooser=Intent.createChooser(i,"Launch Market");
startActivity(chooser);
}
if(view.getId()==R.id.sendEmail)
{
i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("mailto:"));
String[]to={
"navesh.sapad@gmail.com","abhishekravindra7@gmail.com"
};
i.putExtra(Intent.EXTRA_EMAIL,to);
i.putExtra(Intent.EXTRA_SUBJECT,"Hi this was sent from my app");
i.putExtra(Intent.EXTRA_TEXT,"Hey whats's up,how are you? This is
my first Email message");
i.setType("message/rfe822");
chooser=Intent.createChooser(i,"send email");
startActivity(chooser);
}
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 41


ANDROID LAB 3RD SEMESTER MSC

OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 42


ANDROID LAB 3RD SEMESTER MSC

9.Write a program for Implicit Intents.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:layout_alignParentTop="true"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner"
android:layout_centerVertical="true"
android:layout_marginTop="72dp"
android:layout_marginRight="54dp"
android:onClick="onClick"
android:text="Button"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 43


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.implicitintents;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.spinner);
List<String> Is=new ArrayList<String>();
Is.add("0");
Is.add("1");
Is.add("2");
Is.add("3");
Is.add("4");
Is.add("5");
ArrayAdapter adapter = newArrayAdapter(
this,android.R.layout.simple_spinner_item,Is);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 44


ANDROID LAB 3RD SEMESTER MSC

public void onClick(View view){


int position = spinner.getSelectedItemPosition();
Intent intent = null;
switch(position){
case 0:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
break;
case 1:
intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:(+91)123456789"));
break;
case 2:
intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:(+91)123456789"));
break;
case 3:
intent=new Intent("android.media.action.IMAGE_CAPTURE");
break;
case 4:
intent=new Intent(Intent.ACTION_VIEW,Uri.parse(
"content://contacts/people"));
break;
case 5:
intent=new Intent(Intent.ACTION_EDIT,Uri.parse(
"content://contacts/people/1"));
break;
}
if(intent!=null)
{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 45


ANDROID LAB 3RD SEMESTER MSC

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if (resultCode== Activity.RESULT_OK&&requestCode==0)
{
String result=data.toURI();
Toast.makeText(this,result,Toast.LENGTH_LONG);
}
}
}

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.implicitintents">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 46


ANDROID LAB 3RD SEMESTER MSC

OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 47


ANDROID LAB 3RD SEMESTER MSC

10.Write a program for list view.


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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10">
<requestFocus/>
</EditText>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/editText1">
</ListView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 48


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView myListView;
private EditText myEditText;
private ArrayList todoItems;
private ArrayAdapter aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView)findViewById(R.id.listView1);
myEditText = (EditText)findViewById(R.id.editText1);
todoItems = new ArrayList();
int resID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter(this,resID,todoItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int KeyCode, KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_UP)
if(KeyCode == KeyEvent.KEYCODE_ENTER)
{
String s = myEditText.getText().toString();
todoItems.add(s);
myEditText.setText("");

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 49


ANDROID LAB 3RD SEMESTER MSC

aa.notifyDataSetChanged();
return true;
}
return false;
}
});
registerForContextMenu(myListView);
}
}
OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 50


ANDROID LAB 3RD SEMESTER MSC

11.Write a program for menu.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp">
</ListView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 51


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.menu;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void CreateMenu(Menu menu) {
MenuItem mnum1 = menu.add(0, 0, 0, "Item 1");
{
mnum1.setAlphabeticShortcut('a');
}
MenuItem mnum2 = menu.add(0, 1, 1, "Item 2");
{
mnum2.setAlphabeticShortcut('b');
}
MenuItem mnum3 = menu.add(0, 2, 2, "Item 3");
{
mnum3.setAlphabeticShortcut('c');
}
MenuItem mnum4 = menu.add(0, 3, 3, "Item 4");
{
mnum4.setAlphabeticShortcut('d');
menu.add(0, 4, 4, "Item 5");
menu.add(0, 5, 5, "Item 6");
menu.add(0, 6, 6, "Item 7");
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 52


ANDROID LAB 3RD SEMESTER MSC

private boolean MenuChoice(MenuItem item) {


switch (item.getItemId()) {
case 0:
Toast.makeText(this, "you clicked on item 1", Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this, "you clicked on item 2", Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, "you clicked on item 3", Toast.LENGTH_LONG).show();
return true;
case 3:
Toast.makeText(this, "you clicked on item 4", Toast.LENGTH_LONG).show();
return true;
case 4:
Toast.makeText(this, "you clicked on item 5", Toast.LENGTH_LONG).show();
return true;
case 5:
Toast.makeText(this, "you clicked on item 6", Toast.LENGTH_LONG).show();
return true;
case 6:
Toast.makeText(this, "you clicked on item 7", Toast.LENGTH_LONG).show();
return true;
}
return false;
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 53


ANDROID LAB 3RD SEMESTER MSC

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}
}
OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 54


ANDROID LAB 3RD SEMESTER MSC

12. Write a program for Shared Preference.


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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="123dp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/seekBar1"
android:layout_marginLeft="43dp"
android:ems="10"/>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 55


ANDROID LAB 3RD SEMESTER MSC

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="44dp"
android:layout_alignLeft="@+id/editText1"
android:text="Button" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.sharedpreference;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SharedPreferences prefs;
private String prefName = "MyPref";
private EditText editText;
private SeekBar seekBar;
private Button btn;
private static final String FONT_SIZE_KEY = "fontsize";
private static final String TEXT_VALUE_KEY = "textvalue";

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 56


ANDROID LAB 3RD SEMESTER MSC

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText1);
seekBar = (SeekBar)findViewById(R.id.seekBar1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat(FONT_SIZE_KEY, editText.getTextSize());
editor.putString(TEXT_VALUE_KEY,
editText.getText().toString());
editor.commit();
Toast.makeText(getBaseContext(), "fontsize saved successfully",
Toast.LENGTH_SHORT).show();
}
});
SharedPreferences prefs =
getSharedPreferences(prefName,MODE_PRIVATE);
float fontsize = prefs.getFloat(FONT_SIZE_KEY,12);
seekBar.setProgress((int)fontsize);
editText.setText(prefs.getString(TEXT_VALUE_KEY," "));
editText.setTextSize(seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
editText.setTextSize(progress);
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 57


ANDROID LAB 3RD SEMESTER MSC

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 58


ANDROID LAB 3RD SEMESTER MSC

13. Write a program for concatenation of two strings.


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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="click"/>
</RelativeLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 59


ANDROID LAB 3RD SEMESTER MSC

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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:ems="10">
<requestFocus/>
</EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:ems="10"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="enter"/>
</LinearLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 60


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.concatination;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button =(Button)findViewById(R.id.button1);
button.setTextColor(Color.parseColor("#FF0000"));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,second.class);
startActivity(intent);
}
});
TextView name=(TextView) findViewById(R.id.textView1);
if(getIntent()!=null && getIntent().getExtras()!=null &&
getIntent().getExtras().getString("EXTRA_MSG")!=null){
String msg = getIntent().getExtras().getString("EXTRA_MSG");
name.setText(msg);
}
else{
name.setText("Click the Button");
}
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 61


ANDROID LAB 3RD SEMESTER MSC

Second.java
package com.example.concatination;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class second extends Activity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
final EditText text =(EditText) findViewById(R.id.editText1);
final EditText text1 =(EditText) findViewById(R.id.editText2);
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(second.this,MainActivity.class);
String msg = text.getText().toString();
String msg1 = text1.getText().toString();
intent.putExtra("EXTRA_MSG",msg+""+msg1);
startActivity(intent);
}
});
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 62


ANDROID LAB 3RD SEMESTER MSC

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.concatination">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 63


ANDROID LAB 3RD SEMESTER MSC

OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 64


ANDROID LAB 3RD SEMESTER MSC

14. Program for Notification.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="Click to view notification"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="20dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="Send Notification"
android:layout_marginTop="62dp"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

</android.support.constraint.ConstraintLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 65


ANDROID LAB 3RD SEMESTER MSC

Notification.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_marginTop="30dp"
android:text="detailed notification"/>
</LinearLayout>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 66


ANDROID LAB 3RD SEMESTER MSC

MainActivity.java
package com.example.notify;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {


Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNotification();
}
});
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 67


ANDROID LAB 3RD SEMESTER MSC

private void addNotification() {


NotificationCompat.Builder builder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("NotificationExample")
.setContentText("TEXT NOTIFICATION");
Intent notificationIntent = new Intent(this,NotificationView.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent);
NotificationManager manager =(NotificationManager)getSystemService(
Context.NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}

NotificationView.java
package com.example.notify;
import android.app.Activity;
import android.os.Bundle;
public class NotificationView extends Activity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
}
}

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 68


ANDROID LAB 3RD SEMESTER MSC

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notify">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationView"/>
</application>
</manifest>

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 69


ANDROID LAB 3RD SEMESTER MSC

OUTPUT

NATIONAL DEGREE COLLEGE,BASAVANGUDI Page 70

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