android apps

Published on February 2017 | Categories: Documents | Downloads: 59 | Comments: 0 | Views: 715
of 22
Download PDF   Embed   Report

Comments

Content

Android Practical Exam
12 Understanding content providers and permissions:
a. Read phonebook contacts using content providers and display in list.
Pro12Activity.java :
package ps.pro12;
import
import
import
import
import
import

android.app.Activity;
android.database.Cursor;
android.net.Uri;
android.os.Bundle;
android.widget.ArrayAdapter;
android.widget.ListView;

public class Pro12Activity extends Activity {
ListView mylist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mylist=(ListView) findViewById(R.id.listView1);
//Uri u=Uri.parse("content://contacts/people");
Uri u=Phones.CONTENT_URI;
Cursor cur=managedQuery(u, null,null,null,null);
String names[]=new String[cur.getCount()];
int i=0;
while(cur.moveToNext())
{
names[i++]=cur.getString(15)+"ntttt"+cur.getString(7);
}
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,names);
mylist.setAdapter(aa);
}
}

13 Read messages from the mobile and display it on the screen.

Pro13Activity.java :
package ps.pro13;
import
import
import
import
import

android.app.Activity;
android.database.Cursor;
android.net.Uri;
android.os.Bundle;
android.widget.TextView;

public class Pro13Activity extends Activity {
TextView txtmsg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtmsg=(TextView) findViewById(R.id.TextViewmsgs);
Uri u=Uri.parse("content://sms/inbox");
Cursor cur=managedQuery(u, null, null, null, null);
String msg="";
while(cur.moveToNext())
{
msg+=cur.getString(2)+":\n\t"+cur.getString(11)+"\n\n";
}
txtmsg.setText(msg);
}
}

14 Create an application to call specific entered number by user in the EditText
Pro14Activity.java :
package ps.pro14;
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.Intent;
android.net.Uri;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;

public class Pro14Activity extends Activity implements OnClickListener {
EditText num;
Button dial;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
num=(EditText) findViewById(R.id.editTextnum);
dial=(Button) findViewById(R.id.buttondial);
dial.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==dial.getId())
{
Intent myintent=new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:"+num.getText()));
startActivity(myintent);
}
}
}

15 Create an application that will create database with table of User credential.

PRO15Activity.java :
package vc.PRO15;
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.Intent;
android.database.Cursor;
android.database.sqlite.SQLiteDatabase;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.widget.AdapterView;
android.widget.AdapterView.OnItemClickListener;
android.widget.ArrayAdapter;
android.widget.ListView;
android.widget.TextView;
android.widget.Toast;

public class PRO15Activity extends Activity implements OnItemClickListener {
SQLiteDatabase db;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db=openOrCreateDatabase("database1",SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("create table if not exists stud(id integer primary key
AUTOINCREMENT,name text,age integer)");

lv =(ListView) findViewById(R.id.list);
lv.setOnItemClickListener(this);
Cursor cur=db.query("stud",null, null, null, null, null, null);
if(cur.getCount()>0)
{
String name[]=new String[cur.getCount()];
int i=0;
while(cur.moveToNext())
{
name[i++]=cur.getString(1);
}
ArrayAdapter<string> aa=new
ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,name);
lv.setAdapter(aa);
}
else
{
Toast.makeText(this,"DataBase is Empty...", 1000).show();
}
}
@Override
public void onItemClick(AdapterView p, View v, int pos, long c) {
// TODO Auto-generated method stub
Intent myintent=new Intent(this,update.class);
TextView iname=(TextView) v;
myintent.putExtra("label",iname.getText().toString() );
startActivity(myintent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(0,5,0,"Insert");
menu.add(0,1,0,"Update");
menu.add(0,2,0,"Delete");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
if(item.getItemId()==5)
{
Intent myintent=new Intent(this,update.class);
myintent.putExtra("flag",item.getItemId());
startActivity(myintent);
}
else if(item.getItemId()==1)
{
Intent myintent=new Intent(this,update.class);
myintent.putExtra("flag",item.getItemId());
startActivity(myintent);
}
else if(item.getItemId()==2)
{
Intent myintent=new Intent(this,update.class);

myintent.putExtra("flag",item.getItemId());
startActivity(myintent);
}
return true;
}
}
</string></string>

Insert.java :
package vc.PRO15;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class insert extends Activity

{

SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db=openOrCreateDatabase("database1",SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("create table if not exists stud(id integer primary key,name
text,age integer)");
}
}

Update.java :
package vc.PRO15;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.ContentValues;
android.content.Intent;
android.database.Cursor;
android.database.sqlite.SQLiteDatabase;
android.os.Bundle;
android.text.Editable;
android.text.TextWatcher;
android.view.View;
android.view.View.OnClickListener;
android.widget.AdapterView;
android.widget.AdapterView.OnItemClickListener;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.EditText;
android.widget.ListView;
android.widget.TextView;
android.widget.Toast;

public class update extends Activity implements OnClickListener, TextWatcher,
OnItemClickListener {
SQLiteDatabase db;
EditText txtname,txtage,txtid;
Button btnedit,btncancel;
TextView lblage;
ListView lv;
String name[];
int flag=-1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
db=openOrCreateDatabase("database1",SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("create table if not exists stud(id integer primary key
AUTOINCREMENT,name text,age integer)");
lblage=(TextView) findViewById(R.id.textView2);
txtname=(EditText) findViewById(R.id.txtname);
txtage=(EditText) findViewById(R.id.txtage);
txtid=(EditText) findViewById(R.id.txtid);
btnedit=(Button) findViewById(R.id.btnedit);
btncancel=(Button) findViewById(R.id.btncancel);
lv=(ListView) findViewById(R.id.list);
lv.setVisibility(-1);
txtid.setVisibility(-1);
Intent myintent=getIntent();
flag=myintent.getIntExtra("flag", 0);
if(flag==0)
{
try
{
String label=myintent.getStringExtra("label");
Cursor cur=db.query("stud",null,"name=?",new String []{label},null,
null,null);
cur.moveToFirst();
Toast.makeText(this,cur.getString(0)+"", 1).show();
txtid.setText(cur.getString(0)+"");
txtname.setText(cur.getString(1));
txtage.setText(cur.getString(2));
btnedit.setText("Update");
txtage.setEnabled(true);
}
catch(Exception e)
{
Toast.makeText(this,e.toString(), 1).show();
}
}
else if(flag==1)
{
txtname.addTextChangedListener(this);
lv.setVisibility(0);
invisible();
txtage.setEnabled(false);

}
else if(flag==2)
{
txtname.addTextChangedListener(this);
lv.setVisibility(0);
invisible();
btnedit.setText("Delete");
}
else if(flag==5)
{
btnedit.setText("Save");
}
btnedit.setOnClickListener(this);
btncancel.setOnClickListener(this);
lv.setOnItemClickListener(this);
}
public void invisible()
{
lblage.setVisibility(-1);
txtage.setVisibility(-1);
btncancel.setVisibility(-1);
btnedit.setVisibility(-1);
}
public void visible()
{
lblage.setVisibility(0);
txtage.setVisibility(0);
btncancel.setVisibility(0);
btnedit.setVisibility(0);
}
public void clear()
{
txtid.setText("");
txtname.setText("");
txtage.setText("");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button action=(Button) v;
if(action.getText().toString().equals("Edit"))
{
txtage.setEnabled(true);
btnedit.setText("Update");
}
else if(action.getText().toString().equals("Update"))
{
ContentValues value=new ContentValues();
value.put("name", txtname.getText().toString());
value.put("age",txtage.getText().toString());
db.update("stud",value, "id=?", new String[]{txtid.getText().toString()});
btnedit.setText("Edit");
Toast.makeText(this,"Update Successfully",1000).show();
startActivity(new Intent(this,PRO15Activity.class));
}
else if(action.getText().toString().equals("Delete"))
{

db.delete("stud", "id=?",new String[]{txtid.getText().toString()});
Toast.makeText(this, "Record Deleted...", Toast.LENGTH_LONG).show();
startActivity(new Intent(this,PRO15Activity.class));

}
else if(action.getText().toString().equals("Save"))
{
if(txtname.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter Name ...", 1).show();
}
else if(txtage.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter Age ...", 1).show();
}
else
{
try
{
Cursor cur=db.query("stud", null, "name=?",new String[]
{txtname.getText().toString()},null, null,null);
if(cur.getCount()>0)
{
Toast.makeText(this,"Name Allready Exists ...",1).show();
}
else
{
ContentValues values=new ContentValues();
values.put("name", txtname.getText().toString());
values.put("age",txtage.getText().toString());
db.insert("stud",null, values);
Toast.makeText(this,"Record Saved...", 1).show();
}
}
catch(Exception e)
{
Toast.makeText(this,e.toString(), 1).show();
}
}
}
else if(v.getId()==btncancel.getId())
{
startActivity(new Intent(this,PRO15Activity.class));
}
}
public void enabled()
{
txtage.setEnabled(true);
}
public void disabled()
{
txtage.setEnabled(false);
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
Cursor cur=db.rawQuery("select *from stud where name like
'"+txtname.getText().toString()+"%'",null);

int len=cur.getCount();
try
{
if(txtname.getText().toString().equals(""))
{
lv.setAdapter(null);
name[0]="";
}
else if(len>0)
{
if(txtname.getText().toString().equals(""))
{
lv.setAdapter(null);
for(int i=0; i<len; i++)
name[i]="";
}
else if(len>0)
{
name=new String[len];
cur.moveToNext();
for(int i=0; i<len; i++)
{
name[i]=cur.getString(1);
cur.moveToNext();
}
}
ArrayAdapter<String> aa=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,name);
lv.setAdapter(aa);
}
else
{
Toast.makeText(this,"Recoerd Not Found...", 1000).show();
}
}
catch(Exception e)
{
Toast.makeText(this,e.toString(),1000).show();
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onItemClick(AdapterView<?> p, View v, int pos, long c) {
// TODO Auto-generated method stub
TextView iname=(TextView) v;
String sname=iname.getText().toString();
Cursor cur=db.rawQuery("select *from stud",null);
if(cur.getCount()>0)

{

while(cur.moveToNext())
{
if(sname.equals(cur.getString(1)))
{
txtid.setText(cur.getInt(0)+"");
txtname.setText(cur.getString(1));
txtage.setText(cur.getString(2));
break;
}
}
visible();
lv.setVisibility(-1);

}
}

}

16 Create an application to read file from asset folder and copy it in memory card.

Pro16Activity.java :
package ps.pro16;
import
import
import
import
import
import

java.io.File;
java.io.FileInputStream;
java.io.FileOutputStream;
java.io.IOException;
java.io.InputStream;
java.io.OutputStream;

import
import
import
import

android.app.Activity;
android.os.Bundle;
android.widget.TextView;
android.widget.Toast;

public class Pro16Activity extends Activity {
TextView tvmsgfromasset,tvmsgfromsdcard;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
tvmsgfromasset=(TextView) findViewById(R.id.textView2);
tvmsgfromsdcard=(TextView) findViewById(R.id.textView4);
//--------reading from asset folder----------InputStream is1=null;
is1=getResources().getAssets().open("hello.txt");

if(is1!=null)
{
Toast.makeText(this, "File Exists", Toast.LENGTH_LONG).show();
String myMsg1="";
while(is1.available()>0)
{
myMsg1=myMsg1+(char)is1.read();
}
is1.close();
tvmsgfromasset.setText(myMsg1);
//--------writing to sdcard----------byte b[]=myMsg1.getBytes();
File myFile = new File("/sdcard/hello.txt");
OutputStream os=new FileOutputStream(myFile);
os.write(b);
os.close();
Toast.makeText(this, "write Success.", Toast.LENGTH_LONG).show();
//--------read file from sdcard-------InputStream is2=null;
is2=new FileInputStream(myFile);
String myMsg2="";
while(is2.available()>0)
{
myMsg2=myMsg2+(char)is2.read();
}
is2.close();
tvmsgfromsdcard.setText(myMsg2+"");

}
}

catch (IOException e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

}

}

}

17 Create an application that will play a media file from the memory card.

PRO17Activity.java :
package vc.PRO17;
import android.app.Activity;
import android.database.Cursor;

import
import
import
import
import
import
import
import
import
import
import
import

android.graphics.Color;
android.media.MediaPlayer;
android.os.Bundle;
android.provider.MediaStore.Audio.Media;
android.view.View;
android.view.View.OnClickListener;
android.widget.AdapterView;
android.widget.AdapterView.OnItemClickListener;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.ListView;
android.widget.Toast;

public class PRO17Activity extends Activity implements OnClickListener,
OnItemClickListener {
Button btnpre,btnstart,btnnext;
String song[];
MediaPlayer player;
ListView lv;
boolean flag=false;
static int j=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnpre=(Button) findViewById(R.id.btnpre);
btnstart=(Button) findViewById(R.id.btnstart);
btnnext=(Button) findViewById(R.id.btnnext);
lv=(ListView) findViewById(R.id.list);
btnpre.setOnClickListener(this);
btnstart.setOnClickListener(this);
btnnext.setOnClickListener(this);
lv.setOnItemClickListener(this);
btnpre.setTextColor(Color.GREEN);
btnstart.setTextColor(Color.GREEN);
btnnext.setTextColor(Color.GREEN);
try
{
Cursor cur=managedQuery(Media.EXTERNAL_CONTENT_URI, null, null, null,
null);

song=new String[cur.getCount()];
int i=0;
while(cur.moveToNext())
{
song[i]=cur.getString(1).substring(12);
i++;
}
ArrayAdapter<string> aa=new
ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,song);
lv.setAdapter(aa);
}
catch(Exception e)
{
Toast.makeText(this,e.toString(),1).show();

}
}
public void song_play()
{
try
{
if(flag==true)
player.stop();
player=new MediaPlayer();
player.setDataSource("/sdcard/"+song[j]);
player.prepare();
player.start();
flag=true;

}

catch(Exception e)

{

Toast.makeText(this,"Play"+j, 1).show();

}

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button action =(Button) v;
if(v.getId()==btnpre.getId())
{
if(j>0)
{
j--;
song_play();
}
else
{
song_play();
}
}
else if(action.getText().toString().equals("|>"))
{
song_play();
btnstart.setBackgroundResource(R.drawable.pause);
btnstart.setText("||");
}
else if(action.getText().toString().equals("||"))
{
player.stop();
btnstart.setBackgroundResource(R.drawable.play);
btnstart.setText("|>");
}
else if(v.getId()==btnnext.getId())
{
if(song.length-1>j)
{
j++;
song_play();
}
else
{

song_play();

}

}
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
j=arg2;
song_play();
btnstart.setText("||");
btnstart.setBackgroundResource(R.drawable.pause);
}
}
</string></string>

18 Create an application to make Insert , update , Delete and retrieve operation on the
database.

DataBaseActivity.java :
package vc.dataBase;
import
import
import
import
import
import

android.app.Activity;
android.content.Intent;
android.database.sqlite.SQLiteDatabase;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;

public class DataBaseActivity extends Activity {
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// db.openDatabase("Data1",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db=openOrCreateDatabase("Data1",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("create table if not exists student(id integer primary key,
name text not null,age integer not null,phone text not null)");
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
//When You are put icon in Menu then create the Menu item object
menu.add(0,0,0,"Insert");
menu.add(0,1,0,"Update");
menu.add(0,2,0,"Delete");

menu.add(0,3,0,"View");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
Intent myIntent=new Intent(this,InsertActivity.class);
myIntent.putExtra("label",item.getItemId());
if(item.getItemId()==0)
{
this.startActivity(myIntent);
}
else if(item.getItemId()==1)
{
this.startActivity(myIntent);
}
else if(item.getItemId()==2)
{
this.startActivity(myIntent);
}
else if(item.getItemId()==3)
{
this.startActivity(myIntent);
}
return true;
}

}

19 Create an application to read file from the sdcard and display that file content to the
screen.

Pro19Activity.java :
package ps.pro19;
import
import
import
import
import
import
import

java.io.File;
java.io.FileInputStream;
java.io.InputStream;
android.app.Activity;
android.os.Bundle;
android.widget.TextView;
android.widget.Toast;

public class Pro19Activity extends Activity {
TextView tvcontent;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvcontent=(TextView) findViewById(R.id.textView2);
try
{
File myFile=new File("/sdcard/hello.txt");
InputStream in=null;
in=new FileInputStream(myFile);
String msg="";
while(in.available()>0)
{
msg=msg+(char)in.read();
}
in.close();
tvcontent.setText(msg);

}

}
catch(Exception e)
{
Toast.makeText(this, e.toString(),Toast.LENGTH_LONG).show();
}

}

20 Create an application to draw line on the screen as user drag his finger.

Pro20Activity.java :
package ps.pro20;
import android.app.Activity;
import android.os.Bundle;
public class Pro20Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ScreenActivity(this));
}
}<span style="color: #0b5394;"><b>
</b></span>

ScreenActivity.java :
package ps.pro20;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
public class ScreenActivity extends View
{
/**
* www.master-gtu.blogspot.com
* pankaj sharma(8460479175),
* chavda vijay(8460420769)
*/
Paint paintBrush=new Paint(Paint.ANTI_ALIAS_FLAG);
Path path=new Path();
public ScreenActivity(Context context) {
super(context);
paintBrush.setColor(Color.RED);
paintBrush.setStrokeWidth(5);
paintBrush.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawPath(path, paintBrush);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
super.onTouchEvent(event);
//------getting position------float x=event.getX();
float y=event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(x, y);
break;
}
invalidate();
return true;

}
}

21 Create an application to send message between two emulators.

Pro21Activity.java :
package ps.pro21;
import
import
import
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.telephony.gsm.SmsManager;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;

public class Pro21Activity extends Activity implements OnClickListener {
EditText ednumber, edmsg;
Button btsend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ednumber=(EditText) findViewById(R.id.editTextnumber);
edmsg=(EditText) findViewById(R.id.EditTextmsg);
btsend=(Button) findViewById(R.id.buttonsend);
btsend.setOnClickListener(this);
}
private void sendsms(String num,String msg)
{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(num, null, msg, null, null);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sendsms(ednumber.getText().toString(),edmsg.getText().toString());;
}
}

22 Create an application to take picture using native application.

Pro22Activity.java :
Note: Give permission in manifest file as "android.permission.CAMERA"

package ps.pro22;
import android.app.Activity;
import android.content.Intent;

import
import
import
import
import
import

android.graphics.Bitmap;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.ImageView;

public class Pro22Activity extends Activity implements OnClickListener {
ImageView img;
Button btcapture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView) findViewById(R.id.imageView1);
btcapture=(Button) findViewById(R.id.buttoncapture);
btcapture.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myintent=new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(myintent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap myimage=(Bitmap) data.getExtras().get("data");
img.setImageBitmap(myimage);
}
}

23 Create an application to pick up any image from the native application gallery and
display it on the screen.

Pro23Activity.java :
Note : Drop Images on Sdcard then go to menus open "Dev-Tools" -> "Media Scanner"

package ps.pro23;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.BitmapFactory;

import
import
import
import
import
import
import
import
import

android.net.Uri;
android.os.Bundle;
android.provider.MediaStore.Images.Media;
android.util.Log;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.ImageView;
android.widget.Toast;

public class Pro23Activity extends Activity implements OnClickListener {
ImageView img;
Button btnext, btprevious;
Cursor cur;
int total,current;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView) findViewById(R.id.imageView1);
btnext=(Button) findViewById(R.id.buttonnext);
btprevious=(Button) findViewById(R.id.buttonprevious);
btprevious.setEnabled(false);
btnext.setOnClickListener(this);
btprevious.setOnClickListener(this);
//Note Media Class is inside MediaStore.Images for images [see 8th line]
//Uri umedia=Uri.parse(Media.EXTERNAL_CONTENT_URI);
Uri umedia=Uri.parse("content://media/external/images/media");
cur=managedQuery(umedia, null, null,null,null);
total=cur.getCount();
current=1;
cur.moveToNext();
img.setImageBitmap(BitmapFactory.decodeFile(cur.getString(1)));

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==btnext.getId())
{
current++;
cur.moveToNext();
img.setImageBitmap(BitmapFactory.decodeFile(cur.getString(1)));
if(current==total)
{
btnext.setEnabled(false);
}
btprevious.setEnabled(true);
}
else if(v.getId()==btprevious.getId())

{

current--;
cur.moveToPrevious();
img.setImageBitmap(BitmapFactory.decodeFile(cur.getString(1)));
if(current==1)
{
btprevious.setEnabled(false);
}
btnext.setEnabled(true);

}
}

}

24 Create an application to open any URL inside the application and clicking on any link
from that URl should not open Native browser but that URL should open the same
screen.
Note:Give "INTERNET" permission in android Manifest file.

WebViewActivity.java :
package ps.webview;
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
android.webkit.WebView;
android.webkit.WebViewClient;
android.widget.Button;
android.widget.TextView;
android.widget.Toast;

public class WebviewActivity extends Activity implements OnClickListener
{
//CREATE WEBVIEW OBJECT
WebView web;
TextView edurl;
Button btgo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// INTIALIZE WEBVIEW OBJECT
// GIVE PERMISSION INTERNET IN ANDROIDMAINFEST FILE
web=(WebView) findViewById(R.id.webView1);
edurl=(TextView) findViewById(R.id.editTexturl);
btgo=(Button) findViewById(R.id.buttongo);

btgo.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(edurl.getText().toString().equals(""))
{
Toast.makeText(this, "Pls Enter URL...", 1).show();
}
else
{
String stringurl=edurl.getText().toString();
if(!stringurl.startsWith("http://") &amp;&amp;
stringurl.startsWith("https://") )
stringurl="http://"+stringurl;

!

//ENABELING JAVA SCRIPT ON WEBVIEW
web.getSettings().setJavaScriptEnabled(true);
//LOAD URL ON WEBVIEW
web.loadUrl(stringurl);
//SETTING WEBVIEW CLIENT TO OPEN IN SAME SCREEN NOT ON NATIVE BROWSER
web.setWebViewClient(new WebViewClient());
}

}
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close