Toggle Button help


Last Updated: 2011-03-21 05:49:10
  1. cr5315

    cr5315 Well-Known Member Developer

    I'm making an app and I'm trying to figure out how to get a toggle button to work. The button is supposed to change the way that an activity acts when an item in a listview is clicked on. If the button is set to on, it will launch a certain activity and if the button is off, a different activity will be launched. I've tried to figure out how to get it myself, but it always crashes when I click on one of the items. I was wondering if the toggle controls had to be in the activity that has the button in its layout xml, as that is not the case in my app.

    Anywho, enough talking, time for some code

    AndroidRssReader.java concerning the listview
    Code (Text):
    1.  
    2. @Override
    3.  protected void onListItemClick(ListView l, View v, final int position, long id) {
    4.   // TODO Auto-generated method stub
    5.   final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.toggleButton1);
    6.   togglebutton.setOnClickListener(new OnClickListener() {
    7.       public void onClick(View v) {
    8.           // Perform action on clicks
    9.           if (togglebutton.isChecked()) {
    10.                Intent intent = new Intent(AndroidRssReader.this, ShowDetails.class);
    11.                Bundle bundle = new Bundle();
    12.                bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
    13.                bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
    14.                bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
    15.                bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
    16.                intent.putExtras(bundle);
    17.                     startActivity(intent);
    18.               }
    19.          
    20.       else {
    21.                Intent intent = new Intent(AndroidRssReader.this,ShowNoDetails.class);
    22.                Bundle bundle = new Bundle();
    23.                bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
    24.                bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
    25.                bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
    26.                bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
    27.                intent.putExtras(bundle);
    28.                     startActivity(intent);
    29.               }
    30.          
    31.       }});
    settings.xml
    Code (Text):
    1.  
    2.  
    3.     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
    4.         <Button android:id="@+id/button1" android:layout_height="wrap_content" android:background="@drawable/logo" android:layout_width="fill_parent"></Button>
    5.         <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Settings" android:layout_gravity="center_horizontal" android:textSize="25sp"></TextView>
    6.         <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:layout_marginTop="25px" android:text="Show News Story Description" android:textSize="23sp"></TextView>
    7.         <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Note: Will contain HTML tags such as &lt;p&gt; and &lt;/p&gt;" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:textSize="19sp"></TextView>
    8.         <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10px" android:text="descriptionToggle" android:textOn="Details On" android:textOff="Details Off"></ToggleButton>
    9.     </LinearLayout>
    The ShowDetails and ShowNoDetails activities are exactly the same except for the fact that ShowNoDetails, obviously, doesn't show the details of the listview item.

    When I click on an item, Eclipse highlights this part of AndroidRssReader
    Code (Text):
    1. togglebutton.setOnClickListener(new OnClickListener() {
    Any ideas on how to make the togglebutton work?

    Advertisement
  2. cr5315

    cr5315 Well-Known Member Developer

    Anyone?
  3. JamTheMan

    JamTheMan Well-Known Member

    Hmm... Need some more info on that error - What exactly is Eclipse saying? LogCat?

    You might just need to import andorid.View.* to be able to do it?

    Adding "View." in front of OnClickListener() might also do some magic for you (also resulting in the afore mentioned import being forced).

    Or maybe it is you reference to the "myRssFeed" that is not available at that point during runtime. Is it static? Or in any other way reachable?
  4. cr5315

    cr5315 Well-Known Member Developer

    I added View to the OnClickListener and tired again. The LogCat also didn't show any errors when the button was pressed.

    The "myRssFeed" has this for it's code
    Code (Text):
    1.  
    2. private RSSFeed myRssFeed = null;
    3.  
    I made the private public and the same thing happened as before
  5. alostpacket

    alostpacket Over Macho Grande? VIP Member

    I'm confused, your setting a listener for the toggle button inside the listener for the list item?

    ListView items are weird in the way they handle events, if your OnListItemClickLister gets triggered, then theoretically the toggle wont get triggered.


    If you want to make it so the listview item controls the toggle button, you need to set the toggle as not enabled and not focusable and no need for a listener on it.

    Then handle the clicks and setChecked yourself in the list item listener.

    Otherwise you might want to look into using a ListAdapter for your listview and setting the onClick in the getView method.

Share This Page