2
votes

I'm new to android studio and trying to send a value checkbox that only checked via SMS message.

I change the value to String so all value checkbox sent though it's unchecked

Please, help me fixing the program so that the value checkbox only checked are sent via SMS message.

Full code:

private EditText qty1, qty2,qty3,qty4,qty5,qty6;
private CheckBox cos5,cos10,cos20,cos25,cos50,cos100;
private Spinner spinnerrs;
private Button buttonsendtrx;

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

    qty1 = (EditText)findViewById(R.id.editTextQty1);
    qty2 = (EditText)findViewById(R.id.editTextQty2);
    qty3 = (EditText)findViewById(R.id.editTextQty3);
    qty4 = (EditText)findViewById(R.id.editTextQty4);
    qty5 = (EditText)findViewById(R.id.editTextQty5);
    qty6 = (EditText)findViewById(R.id.editTextQty6);
    cos5 =(CheckBox)findViewById(R.id.S05);
    cos10=(CheckBox)findViewById(R.id.S10);
    cos20=(CheckBox)findViewById(R.id.S20);
    cos25=(CheckBox)findViewById(R.id.S25);
    cos50=(CheckBox)findViewById(R.id.S50);
    cos100=(CheckBox)findViewById(R.id.S100);

    buttonsendtrx = (Button) findViewById(R.id.buttonsendtrx);
    addItemOnSpinner();

     buttonsendtrx.setOnClickListener(new View.OnClickListener(){
         public void onClick(View view) {
            CheckBox cb = (CheckBox) view;

            if (view==buttonsendtrx);
             {
                 sendsms();
             }
         }
     });
    }


public void sendsms () {

    Log.i("Send SMS","");
    String nomor = spinnerrs.getSelectedItem().toString();
    String jumlah1 = qty1.getText().toString();
    String jumlah2 = qty2.getText().toString();
    String jumlah3 = qty3.getText().toString();
    String jumlah4 = qty4.getText().toString();
    String jumlah5 = qty5.getText().toString();
    String jumlah6 = qty6.getText().toString();
    String s5 = cos5.getText().toString();
    String s10 = cos10.getText().toString();
    String s20 = cos20.getText().toString();
    String s25 = cos25.getText().toString();
    String s50 = cos50.getText().toString();
    String s100 = cos100.getText().toString();


    {
        try {
            SmsManager smsManager = SmsManager.getDefault();

            smsManager.sendTextMessage(nomor, null, s5+jumlah1+s10+jumlah2+s20+jumlah3+s25+jumlah4+s50+jumlah5+s100++jumlah6, null, null);
            Toast.makeText(getApplicationContext(), "SMS SEND", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS FAILED,PLEASE TRY AGAIN", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }
}

public void addItemOnSpinner() {
    spinnerrs = (Spinner) findViewById(R.id.spinnerrs);
    List<String> kategori = new ArrayList<String>();
    kategori.add("08127648200");
    kategori.add("081276482000");
    kategori.add("081275250000");
    ArrayAdapter<String> dataadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,kategori);
    dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerrs.setAdapter(dataadapter);
}

public void selectItem(View view) {

    boolean checked = ((CheckBox) view).isChecked();

    switch (view.getId())
    {
        case R.id.S05:

        if (checked){
            qty1.setBackgroundColor(Color.parseColor("#FFFFFF"));
            qty1.setFocusable(true);
            qty1.setFocusableInTouchMode(true);
            qty1.setCursorVisible(true);
            qty1.setEnabled(true);
            qty1.setVisibility(view.VISIBLE);
            break;
        }
        else {
            //selection.remove(String.valueOf(cos5));
            qty1.setBackgroundColor(Color.parseColor("#CCCCCC"));
            //qty1.setTextIsSelectable(false);
            qty1.setFocusable(false);
            qty1.setFocusableInTouchMode(false);
            qty1.setCursorVisible(false);
            qty1.setEnabled(false);
            qty1.setVisibility(view.INVISIBLE);
            break;
        }

        case R.id.S10:
            if (checked){
                qty2.setBackgroundColor(Color.parseColor("#FFFFFF"));

                qty2.setFocusable(true);
                qty2.setFocusableInTouchMode(true);
                qty2.setCursorVisible(true);
                qty2.setEnabled(true);
                qty2.setVisibility(view.VISIBLE);

                break;
            }
            else {
                qty2.setBackgroundColor(Color.parseColor("#CCCCCC"));

                qty2.setFocusable(false);
                qty2.setFocusableInTouchMode(false);
                qty2.setCursorVisible(false);
                qty2.setEnabled(false);
                qty2.setVisibility(view.INVISIBLE);
                break;
            }

        case R.id.S20:
            if (checked){
                qty3.setBackgroundColor(Color.parseColor("#FFFFFF"));

                qty3.setFocusable(true);
                qty3.setFocusableInTouchMode(true);
                qty3.setCursorVisible(true);
                qty3.setEnabled(true);
                qty3.setVisibility(view.VISIBLE);

                break;
            }
            else {
                qty3.setBackgroundColor(Color.parseColor("#CCCCCC"));

                qty3.setFocusable(false);
                qty3.setFocusableInTouchMode(false);
                qty3.setCursorVisible(false);
                qty3.setEnabled(false);
                qty3.setVisibility(view.INVISIBLE);
                break;
            }

        case R.id.S25:
            if (checked){
                qty4.setBackgroundColor(Color.parseColor("#FFFFFF"));

                qty4.setFocusable(true);
                qty4.setFocusableInTouchMode(true);
                qty4.setCursorVisible(true);
                qty4.setEnabled(true);
                qty4.setVisibility(view.VISIBLE);

                break;
            }
            else {
                qty4.setBackgroundColor(Color.parseColor("#CCCCCC"));
                //qty4.setTextIsSelectable(false);
                qty4.setFocusable(false);
                qty4.setFocusableInTouchMode(false);
                qty4.setCursorVisible(false);
                qty4.setEnabled(false);
                qty4.setVisibility(view.INVISIBLE);
                break;
            }
            case R.id.S50:
            if (checked){
                qty5.setBackgroundColor(Color.parseColor("#FFFFFF"));
                //qty5.setTextIsSelectable(true);
                qty5.setFocusable(true);
                qty5.setFocusableInTouchMode(true);
                qty5.setCursorVisible(true);
                qty5.setEnabled(true);
                qty5.setVisibility(view.VISIBLE);
                break;
            }
            else {
                qty5.setBackgroundColor(Color.parseColor("#CCCCCC"));
                qty5.setFocusable(false);
                qty5.setFocusableInTouchMode(false);
                qty5.setCursorVisible(false);
                qty5.setEnabled(false);
                qty5.setVisibility(view.INVISIBLE);
                break;
            }
        case R.id.S100:
            if (checked){
                qty6.setBackgroundColor(Color.parseColor("#FFFFFF"));
                qty6.setFocusable(true);
                qty6.setFocusableInTouchMode(true);
                qty6.setCursorVisible(true);
                qty6.setEnabled(true);
                qty6.setVisibility(view.VISIBLE);
                break;
            }
            else {
                qty6.setBackgroundColor(Color.parseColor("#CCCCCC"));
                qty6.setFocusable(false);
                qty6.setFocusableInTouchMode(false);
                qty6.setCursorVisible(false);
                qty6.setEnabled(false);
                qty6.setVisibility(view.INVISIBLE);
                break;
            }

    }

  }

}
2
what is your exact question? - W4R10CK
please... how to fix the program so that the value checkbox only checked are sent via sms message. thanx - Coo Key

2 Answers

0
votes
public void sendsms () {

Log.i("Send SMS","");
String nomor = spinnerrs.getSelectedItem().toString();
String jumlah1 = qty1.getText().toString();
String jumlah2 = qty2.getText().toString();
String jumlah3 = qty3.getText().toString();
String jumlah4 = qty4.getText().toString();
String jumlah5 = qty5.getText().toString();
String jumlah6 = qty6.getText().toString();

if(cos5.isChecked){
String s5 = cos5.getText().toString();
return;
}
if(cos10.isChecked){
String s10 = cos10.getText().toString();
return;
}
if(cos20.isChecked){
String s20 = cos20.getText().toString();
return;
}
if(cos25.isChecked){
String s25 = cos25.getText().toString();
return;
}
if(cos50.isChecked){
String s50 = cos50.getText().toString();
return;
}
if(cos100.isChecked){
String s100 = cos100.getText().toString();
return;
}


try {
        SmsManager smsManager = SmsManager.getDefault();

        smsManager.sendTextMessage(nomor, null, s5+jumlah1+s10+jumlah2+s20+jumlah3+s25+jumlah4+s50+jumlah5+s100++jumlah6, null, null);
        Toast.makeText(getApplicationContext(), "SMS SEND", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS FAILED,PLEASE TRY AGAIN", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }


}
0
votes

First Find if the checkbox is checked or not, and add it to a string as follows:-

 String msg_to_send="";
    if(cos5.isChecked()){
    msg_to_send = msg_to_send+cos5.getText().toString();
    }
    if(cos10.isChecked()){
    msg_to_send = msg_to_send+cos10.getText().toString();
    }
    if(cos20.isChecked()){
    msg_to_send = msg_to_send+ cos20.getText().toString();
    }
    if(cos25.isChecked()){
    msg_to_send = msg_to_send+cos25.getText().toString();
    }
    if(cos50.isChecked()){
   msg_to_send = msg_to_send+ cos50.getText().toString();
    }
    if(cos100.isChecked()){
   msg_to_send = msg_to_send+cos100.getText().toString();

    }

Send this string to your sms:-

 smsManager.sendTextMessage(nomor, null, msg_to_send, null, null);//you can add your editText also