3 buttons with the same method, differentiate them

0

I had a little problem where I repeat the same code 3 times, and I'm trying to reduce it to one without success, the code I have is this

public void shared(View view ) {
    Intent emailIntent = new Intent( Intent.ACTION_SEND );

    emailIntent.setData( Uri.parse( "mailto:" ) );
    emailIntent.setType( "text/plain" );
    emailIntent.putExtra( Intent.EXTRA_EMAIL, new String[]{} ); // * configurar email aquí!
    emailIntent.putExtra( Intent.EXTRA_SUBJECT, " " );
    emailIntent.putExtra( Intent.EXTRA_TEXT, mensaje );
    //other,whassapp,sGmail
    if(other.callOnClick( )){

    } else if(sGmail.callOnClick()){
        emailIntent.setPackage("com.google.android.gm");
    } else if(whassapp.callOnClick()){
        emailIntent.setPackage("com.whatsapp");
    }

    try {
        startActivity( Intent.createChooser( emailIntent, "Enviar email." ) );
        Log.i( "EMAIL", "Enviando email..." );
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText( this, "NO existe ningún cliente de email instalado!.", Toast.LENGTH_SHORT ).show();
    }

}

And what I want is to know how to detect the button pressed since the 3 call to the same method

    
asked by lujan 15.07.2018 в 10:27
source

1 answer

1

In the end I was able to solve it by making a switch

public void shared(View view ) {
    Intent emailIntent = new Intent( Intent.ACTION_SEND );

    emailIntent.setData( Uri.parse( "mailto:" ) );
    emailIntent.setType( "text/plain" );
    emailIntent.putExtra( Intent.EXTRA_EMAIL, new String[]{} ); // * configurar email aquí!
    emailIntent.putExtra( Intent.EXTRA_SUBJECT, " " );
    emailIntent.putExtra( Intent.EXTRA_TEXT, mensaje );

    switch (view.getId()) {

        case R.id.btn_other:

            break;

        case R.id.btn_gmail:
            emailIntent.setPackage("com.google.android.gm");
            break;

        case R.id.btn_whass:
            emailIntent.setPackage("com.whatsapp");
            break;

        default:
            break;
    }

    try {
        startActivity( Intent.createChooser( emailIntent, "Enviar email." ) );
        Log.i( "EMAIL", "Enviando email..." );
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText( this, "NO existe ningún cliente de email instalado!.", Toast.LENGTH_SHORT ).show();
    }

}
    
answered by 15.07.2018 / 12:27
source