1. Check out our companion app, Forums for Android! Download from Google Play

Toggle Button help

Discussion in 'Application Development' started by cr5315, Mar 13, 2011.

  1. cr5315

    cr5315 Well-Known Member
    Thread Starter

    Jul 23, 2010
    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):
    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.               }
    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.               }
    31.       }});
    Code (Text):
    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?


  2. cr5315

    cr5315 Well-Known Member
    Thread Starter

    Jul 23, 2010
  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
    Thread Starter

    Jul 23, 2010
    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):
    2. private RSSFeed myRssFeed = null;
    I made the private public and the same thing happened as before
  5. alostpacket

    alostpacket Over Macho Grande?

    Nov 29, 2009
    Android App Developer
    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