androidsmsonclicklistener

multiple buttons to send predefined SMS


Hi guys i am new to programming. I am trying to have multiple button to send different predefined SMS to predefined number. I am not sure how to have multiple setOnClickListener(new OnClickListener() as the 2nd setOnClickListener(new OnClickListener() gave me error. Without the "buttonSend2 = (Button) findViewById(R.id.buttonSend2)" the program works fine.

public class SendSMSActivity extends Activity {

    Button buttonSend;
    Button buttonSend2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        buttonSend2 = (Button) findViewById(R.id.buttonSend2);

        buttonSend.setOnClickListener(new OnClickListener() {
        buttonSend2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()) {

                case R.id.buttonSend:
                     Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                     sendIntent.putExtra("sms_body", "abc"); 
                     sendIntent.putExtra("address", "9909990");
                     sendIntent.setType("vnd.android-dir/mms-sms");
                     startActivity(sendIntent);
                     break;

                case R.id.buttonSend2:
                    Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
                     sendIntent1.putExtra("sms_body", "def"); 
                     sendIntent1.putExtra("address", "012345678");
                     sendIntent1.setType("vnd.android-dir/mms-sms");
                     startActivity(sendIntent1);
                     break;
                }

            }
            });
        });
    }

}

Thank you!


Solution

  • Looks like your click listeners were a little confused. Try this:

    buttonSend = (Button) findViewById(R.id.buttonSend);
    buttonSend2 = (Button) findViewById(R.id.buttonSend2);
    
    OnClickListener listener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
            switch (v.getId()) {
    
            case R.id.buttonSend:
                 Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                 sendIntent.putExtra("sms_body", "abc"); 
                 sendIntent.putExtra("address", "9909990");
                 sendIntent.setType("vnd.android-dir/mms-sms");
                 startActivity(sendIntent);
                 break;
    
            case R.id.buttonSend2:
                Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
                 sendIntent1.putExtra("sms_body", "def"); 
                 sendIntent1.putExtra("address", "012345678");
                 sendIntent1.setType("vnd.android-dir/mms-sms");
                 startActivity(sendIntent1);
                 break;
            }
    
        }
    };
    
    buttonSend.setOnClickListener(listener);
    buttonSend2.setOnClickListener(listener);