Android Projects

Home  >>  Computer Science  >>  Android Projects

Android Projects

22
Nov,2015

0

1. Activity Life Cycle

package com.example.shagun.activitylifecycle;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
final Context context = this;
String tag=”Life Cycle”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,”In the onCreate() event”);
}
public void onStart()

{
super.onStart();
Log.d(tag, “In the onStart() event”);
}
public void onRestart()
{
super.onRestart();
Log.d(tag,”In the onRestart() event”);
}
public void onResume()

{
super.onResume();
Log.d(tag, “In the onResume() event”);
}
public void onPause()

{
super.onPause();
Log.d(tag, “In the onPause() event”);
}
public void onStop()

{
super.onStop();
Log.d(tag, “In the onStop() event”);
}
public void onDestroy()

{
super.onDestroy();
Log.d(tag, “In the onDestroy() event”);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

2.Toast message

package com.example.shagun.sept7;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);

MenuItem M1=menu.add(0,0,0,”fb”);
M1.setIcon(R.drawable.fb);
M1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

MenuItem M2=menu.add(0,1,1,”whatsapp”);
M2.setIcon(R.drawable.whatsapp);
M2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

MenuItem M3=menu.add(0,2,2,”twitter”);
M3.setIcon(R.drawable.twitter);
M3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
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();

//noinspection SimplifiableIfStatement
if (id==0)
Toast.makeText(this,”YOU PRESSED 1ST ICON”,Toast.LENGTH_SHORT).show();

if(id==1)
Toast.makeText(this, “PRESSED 2ND ICON”, Toast.LENGTH_SHORT).show();
if(id==2)
Toast.makeText(this,”YOU PRESSED 3RD ICON”,Toast.LENGTH_SHORT).show();
if(id==R.id.Home)
Toast.makeText(this,”AT HOME”,Toast.LENGTH_SHORT).show();

return super.onOptionsItemSelected(item);
}
}

in .xml file

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>

<TextView android:text=”@string/hello_world” android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

</RelativeLayout>

 

 3. Action Bars

package com.example.shagun.sept7;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem M1=menu.add(0,0,0,”fb”);
M1.setIcon(R.drawable.fb);
M1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

MenuItem M2=menu.add(0,1,1,”whatsapp”);
M2.setIcon(R.drawable.whatsapp);
M2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

MenuItem M3=menu.add(0,2,2,”twitter”);
M3.setIcon(R.drawable.twitter);
M3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
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();

//noinspection SimplifiableIfStatement
if (id==0)
Toast.makeText(this,”YOU PRESSED 1ST ICON”,Toast.LENGTH_SHORT).show();

if(id==1)
Toast.makeText(this, “PRESSED 2ND ICON”, Toast.LENGTH_SHORT).show();
if(id==2)
Toast.makeText(this,”YOU PRESSED 3RD ICON”,Toast.LENGTH_SHORT).show();
if(id==R.id.Home)
Toast.makeText(this,”AT HOME”,Toast.LENGTH_SHORT).show();

return super.onOptionsItemSelected(item);
}
}

 

4. Image View

in .java

package com.example.shagun.imageview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

ImageView Img=(ImageView)findViewById(R.id.imageView);
Img.setImageResource(R.drawable.memory1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

in .xml
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>

<TextView android:text=”@string/hello_world” android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/textView” />

<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/imageView”
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”129dp” />

</RelativeLayout>

5. Web View

.java

package com.example.shagun.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

}

private class MyBrowser extends WebViewClient

{
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
public void perform(View V) {
ed1 = (EditText) findViewById(R.id.editText);
wv1 = (WebView) findViewById(R.id.webView);

wv1.setWebViewClient(new MyBrowser());

String url = ed1.getText().toString();

url = “http://www.” + url;

Log.d(“Status”, “Opening the URL” +url);

wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
Log.d(“Status”, “Closed”);

}
}

in .xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>

<TextView android:text=”@string/hello_world” android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
<LinerLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:orientation=”horizontal”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/textView”
android:text=”text”
/>

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/editText”
android:hint=”Enter url”
android:width=”250dp”/>
</LinerLayout>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Open URL”
android:id=”@+id/button”
android:onClick=”perform”
android:layout_gravity=”center_horizontal”/>

<WebView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”40dp”
android:id=”@+id/webView”
android:layout_gravity=”center_horizontal”/>
</LinearLayout>
.. in manifest file

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.shagun.webview” >

<uses_permission
android:name=”android.permission.INTERNET”/>
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”

android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

 

6. Text to speech

in .java

//before the onCreate()…
public class MainActivity extends AppCompatActivity {

TextToSpeech T1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
T1=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener()
{
public void onInit(int status)
{
if(status==TextToSpeech.SUCCESS)
T1.setLanguage(Locale.ENGLISH);
else
Toast.makeText(getApplicationContext(),”Error”,Toast.LENGTH_SHORT).show();
}
});
}

public void convert(View v)
{
Log.d(“STatus”, “Running”);
EditText E1=(EditText)findViewById(R.id.editText);
String S=E1.getText().toString();
T1.speak(S, TextToSpeech.QUEUE_FLUSH, null, null);

}

in .xml file

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Text”
android:id=”@+id/Text”
android:layout_alignBottom=”@+id/editText”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/editText”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Speak”

android:id=”@+id/Speak”
android:onClick=”convert”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />

</RelativeLayout>

7. Notification

.java

public void perform(View V)
{
NotificationManager notificationmanager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra(“title”,”new title”);
intent.putExtra(“text”,”new text”);
PendingIntent pIntent= PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap icon= BitmapFactory.decodeResource(getResources(), R.drawable.rdhe);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.rdhe)
.setLargeIcon(Bitmap.createScaledBitmap(icon,128,128,false))
.setContentTitle(“New Message”)
.setContentText(“you received a new message”)
.addAction(android.R.drawable.ic_menu_close_clear_cancel,”Cancel”,pIntent)
.setContentIntent(pIntent)
.setAutoCancel(true);
notificationmanager.notify(0,builder.build());
}

.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”show”
android:id=”@+id/button”
android:onClick=”perform”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>

8. Animations

.java

package com.example.shagun.animat;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

@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.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void perform(View V)
{
int id=V.getId();
ImageView I=(ImageView)findViewById(R.id.imageView);
Animation A=null;
if(id==R.id.z)
A= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom);
else if(id==R.id.r)
A=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate);

else if(id==R.id.f)
A=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);

I.startAnimation(A);
}
}
//res->anim->zoom.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<scale
android:fromXScale=”0.5″
android:toXScale=”3.0″
android:fromYScale=”0.5″
android:toYScale=”3.0″
android:duration=”5000″
android:pivotX=”50%”
android:pivotY=”50%”>
</scale>
<scale
android:startOffset=”5000″
android:fromXScale=”3.0″
android:toXScale=”0.5″
android:fromYScale=”3.0″
android:toYScale=”0.5″
android:duration=”5000″
android:pivotX=”50%”
android:pivotY=”50%”>
</scale>
</set>

res-> anim-> fade.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<alpha
android:fromAlpha=”0″
android:toAlpha=”1″
android:duration=”2000″
>
</alpha>
<alpha
android:startOffset=”2000″
android:fromAlpha=”1″
android:toAlpha=”0″
android:duration=”2000″>
</alpha>
</set>
//res-> anim-> rotate.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<rotate
android:fromDegrees=”0″
android:toDegrees=”360″
android:pivotX=”50%”
android:pivotY=”50%”
android:duration=”5000″>
</rotate>
<rotate
android:startOffset=”5000″
android:fromDegrees=”360″
android:toDegrees=”0″

android:duration=”5000″
android:pivotX=”50%”
android:pivotY=”50%”>
</rotate>
</set>

9. Music Player

package com.example.shagun.musicplayer;

import android.app.AlertDialog;
import android.app.Dialog;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

MediaPlayer Lovely;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Lovely=MediaPlayer.create(this,R.raw.yo);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void playOperation(View v)
{
AlertDialog.Builder B=new AlertDialog.Builder(this) ;
B.setMessage(“Playing the music”).show();
Button B1=(Button)findViewById(R.id.play);

if(!Lovely.isPlaying()) {
Lovely.start();

B1.setText(“Pause”);
}

else
{

Lovely.pause();
B1.setText(“Play”);
}
}
public void stopOperation(View v)
{
Button B1=(Button)findViewById(R.id.play);
Lovely.stop();
B1.setText(“Play”);
Lovely=MediaPlayer.create(this,R.raw.yo);
}

}

in .xml file

<RelativeLayout

xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”

android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”play”
android:id=”@+id/play”
android:onClick=”playOperation”
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”124dp” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”stop”
android:id=”@+id/stop”
android:onClick=”stopOperation”
android:layout_below=”@+id/button”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”73dp” />

</RelativeLayout>

10. File Handling

.java file

public void Store(View v)
{
EditText et1=(EditText)findViewById(R.id.editText);
String filename=et1.getText().toString();
EditText et2=(EditText)findViewById(R.id.editText2);
String value=et2.getText().toString();

try{
FileOutputStream Fout=openFileOutput(filename, Context.MODE_PRIVATE);

Fout.write(value.getBytes());

Toast.makeText(this, “file writing done”, Toast.LENGTH_SHORT);
}

catch(Exception F)
{
Toast.makeText(this,”Error with file operation”,Toast.LENGTH_SHORT).show();
}
}

in .xml file

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Filename”
android:id=”@+id/Filename”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Data”
android:id=”@+id/Data”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”40dp” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/editText”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true” />

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/editText2″
android:layout_below=”@+id/editText”
android:layout_centerHorizontal=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Write”
android:id=”@+id/Write”
android:onClick=”Stop”
android:layout_marginTop=”71dp”
android:layout_below=”@+id/editText2″
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Read”
android:id=”@+id/Read”
android:onClick=”Print”
android:layout_alignTop=”@+id/Write”
android:layout_toRightOf=”@+id/textView3″
android:layout_toEndOf=”@+id/textView3″ />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Text View”
android:id=”@+id/textView3″
android:layout_below=”@+id/Read”
android:layout_alignRight=”@+id/editText2″
android:layout_alignEnd=”@+id/editText2″
android:layout_marginTop=”108dp” />

</RelativeLayout>

11. Splash screen

.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.lang.InterruptedException;

public class Welcome extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Thread timerThread=new Thread(){
public void run(){
try{
int i=0;
while(i<=10){
sleep(1000);
Log.d(“Digit”, “Digits=” + i);
i++;
}

catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent=new Intent(this,output.class);
startActivity(intent);
//startActivity(intent);
}

}

12. Services

.java
package com.example.shagun.myapplication;

import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

MediaPlayer mp;
@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.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void playmusic(View V)
{

mp=MediaPlayer.create(this,R.raw.shgn);
mp.start();
}

public void perform(View V)
{
int id=V.getId();
Intent I=new Intent(this,Playservice.class);
if(id==R.id.st)
startService(I);
else if(id==R.id.sp)
stopService(I);

if (mp!=null)
mp.stop();
finish();
}
}

.xml
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Start”
android:id=”@+id/st”
android:onClick=”perform”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”143dp” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Stop”
android:onClick=”perform”
android:id=”@+id/sp”
android:layout_marginTop=”65dp”
android:layout_below=”@+id/st”
android:layout_alignLeft=”@+id/st”
android:layout_alignStart=”@+id/st” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”play”
android:id=”@+id/p”
android:onClick=”playmusic”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”60dp” />
</RelativeLayout>
Playservices.java

package com.example.shagun.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;

public class Playservice extends Service {
MediaPlayer mp1;
public Playservice() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException(“Not yet implemented”);
}
public void onCreate()
{
mp1= MediaPlayer.create(this, R.raw.shgn);

}

public void onStart(Intent intent,int id)
{
mp1.start();
}

public void onDestroy()
{

mp1.stop();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *