Reputation: 39
I have 3 toggle buttons. Only one is allowed to be on at a time. Can anyone tell me why the other two toggle buttons don't turn off if the third button is pressed.
MusicPlayerActivity.java
package com.example.musicplayer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
public class MusicPlayerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player);
addListenerOnButton(null);
}
public void addListenerOnButton(View view) {
ToggleButton artist = (ToggleButton) findViewById(R.id.artistID);
ToggleButton album = (ToggleButton) findViewById(R.id.albumID);
ToggleButton song = (ToggleButton) findViewById(R.id.songID);
}
}
music_player.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:background="#009ACD"
android:text="Play All Music"
android:textColor="#FFFFFF" />
<RadioGroup
android:id="@+id/toggleGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ToggleButton
android:id="@+id/albumID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addListenerOnButton"
android:layout_alignBottom="@+id/artistID"
android:layout_toRightOf="@+id/artistID"
android:text="Album"
android:textOff="Album"
android:textOn="Album" />
<ToggleButton
android:id="@+id/songID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/albumID"
android:onClick="addListenerOnButton"
android:layout_alignBottom="@+id/albumID"
android:layout_toRightOf="@+id/albumID"
android:text="Song"
android:textOff="Song"
android:textOn="Song" />
<ToggleButton
android:id="@+id/artistID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addListenerOnButton"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="63dp"
android:layout_marginTop="16dp"
android:text="Artist"
android:textOff="Artist"
android:textOn="Artist" />
</RadioGroup>
</RelativeLayout>
Upvotes: 0
Views: 1373
Reputation: 15146
You should instead be using a RadioGroup
to manage de-selecting any other selected item after an item has been selected. It's called "multiple-exclusion", which ensures only 1 item is selected at a time.
<RadioGroup
android:id="@+id/toggleGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ToggleButton
android:id="@+id/albumID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addListenerOnButton"
android:layout_alignBottom="@+id/artistID"
android:layout_toRightOf="@+id/artistID"
android:text="Album"
android:textOff="Album"
android:textOn="Album" />
<ToggleButton
android:id="@+id/songID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/albumID"
android:onClick="addListenerOnButton"
android:layout_alignBottom="@+id/albumID"
android:layout_toRightOf="@+id/albumID"
android:text="Song"
android:textOff="Song"
android:textOn="Song" />
<ToggleButton
android:id="@+id/artistID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addListenerOnButton"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="63dp"
android:layout_marginTop="16dp"
android:text="Artist"
android:textOff="Artist"
android:textOn="Artist" />
</RadioGroup>
This group will ensure only 1 ToggleButton
is selected at a time.
Now, if you wanted to add listeners to this, you must specify the onClick
attribute. For the value, you specify which method you want to execute.
The method must
The problem with your addListenerOnButton
method is that it does not declare a View
parameter. Your method should look like
public void addListenerOnButton(View view) {
}
Also, since you have the RadioGroup
, you no longer need to manually deselect the other buttons.
Upvotes: 0
Reputation: 4821
Issue is Toggle Button isChecked Method is not getting fired.
OPTION 1
In your xml file, add this line for each of the toggle button
android:onClick="MethodName"
Example
<ToggleButton
android:id="@+id/albumID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/artistID"
android:layout_toRightOf="@+id/artistID"
android:text="Album"
android:textOff="Album"
android:textOn="Album"
android:onClick="onAlbumIDClicked" />
In your oncreate method, create respective methods and then check if they are checked or not. That would work.
OPTION 2
Remove all the If statements and Add this code below
artist.setOnCheckedChangeListener(changeChecker);
album.setOnCheckedChangeListener(changeChecker);
song.setOnCheckedChangeListener(changeChecker);
OnCheckedChangeListener changeChecker = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
if (buttonView == artist) {
artist.setChecked(true);
album.setChecked(false);
song.setChecked(false);
}
if (buttonView == album) {
artist.setChecked(false);
album.setChecked(true);
song.setChecked(false);
}
if (buttonView == song) {
artist.setChecked(false);
album.setChecked(false);
song.setChecked(true);
}
}
}
};
OPTION 3 - Using Radio Group
in your xml file, add the below lines of code wrapping the 3 toggle buttons.
<RadioGroup android:id="@+id/toggleGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:orientation="horizontal"
>
In your activity class, create a listener
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};
Now, register the listener in OnCreate Method.
((RadioGroup) findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);
Now in the onToggle Method, based on the id, set accordingly.
public void onToggle(View view) {
((RadioGroup)view.getParent()).check(view.getId());
// app specific stuff ..
}
Upvotes: 1