deyanmalinov

Android / Myau

Jun 13th, 2021 (edited)
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. ---MainActivity.java---
  2.  
  3. package com.example.myau;
  4.  
  5. import androidx.appcompat.app.AppCompatActivity;
  6.  
  7. import android.media.MediaPlayer;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ImageButton;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         ImageButton button;
  20.         button = (ImageButton) findViewById(R.id.imageButton);
  21.         final MediaPlayer mp = MediaPlayer.create(this, R.raw.meow);
  22.         button.setOnClickListener(new View.OnClickListener() {
  23.             @Override
  24.             public void onClick(View v) {
  25.                 mp.start();
  26.             }
  27.         });
  28.     }
  29. }
  30.  
  31. ---activity_main.xml---
  32.  
  33. <?xml version="1.0" encoding="utf-8"?>
  34. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  35.     xmlns:app="http://schemas.android.com/apk/res-auto"
  36.     xmlns:tools="http://schemas.android.com/tools"
  37.     android:layout_width="match_parent"
  38.     android:layout_height="match_parent"
  39.     tools:context=".MainActivity">
  40.  
  41.     <ImageButton
  42.         android:id="@+id/imageButton"
  43.         android:layout_width="200dp"
  44.         android:layout_height="200dp"
  45.         android:scaleType="fitXY"
  46.         android:soundEffectsEnabled="true"
  47.         app:layout_constraintBottom_toBottomOf="parent"
  48.         app:layout_constraintEnd_toEndOf="parent"
  49.         app:layout_constraintStart_toStartOf="parent"
  50.         app:layout_constraintTop_toTopOf="parent"
  51.         app:srcCompat="@drawable/mac" />
  52.  
  53. </androidx.constraintlayout.widget.ConstraintLayout>
Add Comment
Please, Sign In to add comment