Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---MainActivity.java---
- package com.example.myau;
- import androidx.appcompat.app.AppCompatActivity;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageButton;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ImageButton button;
- button = (ImageButton) findViewById(R.id.imageButton);
- final MediaPlayer mp = MediaPlayer.create(this, R.raw.meow);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mp.start();
- }
- });
- }
- }
- ---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">
- <ImageButton
- android:id="@+id/imageButton"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:scaleType="fitXY"
- android:soundEffectsEnabled="true"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@drawable/mac" />
- </androidx.constraintlayout.widget.ConstraintLayout>
Add Comment
Please, Sign In to add comment