package com.corporation.ilumian.latihandua;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
Button btnSubmit;
EditText txtNama,txtKelas;
RadioGroup radGroup;
RadioButton radBut1,radBut2;
CheckBox boxBola,boxGame,boxMakan;
String cbBola,cbGame,cbMakan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSubmit = (Button)findViewById(R.id.button);
txtNama = (EditText)findViewById(R.id.editText);
txtKelas = (EditText)findViewById(R.id.editText2);
radGroup = (RadioGroup)findViewById(R.id.radioGroup);
boxBola = (CheckBox)findViewById(R.id.checkBox);
boxGame = (CheckBox)findViewById(R.id.checkBox2);
boxMakan = (CheckBox)findViewById(R.id.checkBox3);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
}
public void showDialog(){
int selectedRb = radGroup.getCheckedRadioButtonId();
radBut1 = (RadioButton)findViewById(selectedRb);
getCheckboxData();
AlertDialog.Builder hasilInput = new AlertDialog.Builder(this);
//set judul
hasilInput.setTitle("Hasil Input");
hasilInput.setMessage("Nama : " + txtNama.getText().toString() + "\n" +
"Kelas : " + txtKelas.getText().toString() + "\n" +
"Kamu Seorang " + radBut1.getText() + "\n" +
"Hobby : " + cbBola + cbGame + cbMakan + "\n").setPositiveButton("Oke", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = hasilInput.create();
alert.show();
}
public void getCheckboxData(){
if (boxBola.isChecked() && boxGame.isChecked() && boxMakan.isChecked()){
cbBola = boxBola.getText().toString() + ", ";
cbGame = boxGame.getText().toString() + ", ";
cbMakan = boxMakan.getText().toString();
}
else {
if (boxBola.isChecked() && boxGame.isChecked()){
cbBola = boxBola.getText().toString() + ", ";
cbGame = boxGame.getText().toString();
}
else{
if (boxBola.isChecked() && boxMakan.isChecked()){
cbBola = boxBola.getText().toString() + ", ";
cbMakan = boxMakan.getText().toString();
}
else {
if (boxBola.isChecked()){
cbBola = boxBola.getText().toString();
}
else {
cbBola = "";
}
}
}
}
}
}
Hello everyone thank you for your time reading my first post I've homework about adding comma automatically on the text in the alert dialogue here is the app look like
the "UI" this is where the alert dialogue take the information
Here is the example when submit button is pressed
now the problem is how to add comma between text on hobby?
before ( the result in the picture )
Hobby : Sepak Bola Main Game Makan
no comma to separate the hobby
here how the alert dialogue should look like automatically when 2 or more checkbox is selected
Hobby : Sepak Bola, Main Game, Makan or Hobby : Sepak Bola, Makan
how to add this comma? without manually typing ...(" , ".. in the code
Sorry if this type of question is already been discussed, I genuinely don't know what keyword to search on google or StackOverflow
Thank you