Monday 15 August 2011

android - Togglebutton not turning off when other Togglebutton is clicked -



android - Togglebutton not turning off when other Togglebutton is clicked -

i have 3 toggle buttons. 1 allowed on @ time. can tell me why other 2 toggle buttons don't turn off if 3rd button pressed.

musicplayeractivity.java

bundle 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 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>

issue toggle button ischecked method not getting fired.

option 1

in xml file, add together line each of 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 oncreate method, create respective methods , check if checked or not. work.

option 2

remove if statements , add together 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 xml file, add together below lines of code wrapping 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 activity class, create listener

static final radiogroup.oncheckedchangelistener togglelistener = new radiogroup.oncheckedchangelistener() { @override public void oncheckedchanged(final radiogroup radiogroup, final int i) { (int j = 0; j < radiogroup.getchildcount(); j++) { final togglebutton view = (togglebutton) radiogroup.getchildat(j); view.setchecked(view.getid() == i); } } };

now, register listener in oncreate method.

((radiogroup) findviewbyid(r.id.togglegroup)).setoncheckedchangelistener(togglelistener);

now in ontoggle method, based on id, set accordingly.

public void ontoggle(view view) { ((radiogroup)view.getparent()).check(view.getid()); // app specific stuff .. }

android xml

No comments:

Post a Comment