Disable Android studio buttons

0

Good I need to disable the buttons of my application of a radio until this one executes the stream of music, and then that they work with normality since if I touch a button before initiating the stream it closes. Try with an if and boton.setEnabled but then the buttons do not work even after starting the radio. Thank you very much already.

Java code:

public class MainActivity extends Activity {
ImageButton btnPlay;
int a;

ImageButton btnRec;
ImageButton btnStp;
ImageButton btnSal;
Button btnMul;
Button btnRa;
Button btnNos;

MediaPlayer mediaplayer;
String stream="http://streams.calmradio.com/api/43/128/stream/;";
boolean prepared,started=false;

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

    btnPlay = (ImageButton) findViewById(R.id.play);
    btnRec = (ImageButton) findViewById(R.id.reconectar);
    btnStp = (ImageButton) findViewById(R.id.parar);
    btnSal = (ImageButton) findViewById(R.id.salir);
    btnMul = (Button) findViewById(R.id.multimedia);
    btnRa = (Button) findViewById(R.id.radio);
    btnNos = (Button) findViewById(R.id.nosotros);

    final Toast toast = Toast.makeText(getApplicationContext(), "Esperando respúesta del Servidor...", Toast.LENGTH_LONG);
    toast.show();

    mediaplayer = new MediaPlayer();
    mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    new PlayerTask().execute(stream);



  //botones Radio
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (started){
                started=false;
                mediaplayer.pause();
                btnPlay.setBackgroundResource(R.drawable.play);
                Toast toast=Toast.makeText(getApplicationContext(),"Pausa",Toast.LENGTH_SHORT);
                toast.show();
            }else{
                started=true;
                mediaplayer.start();
                btnPlay.setBackgroundResource(R.drawable.pause);
                Toast toast=Toast.makeText(getApplicationContext(),"Play",Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    });

    btnStp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaplayer.stop();
            Toast toast=Toast.makeText(getApplicationContext(),"Stop",Toast.LENGTH_SHORT);
            toast.show();
        }
    });
    btnRec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaplayer.stop();
            Intent Recargar =getIntent();
            finish();
            startActivity(Recargar );
        }
    });

    //Botones Navegacion
    btnSal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaplayer.release();
            System.exit(0);
        }
    });

    btnNos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent Nosotros=new Intent(getApplicationContext(),Nosotros.class);
            startActivity(Nosotros);
            mediaplayer.stop();

        }
    });

    btnMul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent Multimedia=new Intent(getApplicationContext(),Multimedia.class);
            startActivity(Multimedia);
            mediaplayer.stop();
        }
    });



}

 class PlayerTask extends AsyncTask<String,Void,Boolean>{


    protected Boolean doInBackground(String...String){
        try{
            mediaplayer.setDataSource(String[0]);
            mediaplayer.prepare();
            prepared=true;
        }catch(IOException e){
            e.printStackTrace();
        }
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean){

        super.onPostExecute(aBoolean);
        mediaplayer.start();

    }

    private class BecomingNoisyReceiver extends BroadcastReceiver{


        @Override
        public void onReceive(Context context, Intent intent) {
            if(AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction()));
        }
    }

}

}

    
asked by Eliseo Baroni 13.10.2018 в 23:14
source

2 answers

1

From the layout you can start the buttons as disabled.

android:clickable="false"

And once you have the answer of your asynchronous task, you can update it by setting it to enabled.

yourButton.setEnabled(true);

Do not forget that when you update the interface from another thread you will have to tell your program to do it in the main thread, since it can lead to crashes in execution.

I hope I have helped you.

    
answered by 22.10.2018 / 16:00
source
0

You can start the application with the controls disabled. And, in the absence of an event that tells you when to re-enable them, you can defer the execution of a Runnable to enable them a couple of seconds later.

boton.setEnabled(false);
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        boton.setEnabled(true);
    }
},2000);
    
answered by 14.10.2018 в 03:23