how to use if else statement for radiobuttons in android - java

I'm having two RadioGroups in one layout and each radiogroup has three radio buttons. so how can i use if else statement.
[two radiogroups in each group having three radiobuttons.when user clicked on one radiobutton then it will display correct answer.else wrong answer.][1]

Why do you want to use if else statement. You can do it in the easy way.
int radioButtonId = rbGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonId);

You can do like this to get the checked radiobutton from the radioGroup
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
switch (g.getCheckedRadioButtonId())
{
case R.id.rbtnButton1
//do something
break;
case R.id.rbtnButton2
//do something
break;
}

rg = (RadioGroup) findViewById(R.id.radioGroup1);
button = (RadioButton) findViewById(R.id.radio0);
rg.performClick();
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
#SuppressWarnings("unused")
int selected = rg.getCheckedRadioButtonId();
pos = rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));
switch (pos) {
case 0:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 1",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 2",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 3",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 4",
Toast.LENGTH_SHORT).show();
break;
}
}
});

Related

how to check if the random number exists in array

I'm trying to check my random number exists in my array; this is my array
int [] img = {R.drawable.im1,R.drawable.im2}
My function to update data and show it random
private void updateQuestion() {
int ranom_number = random.nextInt(img.length);
image.setImageResource(img[ranom_number]);
ton= MediaPlayer.create(BallActivity.this,Questions.audinb[ranom_number]);
ton.start();
}
this is my class. In my view I have a button I want when the random number is displaying if the user clicks right the other buttons disappear. If he clicks next then all buttons should be displayed. If the last number of array is displaying the activity finish
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
if (ranom_number == img.length) {
bravo = MediaPlayer.create(getApplicationContext(), R.raw.bravo);
bravo.start();
bt2.setVisibility(v.GONE);
}else{
bt1.setVisibility(v.VISIBLE);
bt2.setVisibility(v.VISIBLE);
}
break;
case R.id.btn2:
if(ranom_number <= img.length-1){
}
break;
case R.id.suiv:
if(ranom_number <= imgnb.length-1) {
updateQuestion();
}else{
Intent i = new Intent(BallActivity.this, NewActivity.class);
BallActivity.this.finish();
startActivity(i);
}
}
But doesn't work thank you for helping

Why is the text in TextViews changing two times when I am calling it only once at a time?

I know setText just changes the text only once but I can't seem to find the reason why the text in it changes before I move on to the next question in the quizActivity
What I have made is an app with one activity in it which has a quiz in it, a question is displayed along with 4 options. When the user selects an option, if that option is correct then it becomes green and red otherwise and additionally, I then open a dialog box showing whether the answer was right or wrong and then the question is changed when the user clicks Next on the dialog box.
But what is happening that when the user selects an option, in between the process of clicking the option and then clicking next on the dialog box, the text in the questions and the options changes and I can't seem to figure out why is that happening. In total, the question and options change two times when they should change only once, the unexpected change is when the user clicks on an option and the dialog box opens.
Here's the code:
#Override
public void onClick(View view) {
int selectedOption = 0;
switch (view.getId()) {
case R.id.option_1_tile:
selectedOption = 1;
break;
case R.id.option_2_tile:
selectedOption = 2;
break;
case R.id.option_3_tile:
selectedOption = 3;
break;
case R.id.option_4_tile:
selectedOption = 4;
break;
default:
}
checkAnswer(selectedOption, view);
}
Here's the function which checks the answer:
private void checkAnswer(int selectedOption, View view) {
if (selectedOption == selected_questions.get(quesNum).getAnswer()) {
//Right Answer
(view).setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
quizReference.child(selected_questions.get(quesNum).getId()).child("correct_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getCorrect_attempts()) + 1));
quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
score++;
correctDialog();
} else {
//Wrong Answer
(view).setBackgroundTintList(ColorStateList.valueOf(Color.RED));
quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
switch (selected_questions.get(quesNum).getAnswer()) {
case 1:
options[0].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 2:
options[1].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 3:
options[2].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 4:
options[3].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
}
wrongDialog ();
}
}
Here's the function which changes the question:
private void changeQuestion() {
resetColor ();
if (quesNum < selected_questions.size() - 1) {
quesNum++;
playAnim(question, 0, 0);
playAnim(option1_text, 0, 1);
playAnim(option2_text, 0, 2);
playAnim(option3_text, 0, 3);
playAnim(option4_text, 0, 4);
qCount.setText(String.valueOf(quesNum + 1) + "/" + String.valueOf(selected_questions.size()));
} else {
// Go to Score Activity
Intent intent = new Intent(quizActivity.this, scoreActivity.class);
intent.putExtra("SCORE", String.valueOf(score) + "/" + String.valueOf(selected_questions.size()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
Here's the function which sets the text and animation:
private void playAnim(final View view, final int value, final int viewNum) {
view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500)
.setStartDelay(100).setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
if (value == 0) {
switch (viewNum) {
case 0:
((TextView) view).setText(selected_questions.get(quesNum).getQuestion());
break;
case 1:
((TextView) view).setText(selected_questions.get(quesNum).getOption1());
break;
case 2:
((TextView) view).setText(selected_questions.get(quesNum).getOption2());
break;
case 3:
((TextView) view).setText(selected_questions.get(quesNum).getOption3());
break;
case 4:
((TextView) view).setText(selected_questions.get(quesNum).getOption4());
break;
}
if (viewNum != 0)
(view).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E99C03")));
playAnim(view, 1, viewNum);
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
Here's the code for the dialog boxes:
public void wrongDialog() {
final Dialog dialogWrong = new Dialog(quizActivity.this);
dialogWrong.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogWrong.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogWrong.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogWrong.setContentView(R.layout.dialog_wrong);
dialogWrong.setCancelable(false);
dialogWrong.show();
TextView wrongText = (TextView) dialogWrong.findViewById(R.id.wrongText);
Button buttonNext = (Button) dialogWrong.findViewById(R.id.dialogNext);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogWrong.dismiss();
//reset the color of buttons back to white
resetColor();
//Change question
changeQuestion();
}
});
}
public void correctDialog() {
final Dialog dialogCorrect = new Dialog(quizActivity.this);
dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogCorrect.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogCorrect.setContentView(R.layout.dialog_correct);
dialogCorrect.setCancelable(false);
dialogCorrect.show();
TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
Button buttonNext = (Button) dialogCorrect.findViewById(R.id.dialogNext);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogCorrect.dismiss();
//reset the color of buttons back to white
resetColor();
//it will increment the question number
changeQuestion();
}
});
}
I have tried to explain it to my best ability though I would be glad to answer any additional information/code you may want. Also, this is the link for the project if you have the time to run it and understand the problem better.
I have checked your code. you have placed an addValueEventListener in setUpdates method. When you select an option, you update the firestore database by setting fields like total attempts. As a result, eventListener gets triggered and "selectQuestionSet" function is called.
Hence, every time you select an option, selectQuestionSet function is called. You should make sure that its called only once at the start.

How do I prevent my submit button in android from giving me multiple scores when clicked multiple times?

Whenever I answer a question and hit the submit button, a score of 1 should show but on hitting submit button again, it keeps adding 1 to the score and more hits on the submit button keeps adding more 1 to the score. I actually don't want 1 to be added to the score every time the submit button is clicked. I do not want the score to be updated based on the number of times I hit the submit button.
package com.example.android.generalknowledge;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int baseScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitResult(View view) {
RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
boolean answerIsLargestRailwayStation = largestRailwayStation.isChecked();
RadioButton insects = (RadioButton) findViewById(R.id.insects);
boolean answerIsInsects = insects.isChecked();
CheckBox physicsChemistry = (CheckBox) findViewById(R.id.physics_chemistry);
boolean physicsChemistryIsAnswer = physicsChemistry.isChecked();
CheckBox physiologyMedicine = (CheckBox) findViewById(R.id.physiology_medicine);
boolean physiologyMedicineIsAnswer = physiologyMedicine.isChecked();
CheckBox literature = (CheckBox) findViewById(R.id.literature);
boolean literatureIsAnswer = literature.isChecked();
CheckBox peaceEconomics = (CheckBox) findViewById(R.id.peace_economics);
boolean peaceEconomicsIsAnswer = peaceEconomics.isChecked();
RadioButton naziParty = (RadioButton) findViewById(R.id.nazi_party);
boolean answerIsNaziParty = naziParty.isChecked();
RadioButton allOfTheAbove = (RadioButton) findViewById(R.id.all_of_the_above);
boolean answerIsAll = allOfTheAbove.isChecked();
RadioButton filmFinance = (RadioButton) findViewById(R.id.film_finance);
boolean answerIsFilmFinance = filmFinance.isChecked();
EditText enterNameHere = (EditText) findViewById(R.id.name_view);
String name = enterNameHere.getText().toString();
EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
String answer = enterAnswerHere.getText().toString();
if (enterAnswerHere.getText().toString().equals("Africa")) {
baseScore += 1 ;
}
int finalScore = calculateTotalScore(answerIsLargestRailwayStation, answerIsInsects, physicsChemistryIsAnswer,
physiologyMedicineIsAnswer, literatureIsAnswer, peaceEconomicsIsAnswer, answerIsNaziParty, answerIsAll, answerIsFilmFinance);
Toast.makeText(MainActivity.this,
name + " " + "\n" + "You have a Total Score of " + " " + finalScore + " " + "/10", Toast.LENGTH_LONG).show();
}
/**
* This method is called when any of the radio buttons in question one is selected
*/
public void onRadioButtonClickedOne(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.largest_railway_station:
if (checked)
// This is the correct answer
break;
case R.id.highest_railway_station:
if (checked)
// Wrong answer
break;
case R.id.longest_railway_station:
if (checked)
// Wrong answer
break;
case R.id.none_of_the_above:
if (checked)
//Wrong answer
break;
}
}
/**
* This method is called when any of the radio buttons in question two is selected
*/
public void onRadioButtonClickedTwo(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.behaviour_of_human_beings:
if (checked)
// Wrong answer
break;
case R.id.insects:
if (checked)
// This is the correct answer
break;
case R.id.origin_history:
if (checked)
// Wrong answer
break;
case R.id.rock_formation:
if (checked)
// Wrong answer
break;
}
}
/**
* This method is called when the checkboxes for question three are clicked
*/
public void onCheckboxThreeClicked(View view) {
//Is the button now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox is clicked
switch (view.getId()) {
case R.id.physics_chemistry:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.physiology_medicine:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.literature:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.peace_economics:
if (checked)
// One of the Correct answers
break;
}
}
/**
* This method is called when any of the radio buttons in question four is selected
*/
public void onRadioButtonClickedFour(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.labour_party:
if (checked)
// Wrong answer
break;
case R.id.nazi_party:
if (checked)
// This is the correct answer
break;
case R.id.leading_party:
if (checked)
// Wrong answer
break;
case R.id.democratic_party:
if (checked)
// Wrong answer
break;
}
}
/**
* This method is called when any of the radio buttons in question six is selected
*/
public void onRadioButtonClickedSix(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.develop_telescope:
if (checked)
// Wrong answer
break;
case R.id.discovered_jupiter:
if (checked)
// Wrong answer
break;
case R.id.movement_of_pendulum:
if (checked)
// Wrong answer
break;
case R.id.all_of_the_above:
if (checked)
// This is the correct answer
break;
}
}
/**
* This method is called when any of the radio buttons in question seven is selected
*/
public void onRadioButtonClickedSeven(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.foreign_finance:
if (checked)
// Wrong answer
break;
case R.id.film_finance:
if (checked)
// This is the correct answer
break;
case R.id.federation_football:
if (checked)
// Wrong answer
break;
case R.id.none:
if (checked)
// Wrong answer
break;
}
}
private int calculateTotalScore(boolean questionOneAnswer, boolean questionTwoAnswer, boolean questionThreeAnswer1,
boolean questionThreeAnswer2, boolean questionThreeAnswer3, boolean questionThreeAnswer4,
boolean questionFourAnswer, boolean questionSixAnswer, boolean questionSevenAnswer) {
if (questionOneAnswer) {
baseScore += 1 ;
}
if (questionTwoAnswer) {
baseScore += 1 ;
}
if (questionThreeAnswer1) {
baseScore += 1 ;
}
if (questionThreeAnswer2) {
baseScore += 1 ;
}
if (questionThreeAnswer3) {
baseScore += 1 ;
}
if (questionThreeAnswer4) {
baseScore += 1 ;
}
if (questionFourAnswer) {
baseScore += 1 ;
}
if (questionSixAnswer) {
baseScore += 1 ;
}
if (questionSevenAnswer) {
baseScore += 1 ;
}
// EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
// String answer = enterAnswerHere.getText().toString();
// {
// if (answer == "Africa") {
// baseScore = baseScore + 1;
// }
// }
return baseScore;
}
}
We can achieve by 2 ways,
You can diable the button by button.setEnable(false); once user click for for time
In your case if you don't have button object so you can use one global boolean and make that as false default and once user click for first time do the process and make that boolean as true, from next time check if the boolean is true just return it.
private boolean mIsSubmited = false;
public void submitResult(View view) {
if(mIsSubmited) {
return;
}
mIsSubmited = true;
RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
// Remaining code..
}
You can disable the submit button once it is clicked. For eg:
onclick() {
submitbutton.setEnabled(false);
}
Prevent user to click on submit again and again.
You can enable it back using:
submitbutton.setEnabled(true);
You can declare a Boolean globally and set it to false at first.
then on button click you can turn its value to true.
on the button click you can check if its false then only to increase the score.
You can use handler for this may be there is better solution then this.
Handler mHandler=new Handler();
//now in your click listener
mHandler.removeCallbacks(mRunnable);//
mHandler.postDelayed(mRunnable, 500);
Runnable mRunnable =new Runnable() {
#Override
public void run() {
//calculate it
}
};
//Wait for half sec before calculating the result if user clicked before then stop runnable from calcualting and again go for calculation.
Hope it helps.

How to set values for Radio button in android?

I am getting the value from DB and setting it to the respective button in the below format. Is there any optimised way to do the same. All these radio buttons are inside a radio group.
if (bundlevalue.get(3).equalsIgnoreCase("Mr.")) {
rg_nametitle.check(R.id.mr);
} else if (bundlevalue.get(3).equalsIgnoreCase("Mrs.")) {
rg_nametitle.check(R.id.mrs);
} else if (bundlevalue.get(3).equalsIgnoreCase("Ms.")) {
rg_nametitle.check(R.id.ms);
} else {
rg_nametitle.check(R.id.messrs);
}
You can try as follows...
String value = bundlevalue.get(3)
Resources res = getResources();
if (value.equalsIgnoreCase("Mr.") || value.equalsIgnoreCase("Mrs.") || value.equalsIgnoreCase("Ms.")) {
String[] splitedValue = value.toLowerCase ().split(".");
int id = res.getIdentifier(splitedValue[0], "id", getContext().getPackageName());
rg_nametitle.check(id);
} else {
rg_nametitle.check(R.id.messrs);
}
In case if you use XML attribute like this :
<RadioGroup
...
...
android:checkedButton="#+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
or you can use switch-case statement like this :
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
Use switch statement. Although, there is nothing big difference in using if-else or switch, you can go ahead with whichever is more readable to you.
public enum Title
{
Mr, Mrs, Ms;
}
String title = bundlevalue.get(3).equalsIgnoreCase("Mr.");
switch(Title.valueOf(title)) {
case Mr:
rg_nametitle.check(R.id.mr);
break;
case Ms:
rg_nametitle.check(R.id.ms);
break;
case Mrs:
rg_nametitle.check(R.id.mrs);
break;
default:
break;
}

How to get RadioButton to play music

So I have a class that will play some songs via the MediaPlayer. I have the following code that When a Radiobutton is selected should play a song, However this does not work, could anyone tell me why?
I do not get any errors, the music just does not play.
Code from the OnCheckedChange Method:
break;
case R.id.rFolk1: //setting up sub radiogroup buttons
if(fsong1.isPlaying() == false)
fsong1.start();
break;
case R.id.rFolk2: //setting up sub radiogroup buttons
if(fsong2.isPlaying() == false)
fsong2.start();
break;
Other code for the songs:
fsong1 = MediaPlayer.create(this, R.raw.folk1);
fsong2 = MediaPlayer.create(this, R.raw.folk2);
Fullcode:
public class Music extends Activity implements OnCheckedChangeListener, OnClickListener{
Button playpause;
RadioGroup selectionList, Folk, Rock, Pop, NewWave, Pipe;//define the radiogroup
RadioButton folk1, folk2, rock1, rock2, pop1, pop2, newwave1, newwave2, pipe1, pipe2; //define radiobuttons
MediaPlayer fsong1, fsong2, rsong1, rsong2, psong1, psong2, nwsong1, nwsong2, pisong1, pisong2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.music); //set layout
initialize(); //call this method
}
public void initialize(){
// set up the radiogroups
selectionList = (RadioGroup) findViewById(R.id.rgMusic);
Folk = (RadioGroup) findViewById(R.id.rgFolk);
Rock = (RadioGroup) findViewById(R.id.rgRock);
Pop = (RadioGroup) findViewById(R.id.rgPop);
Pipe = (RadioGroup) findViewById(R.id.rgPipe);
folk1 = (RadioButton) findViewById(R.id.rFolk1);
folk2 = (RadioButton) findViewById(R.id.rFolk2);
rock1 = (RadioButton) findViewById(R.id.rRock1);
rock2 = (RadioButton) findViewById(R.id.rRock2);
pipe1 = (RadioButton) findViewById(R.id.rPipe1);
pipe2 = (RadioButton) findViewById(R.id.rPipe2);
pop1 = (RadioButton) findViewById(R.id.rPop1);
pop2 = (RadioButton) findViewById(R.id.rPop2);
newwave1 = (RadioButton) findViewById(R.id.rNewWave1);
newwave2 = (RadioButton) findViewById(R.id.rNewWave2);
NewWave = (RadioGroup) findViewById(R.id.rgNewWave);
//settting up on check changed
selectionList.setOnCheckedChangeListener(this);
playpause = (Button) findViewById(R.id.bPlayPause);
playpause.setOnClickListener(this);
fsong1 = MediaPlayer.create(this, R.raw.folk1);
fsong2 = MediaPlayer.create(this, R.raw.folk2);
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
//case statement for onCheckChange to open a new class/layout
//
//This also hides radiogroups and shows others
switch(arg1){
case R.id.rFolk:
Folk.setVisibility(View.VISIBLE); //shows rg for folk
//hides all the rest of the radiogroups if visible
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rPipe:
Pipe.setVisibility(View.VISIBLE);//shows rg for pipe
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rRock:
Rock.setVisibility(View.VISIBLE);//shows rg for rock
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rPop:
Pop.setVisibility(View.VISIBLE);//shows rg for pop
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(NewWave.getVisibility() == View.VISIBLE){
NewWave.setVisibility(View.GONE);
}
break;
case R.id.rNewWave:
NewWave.setVisibility(View.VISIBLE);//shows rg for newwave
//hides all the rest of the radiogroups if visible
if(Folk.getVisibility() == View.VISIBLE){
Folk.setVisibility(View.GONE);
}
if(Pipe.getVisibility() == View.VISIBLE){
Pipe.setVisibility(View.GONE);
}
if(Rock.getVisibility() == View.VISIBLE){
Rock.setVisibility(View.GONE);
}
if(Pop.getVisibility() == View.VISIBLE){
Pop.setVisibility(View.GONE);
}
break;
case R.id.rFolk1: //setting up sub radiogroup buttons
if(fsong1.isPlaying() == false){
fsong1.start();
}
break;
case R.id.rFolk2: //setting up sub radiogroup buttons
if(fsong2.isPlaying() == false){
fsong2.start();
}
}
}
#Override
public void onClick(View view) {
// setting the onclick listener for the buttons for play/pause stop
// check for already playing
if(fsong1.isPlaying()){
fsong1.pause();
// Changing button image to play button
playpause.setBackgroundResource(R.drawable.play_button);
}else{
// Resume song
fsong1.start();
// Changing button image to pause button
playpause.setBackgroundResource(R.drawable.pause_button);
}
}
}
I'd advise against creating so many MediaPlayers . They may very well be the cause of your troubles: When you instance too many of them they don't work well.
And you should take a look at the lifecycle of MediaPlayer as well.
In it you'll find one way to remove your multiple instances of MediaPlayers. You could, for instance change your playing to:
Boolean fsong1, fsong2, rsong1, rsong2, psong1, psong2, nwsong1, nwsong2, pisong1, pisong2;
Boolean isPlaying=false;
//lots of more code
case R.id.rFolk1: //setting up sub radiogroup buttons
if ((isPlaying) && !fsong1){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
if (!fsong1){
//reset all other types of songs!
fsong1=true;
isPlaying = true;
mediaPlayer=MediaPlayer.create(this, R.raw.folk1);
mediaPlayer.start();
}
break;
Warning: I'm not giving you the whole change you'd have to apply!

Categories

Resources