Ex9 Merged
Ex9 Merged
Code:
MainActivity.java:
package com.example.ninth;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
@Override
public void onServiceDisconnected(ComponentName name) {
isBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, MediaPlayerService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (isBound) {
unbindService(serviceConnection);
isBound = false;
}
}
MediaPlayerService.java
package com.example.ninth;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.amrit_bani);
if (mediaPlayer != null) {
mediaPlayer.setLooping(true);
} else {
Toast.makeText(getApplicationContext(), "Failed to create MediaPlayer",
Toast.LENGTH_SHORT).show();
stopSelf(); // Stop the service if MediaPlayer creation fails
return START_NOT_STICKY; // Don't restart the service
}
}
if (isPaused) {
mediaPlayer.seekTo(playbackPosition);
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Music is Resumed", Toast.LENGTH_SHORT).show();
isPaused = false;
} else {
if (mediaPlayer != null) {
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Music is Playing", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "MediaPlayer is not initialized",
Toast.LENGTH_SHORT).show();
}
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<Button
android:id="@+id/btnPlay"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="66dp"
android:layout_marginTop="474dp"
android:layout_marginEnd="211dp"
android:layout_marginBottom="209dp"
android:onClick="playMusic"
android:backgroundTint="#A19D9D"
android:fontFamily="@font/cinzel_bold"
android:text="Play/Pause"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnStop"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginTop="474dp"
android:layout_marginEnd="65dp"
android:layout_marginBottom="209dp"
android:backgroundTint="#A19D9D"
android:onClick="stopMusic"
android:fontFamily="@font/cinzel_bold"
android:text="Stop"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="238dp"
android:layout_height="279dp"
android:layout_marginStart="84dp"
android:layout_marginTop="97dp"
android:layout_marginEnd="89dp"
android:layout_marginBottom="355dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/oip2" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ninth"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MediaPlayerService"
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
android:exported="false" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
Output:
Experiment No.-10: Write a program to draw circles, rectangles and other shapes using canvas.
Code:
MainActivity.java
package com.example.tenth;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shapeView = findViewById(R.id.shapeView);
buttonCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawCircle();
}
});
buttonRectangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawRectangle();
}
});
buttonTriangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawTriangle();
}
});
buttonPolygon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawPolygon();
}
});
}
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"
android:background="@color/white"
tools:context=".MainActivity">
<Button
android:id="@+id/c"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="71dp"
android:layout_marginTop="467dp"
android:layout_marginEnd="220dp"
android:layout_marginBottom="214dp"
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Circle"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/r"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="221dp"
android:layout_marginTop="467dp"
android:layout_marginEnd="71dp"
android:layout_marginBottom="214dp"
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Rectangle"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/t"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="71dp"
android:layout_marginTop="554dp"
android:layout_marginEnd="220dp"
android:layout_marginBottom="127dp"
Subject: Mobile Application Development Lab
khushdeep 11212510 A1
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Triangle"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/p"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="221dp"
android:layout_marginTop="554dp"
android:layout_marginBottom="127dp"
android:backgroundTint="#655B50"
android:fontFamily="@font/cinzel_bold"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:text="Polygon"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.tenth.ShapeView
android:id="@+id/shapeView"
android:layout_width="389dp"
android:layout_height="350dp"
android:layout_marginStart="11dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="11dp"
android:layout_marginBottom="367dp"
android:background="@color/white"
android:backgroundTint="#FFFFF0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
ShapeView.java:
package com.example.tenth;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (shapeToDraw) {
case CIRCLE:
drawCircle(canvas);
break;
case RECTANGLE:
drawRectangle(canvas);
break;
case LINE:
drawTriangle(canvas);
break;
case POLYGON:
drawPolygon(canvas);
break;
}
}
private void drawCircle(Canvas canvas){
canvas.drawCircle(510, 459,400, paint);
}
canvas.drawLines(points, paint);
}
public void setShape(Shape shape) {
shapeToDraw = shape;
invalidate(); // Redraw the view
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Tenth"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>