Botonera Android



import android.media.AudioManager;
import android.media.Image;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;


public class MainActivity extends ActionBarActivity {

    ImageButton imgBut1, imgBut2, imgBut3;
    MediaPlayer mp = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgBut1 = (ImageButton) findViewById(R.id.imageButton);
        imgBut2 = (ImageButton) findViewById(R.id.imageButton2);
        imgBut3 = (ImageButton) findViewById(R.id.imageButton3);

    }


    public void onClickImgButon1(View view){
        mp.reset();
        mp.release();
        mp=MediaPlayer.create(this,R.raw.buenochao); //create a new  media player with the selected id
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //mp.prepare(); //salta exception
        mp.seekTo(0);                             //seek to starting of song means time=0 ms
        mp.start();
    }

    public void onClickImgButon2(View view){
        mp.reset();
        mp.release();
        mp=MediaPlayer.create(this,R.raw.carlitoxidea); //create a new  media player with the selected id
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //mp.prepare(); //salta exception
        mp.seekTo(0);                             //seek to starting of song means time=0 ms
        mp.start();
    }

    public void onClickImgButon3(View view){
        mp.reset();
        mp.release();
        mp=MediaPlayer.create(this,R.raw.chaleco); //create a new  media player with the selected id
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //mp.prepare(); //salta exception
        mp.seekTo(0);                             //seek to starting of song means time=0 ms
        mp.start();
    }


}       

xml

<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"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"
                android:onClick="onClickImgButon1" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton2"
                android:layout_weight="1"
                android:src="@drawable/ale"
                android:onClick="onClickImgButon2" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton3"
                android:layout_weight="1"
                android:src="@drawable/reee"
                android:onClick="onClickImgButon3" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton4"
                android:layout_weight="1" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton5"
                android:layout_weight="1" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton6"
                android:layout_weight="1" />

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton7"
                android:layout_weight="1" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton8"
                android:layout_weight="1" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageButton9"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

6 comentarios:

  1. Muy buen tutorial, sólo que quisiera para cada botón utilizar TextToSpeech, alguna sugerencia de cómo lo podría implementar?

    ResponderEliminar
  2. Hola saludos, excelente tus tutoriales y bien explicados,siendo un miembro nuevo en esto de la prog en android, tengo una duda si quisiera ver unos pequeños videos desde la app serveria este codigo o que tendria que cambiar, si podrias guiarme. muchas gracias

    ResponderEliminar
  3. Hola sabes como hacer que el botón se a grande y se mueva. Con animación scale y translate?

    ResponderEliminar
  4. Hola sabes como hacer que el botón se a grande y se mueva. Con animación scale y translate?

    ResponderEliminar
  5. Hola necesito tu colaboracion he desarrollado el proyecto a fin pero al salir de la app sigue sonando el audio del boton presionado, mi pregunta es como implementar en el mismo boton un stop de audio es decir con el boton di inicio con el mismo boton stop.

    ResponderEliminar
  6. HOLA AMIGOS DESARROLLADORES
    NECESITO AYUDA!
    necesito capturar valores decimales de un edittext y realizar operaciones con ellos

    ResponderEliminar