Sei sulla pagina 1di 23

Q1 Write a Program to Print HelloWorld ?

Code:package com.Demo.mc1;
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

Output:

Q2 Write a Program to print Log Status ?


Code:package com.cm.mc2;
import
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.util.Log;
android.view.Menu;
android.view.MenuItem;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("MCA","Verbos Log Executed");
Log.d("MCA","Debugging log executed");
Log.i("MCA","Info log executed");
Log.e("MCA","Error log executed");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Output:-

Q3 Write a Program to show Toast Message ?


Code:package com.mc.mc3;
import
import
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.util.Log;
android.view.Menu;
android.view.MenuItem;
android.widget.Toast;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("MCA","Verbos Log Executed");
Log.d("MCA","Debugging log executed");
Log.i("MCA","Info log executed");
Log.e("MCA","Error log executed");
Toast.makeText(getApplicationContext(), "Toast is called", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

Output:-

Q4
Code:package com.mc.mc4;
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.util.Log;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
android.widget.Toast;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("MCA","Verbos Log Executed");
Log.d("MCA","Debugging log executed");
Log.i("MCA","Info log executed");
Log.e("MCA","Error log executed");
Toast.makeText(getApplicationContext(), "Toast is called", Toast.LENGTH_LONG).show();
final EditText edittext = (EditText) findViewById(R.id.editText1);

Button but = (Button) findViewById(R.id.button1);


but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
TextView textview = (TextView) findViewById(R.id.textView2);
textview.setText(edittext.getText().toString());
}
});

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Output:-

Q5 Write a program to use following:a)Group RadioButton

b)CheckBox
c)SwitchButton
d)ToggleButton
Code:package com.wp.changewp;
import java.io.IOException;
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.app.WallpaperManager;
android.content.Context;
android.media.AudioManager;
android.os.Bundle;
android.os.Vibrator;
android.view.Menu;
android.widget.CheckBox;
android.widget.CompoundButton;
android.widget.CompoundButton.OnCheckedChangeListener;
android.widget.RadioGroup;
android.widget.Switch;
android.widget.Toast;
android.widget.ToggleButton;

public class MainActivity extends Activity {


ToggleButton tb;
Switch sw;
CheckBox cb;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb=(ToggleButton)findViewById(R.id.toggleButton1);
sw=(Switch)findViewById(R.id.switch1);
cb=(CheckBox)findViewById(R.id.checkBox1);
rg=(RadioGroup)findViewById(R.id.dd);
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

// TODO Auto-generated method stub


AudioManager
am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(isChecked){
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
else{
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
});
sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
Vibrator
vb=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
if(isChecked){
long[] pattern={1000,3000,5000,2000};
vb.vibrate(pattern,2);
}
else
{
vb.cancel();
}
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "Cb is checked",
Toast.LENGTH_SHORT).show();
}

else{
Toast.makeText(MainActivity.this, "Cb is Unchecked",
Toast.LENGTH_SHORT).show();
}
}
});
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
WallpaperManager
wm=WallpaperManager.getInstance(MainActivity.this);
switch(checkedId){
case R.id.radioButton1:
try {
wm.setResource(R.drawable.chrysanthemum);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.radioButton2:
try {
wm.setResource(R.drawable.jellyfish);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.radioButton3:
try {
wm.setResource(R.drawable.lighthouse);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
});

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
output:-

Q6 Write a program to make media player ?


code:package com.mp.mediaplayer;
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.Context;
android.media.AudioManager;
android.media.MediaPlayer;
android.os.Bundle;
android.view.Menu;
android.view.View;
android.widget.SeekBar;
android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {


MediaPlayer mp;
SeekBar sb;
AudioManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb=(SeekBar)findViewById(R.id.seekBar1);
am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mp=MediaPlayer.create(this,R.raw.song);
sb.setMax(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
sb.setProgress(sb.getMax());
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
am.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
}
});
}

public void Play(View v)


{
mp.start();
}
public void pause(View v)
{
mp.pause();
}
public void stop(View v)
{
if(isChild()){
mp.stop();
}
else{
mp.start();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
output:-

Q7 Write a program to use Proximity Sensor with Sound ?


code:package com.sen.sensor;
import java.util.Random;
import
import
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.app.NotificationManager;
android.content.Context;
android.hardware.Sensor;
android.hardware.SensorEvent;
android.hardware.SensorEventListener;
android.hardware.SensorManager;
android.os.Bundle;
android.speech.tts.TextToSpeech;
android.speech.tts.TextToSpeech.OnInitListener;
android.support.v4.app.NotificationCompat;
android.view.Menu;
android.widget.ImageView;

public class MainActivity extends Activity implements SensorEventListener {


int[] imagearray={
R.drawable.chrysanthemum,
R.drawable.desert,
R.drawable.hydrangeas,
R.drawable.jellyfish
};
ImageView vi;
SensorManager sensormanager;
Sensor sensor;
TextToSpeech tts;
NotificationManager nm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vi=(ImageView)findViewById(R.id.imageView1);
sensormanager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensor=sensormanager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sensormanager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMA
L);

tts=new TextToSpeech(this, new OnInitListener() {


@Override
public void onInit(int status) {
}
});
nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.values[0]>0){
tts.speak("Thank you", TextToSpeech.QUEUE_FLUSH, null);
}
else{
int x=new Random().nextInt(4);
vi.setImageResource(imagearray[x]);
tts.speak("touch me Not", TextToSpeech.QUEUE_FLUSH, null);
NotificationCompat.Builder builder=new
NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setTicker("Alert Alert");
builder.setContentTitle("Proximitly Alert");
builder.setSmallIcon(R.drawable.chrysanthemum);
builder.setContentText("Some one Near Mobile");
nm.notify(new Random().nextInt(),builder.build());

}
}
}
output:-

Q8 Write a program to Send SMS one device to another Device ?


code:package com.example.smsapp;
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.graphics.Color;
android.os.Bundle;
android.telephony.SmsManager;
android.view.Menu;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.Toast;

public class MainActivity extends Activity {


EditText etphone, etmsg;
Button bsend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etphone=(EditText)findViewById(R.id.editText2);
etmsg=(EditText)findViewById(R.id.editText1);
bsend=(Button)findViewById(R.id.button1);
etphone.setTextColor(Color.BLUE);
}
public void buttonClick(View v)
{
String contact=etphone.getText().toString();
String message=etmsg.getText().toString();
SmsManager smssender= SmsManager.getDefault();
smssender.sendTextMessage(contact,null,message,null,null);
Toast.makeText(this, "send", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
output:-

Q9 Write a program to find current location using mobile Network ?


code:package com.location.location;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import
import
import
import
import
import
import
import
import
import
import
import
import

android.annotation.SuppressLint;
android.app.Activity;
android.content.Context;
android.location.Address;
android.location.Geocoder;
android.location.Location;
android.location.LocationListener;
android.location.LocationManager;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.widget.TextView;

@SuppressLint("NewApi") public class MainActivity extends Activity implements


LocationListener{
LocationManager lm;
Geocoder geo;
TextView tvcoordinate,tvaddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvcoordinate=(TextView)findViewById(R.id.textView2);
tvaddress=(TextView)findViewById(R.id.textView3);
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
}
public void Mylocation(View v){
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 100,
this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
tvcoordinate.setText("lat:"+location.getLatitude()
+"Ing"+location.getLongitude());
lm.removeUpdates(this);
geo=new Geocoder(this,Locale.getDefault());
if(Geocoder.isPresent()){
try{
List<Address>
add=geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
StringBuilder sb=new StringBuilder();
Address addr=add.get(0);
for(int i=0;i<addr.getMaxAddressLineIndex();i++){
sb.append(addr.getAddressLine(i)+"\n");
}
sb.append(addr.getCountryName()+"\n");
sb.append(addr.getPhone()+"\n");
sb.append(addr.getPostalCode()+"\n");
tvaddress.setText(sb.toString());
}catch(IOException e){
e.printStackTrace();
}
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}

output:-

Potrebbero piacerti anche