• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Toggle Button help

cr5315

Android Enthusiast
Jul 23, 2010
434
158
Narnia
cr5315.com
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:
@Override
 protected void onListItemClick(ListView l, View v, final int position, long id) {
  // TODO Auto-generated method stub
  final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.toggleButton1);
  togglebutton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
          // Perform action on clicks
          if (togglebutton.isChecked()) {
        	   Intent intent = new Intent(AndroidRssReader.this, ShowDetails.class);
        	   Bundle bundle = new Bundle();
        	   bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
        	   bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
        	   bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
        	   bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
        	   intent.putExtras(bundle);
        	        startActivity(intent);
        	  }
          
      else {
        	   Intent intent = new Intent(AndroidRssReader.this,ShowNoDetails.class);
        	   Bundle bundle = new Bundle();
        	   bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
        	   bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
        	   bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
        	   bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
        	   intent.putExtras(bundle);
        	        startActivity(intent);
        	  }
          
      }});

settings.xml
Code:
    <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">
        <Button android:id="@+id/button1" android:layout_height="wrap_content" android:background="@drawable/logo" android:layout_width="fill_parent"></Button>
        <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>
        <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>
        <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>
        <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>
    </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:
togglebutton.setOnClickListener(new OnClickListener() {

Any ideas on how to make the togglebutton work?
 
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?
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones