Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Build a Xylophone Application in Android?

  • Last Updated : 26 Feb, 2021

In this article, we will be building a project on Xylophone using Java and XML in android. Xylophone originally is an instrument played by striking a row of wooden bars. This application will be implementing a Xylophone-like instrument that is capable of producing sounds. We will create few Buttons in this app that will act as keys of the instrument when struck. This is going to be a single activity application. A sample video is given below to get an idea about what we are going to do in this article. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Before going to the coding section first you have to do some pre-task

Add Colors: We need to add colors in the value of the resources directory. We will be using these colors for our Buttons.


<?xml version="1.0" encoding="utf-8"?>
    <color name="green1">#0F9D58</color>
    <color name="green2">#F21BB56A</color>
    <color name="green3">#E620C374</color>
    <color name="green4">#D929CF7E</color>
    <color name="green5">#CC2ED885</color>
    <color name="green6">#BF34E18D</color>
    <color name="green7">#B33FF39B</color>

Add Key Notes: Save these audio notes in the app > res > raw directory

Note: Reference article: Resource Raw Folder in Android Studio

Step 3: Working with the activity_main.xml file

The XML codes are used to build the structure of the activity as well as its styling part. In this XML file, we will create a LinearLayout with vertical orientation. This will contain seven Buttons each with different onClick functions. The width of the button reduces as we go down to give it a look of a xylophone and also to symbolize that the smaller the button less is the note sound. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>

Step 4: Working with the file

In the MainActivity we need to create a new SoundPool. A Soundpool uses MediaPlayer library services to load audio files. We will create a function for each key button with a different audio note. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
    // Helpful Constants
    private final int sim_sound = 7;
    private final float lft_vol = 1.0f;
    private final float rgt_vol = 1.0f;
    private final int loop = 0;
    private final int prty = 0;
    private final float NORMAL_PLAY_RATE = 1.0f;
    // Add member variables here
    private SoundPool mSoundPool;
    private int mCSoundId1;
    private int mDSoundId2;
    private int mESoundId3;
    private int mFSoundId4;
    private int mGSoundId5;
    private int mASoundId6;
    private int mBSoundId7;
    protected void onCreate(Bundle savedInstanceState) {
        // Create a new SoundPool
        mSoundPool = new SoundPool(sim_sound, AudioManager.STREAM_MUSIC, 0);
        // Load and get the IDs to identify the sounds
        mCSoundId1 = mSoundPool.load(getApplicationContext(), R.raw.note1_c, 1);
        mDSoundId2 = mSoundPool.load(getApplicationContext(), R.raw.note2_d, 1);
        mESoundId3 = mSoundPool.load(getApplicationContext(), R.raw.note3_e, 1);
        mFSoundId4 = mSoundPool.load(getApplicationContext(), R.raw.note4_f, 1);
        mGSoundId5 = mSoundPool.load(getApplicationContext(), R.raw.note5_g, 1);
        mASoundId6 = mSoundPool.load(getApplicationContext(), R.raw.note6_a, 1);
        mBSoundId7 = mSoundPool.load(getApplicationContext(), R.raw.note7_b, 1);
    // Add the play methods triggered by the buttons
    public void c (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    public void d (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    // Add the play methods triggered by the buttons
    public void e (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    public void f (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    public void g (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    public void a (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);
    public void b (View v){, lft_vol, rgt_vol, prty,loop,NORMAL_PLAY_RATE);


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!