Android Studio: Tekan Tombol Playsound

From OnnoWiki
Revision as of 18:24, 20 October 2015 by Onnowpurbo (talk | contribs) (New page: ==MainActivity.java== package id.onnocenter.kucing; import android.media.MediaPlayer; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import andr...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

MainActivity.java

package id.onnocenter.kucing;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    MediaPlayer player; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton ImageButton=(ImageButton)findViewById(R.id.imageButton);

        ImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player=MediaPlayer.create(MainActivity.this,R.raw.cow1);
                player.start();
            }
        }); 

    }
}


content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#566780">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Silahkan di Tekan :)"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="30dp"
        android:id="@+id/textView" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:src="@drawable/cow1"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="81dp" /> 

</RelativeLayout>