This is my coding. I don't know how to validate the radio button. When I click the next button,it still can proceed eventhough the radio button is not clicked. Sorry for my bad English.Please help me. Thank you.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dengue_ques2);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupDengue2);
yes2 = (RadioButton) findViewById(R.id.radioButtonQues2); //the answer is yes
no2 = (RadioButton) findViewById(R.id.radioButtonQues2No); //the answer is no
Next=(Button) findViewById(R.id.buttonNextQues2);
yes2.setSelected(true);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.radioButtonQues2)
{
selectedType = yes2.getText().toString();
}
else if (i == R.id.radioButtonQues2No)
{
selectedType = no2.getText().toString();
}
/* this part does not work
else if (i == R.id.radioButtonQues2.isChecked(false) &&
i == R.id.radioButtonQues2No.isChecked(false))
{
Toast.makeText(this, "Please choose 1 answer",
Toast.LENGTH_SHORT).show();
}*/
}
});
Next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(dengueQues2.this, dengueQues3.class);
intent.putExtra("REST2", selectedType);
startActivity(intent);
}
});
}
public void onClickYes2 (View view)
{
Toast.makeText(this, "You have select 1 to 3 days.",Toast.LENGTH_SHORT).show();
}
public void onClickNo2 (View view)
{
Toast.makeText(this, "You have select 3 to 14 days.", Toast.LENGTH_SHORT).show();
}
You need to use the setChecked() method not the setSelected() method on the radio button.
Also you need to default selectedType = yes2.getText().toString(); since you are setting that as the default selection in onCreate().
yes2.setChecked(true);
selectedType = yes2.getText().toString();
EDIT showing how to use no default selection:
Next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(yes2.isChecked() || no2.isChecked()) {
Intent intent = new Intent(dengueQues2.this, dengueQues3.class);
intent.putExtra("REST2", selectedType);
startActivity(intent);
} else {
Toast.makeText(this, "Please choose 1 answer",
Toast.LENGTH_SHORT).show();
}
}
});
Related
as follows:
Button btnSubmit = findViewById(R.id.btn_login_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_login_submit:
Toast.makeText(LoginActivity.this, "click", Toast.LENGTH_SHORT).show();
break;
}
}
});
Button btnSubmit = findViewById(R.id.btn_login_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(LoginActivity.this, "click", Toast.LENGTH_SHORT).show();
}
});
They both worked equally well. Can the switch be omitted?
The btnSubmit has been findViewById. Is switch getId repeated?
Yes you can remove switch case it work same because R.id.btn_login_submit is same
But my suggestion is use view binding instead of findViewById
In my app I have one button for changing theme. And I have 3 different themes. I found a code in github perfect to me but in that code using radio button for changing theme. And i dont wanna use radio button, the user should see different theme evertime he clicked theme button. So i checked internet if there is a sample for converting radio button to clickListener but i couldn't find any.
public void showDialog(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
RadioGroup radioGroup = dialog.findViewById(R.id.radioGroup);
Button submitBtn = dialog.findViewById(R.id.submit);
Button cancelBtn = dialog.findViewById(R.id.cancel);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int itemChecked) {
switch (itemChecked) {
case R.id.themeBlue:
editor.putInt("theme", R.style.BlueTheme);
editor.apply();
break;
case R.id.themeOrange:
editor.putInt("theme", R.style.OrangeTheme);
editor.apply();
break;
case R.id.themeGreen:
editor.putInt("theme", R.style.DefaultTheme);
editor.apply();
break;
}
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
recreate();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}
This is the code on github
themeButton = findViewById(R.id.themeButton);
themeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
and this is mine.Can you help me please?
i have a problem.
I have 2 textviews that are increased on button click, but i don't know how to pass textview's value to the next activity that also have the same 2 textviews. Like increasing the value by 1 on first activity then on the second activity on the same textview there will be the value 1 and so on...
I'm trying to pass the textview value to next activity, i tried a few methods with putextra methods, but it didn't worked.
There is the code
Button btnIncrement;
private CheckBox i1, i2, i3;
public Button bckbutton;
TextView rspCorecte;
TextView rspGresite;
int counterCorecte=0;
int counterGresite=0;
private void initialStates(Intent intent) {
i1=findViewById(R.id.q1_1);
i2=findViewById(R.id.q1_2);
i3=findViewById(R.id.q1_3);
}
Then...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intrebarea_1);
initialStates(getIntent());
final AlertDialog.Builder alert2=new AlertDialog.Builder(Intrebarea1.this);
LayoutInflater factry=LayoutInflater.from(Intrebarea1.this);
final View view2=factry.inflate(R.layout.raspuns_corect, null);
alert2.setView(view2);
alert2.setPositiveButton("", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
final AlertDialog.Builder alertadd = new AlertDialog.Builder(Intrebarea1.this);
LayoutInflater factory = LayoutInflater.from(Intrebarea1.this);
final View view = factory.inflate(R.layout.raspuns1, null);
alertadd.setView(view);
alertadd.setPositiveButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
startActivity(new Intent(Intrebarea1.this,Intrebarea2.class));
finish();
}
});
// SEMNUL INTREBARII
//reference of button added in the main layout
final Button raspuns = findViewById(R.id.arataraspuns);
//setting click event listener to button
raspuns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Intrebarea1.this);
View view = LayoutInflater.from(Intrebarea1.this).inflate(R.layout.raspuns1, null);
builder.setView(view);
builder.show();
}
});
bckbutton=(Button) findViewById(R.id.backbutton);
bckbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
btnIncrement=findViewById(R.id.btnIncrement);
rspCorecte=findViewById(R.id.rspCorecte);
rspGresite=findViewById(R.id.rspGresite);
btnIncrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(i1.isChecked() && !i2.isChecked() && !i3.isChecked()){
alert2.show();
counterCorecte++;
}
else if (i1.isChecked() && i2.isChecked() && i3.isChecked()){
alertadd.show();
counterGresite++;
}
else if(i2.isChecked() && i3.isChecked()){
alertadd.show();counterGresite++;
}
else if(i1.isChecked() && i2.isChecked()){
alertadd.show();counterGresite++;
}
else if(i1.isChecked() && i3.isChecked()){
alertadd.show();counterGresite++;
}
else if(i2.isChecked()){
alertadd.show();counterGresite++;
}
else if(i3.isChecked()){
alertadd.show();counterGresite++;
}
btnIncrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
raspuns.setVisibility(View.VISIBLE);
btnIncrement.setVisibility(View.GONE);
}
});
rspCorecte.setText(String.valueOf(counterCorecte));
rspGresite.setText(String.valueOf(counterGresite));
}
}); }
public void bckButton(){
Intent intent=new Intent(this, AlegeMediulDeInvatare.class);
startActivity(intent);
finish();
}
public void openMainMenu(){
Intent intent=new Intent(this, MainMenu.class);
startActivity(intent);
finish();
}
public void onBackPressed(){
AlertDialog.Builder inchiziteoria = new AlertDialog.Builder(Intrebarea1.this, R.style.AlertDialogStyle);
LayoutInflater inchizi=LayoutInflater.from(Intrebarea1.this);
View view=inchizi.inflate(R.layout.inchizi_teoria, null);
inchiziteoria.setView(view);
// Set the positive button
inchiziteoria.setPositiveButton("DA", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
openMainMenu();
}
});
// Set the negative button
inchiziteoria.setNegativeButton("NU", null);
// Create the alert dialog
AlertDialog dialog = inchiziteoria.create();
// Finally, display the alert dialog
dialog.show();
((Button)dialog.getButton(dialog.BUTTON_POSITIVE)).setTypeface(null, Typeface.BOLD);
((Button)dialog.getButton(dialog.BUTTON_NEGATIVE)).setTypeface(null, Typeface.BOLD);
// Change the alert dialog background color
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
}
public void openUrmatoarea(View v){
Intent intent=new Intent(this, Intrebarea2.class);
startActivity(intent);
finish();
}
}
Can someone tell me how to pass the textview value to the next activity?
Thanks in advance.
Intent intent = new Intent(getBaseContext(), MainActivity.class);
// change the MainActivity if you want to pass the value to another activity
intent.putExtra("text", text);
startActivity(intent);
And in the next activity where you want to get the value :
Intent intent = getIntent();
String text = intent().getStringExtra("text");
I'm confused where to put this code so that the user will not go to the other page until he selected a radiobutton.
This is the code where I don’t know to put:
if(rda.isChecked() == true)
{
butNext.setEnabled(true);
}
if(rdb.isChecked() == true)
{
butNext.setEnabled(true);
}
if(rdc.isChecked() == true)
{
butNext.setEnabled(true);
}
else
{
butNext.setEnabled(false);
}
This is my whole code or MainAct.java:
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
RadioGroup radioGroup1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
DBase db=new DBase(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.rda);
rdb=(RadioButton)findViewById(R.id.rdb);
rdc=(RadioButton)findViewById(R.id.rdc);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qid<10)
{
currentQ=quesList.get(qid);
setQuestionView();
}
else
{
Intent intent = new Intent(MainAct.this, activity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
rda.setChecked(false);
rdb.setChecked(false);
rdc.setChecked(false);
//butNext.setEnabled(false);
}
if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qid<10)
{
currentQ=quesList.get(qid);
setQuestionView();
}
else
{
Intent intent = new Intent(MainAct.this, activity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
or inOnCheckChanged:
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){
butNext.setEnabled(true);
}else{
butNext.setEnabled(false);
}
}
});
You can simply call it OnCheckedChangeListener() method of your RadioGroup.
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){
butNext.setEnabled(true);
}else{
butNext.setEnabled(false);
}
}
});
For your query you simply need to start Intent only when this Radio button has been clicked.
For Ex: Use condition check which will insure that your radio box has been checked..
i.e
if( radio_b.ischecked()){
// start Intent over here
}else{
// Toast a message to user to select same
}
I hope this clears your doubt.
As far placing code is concerned then you can place same in onCreate(); Method only. That actually depends upon your need.
Cheers!
i have to check simentaneously one one radiobutton and see the value of timeone if ab1 is clicked through toast.. , how can i see it .. i am using is "rab"+number of radiobutton.ischecked() method. is it ok or anyother method should be used ?
public class Alarmsettings extends Activity {
RadioButton rab1,rab2,rab3,rab0;
RadioGroup rg;
Button ab1,ab2;
public Integer timeone;
public Alarmsettings()
{
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
rab0=(RadioButton) findViewById(R.id.radio0);
rab1=(RadioButton) findViewById(R.id.radio1);
rab2=(RadioButton) findViewById(R.id.radio2);
rab3=(RadioButton) findViewById(R.id.radio3);
ab1=(Button) findViewById(R.id.button1);
ab2=(Button) findViewById(R.id.button2);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
if(rab0.isChecked())
timeone=15;
else if(rab1.isChecked())
timeone=30;
else if(rab2.isSelected())
timeone=45;
else if(rab3.isChecked())
timeone=60;
ab1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),timeone.toString(), Toast.LENGTH_SHORT).show();
Try assigning the values through flags rather than assigning it directly
if(rab0.isChecked())
flag=1;
else if(rab1.isChecked())
flag=2;
else if(rab2.isSelected())
flag=3;
else if(rab3.isChecked())
flag=4;
ab1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(flag == 1)
timeone=15;
else if(flag == 2)
timeone=30;
else if(flag == 3)
timeone=45;
else if(flag == 4)
timeone=60;
Toast.makeText(getApplicationContext(),timeone.toString(), Toast.LENGTH_SHORT).show();
Hope this helps.