Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old March 13th, 2011, 03:30 PM   #1 (permalink)
ROM Developer
Thread Author (OP)
 
cr5315's Avatar
 
Join Date: Jul 2010
Location: Narnia
Posts: 435
 
Device(s): Current: HTC Evo 4G, Asus Nexus 7 Retired: LG Ally
Carrier: Not Provided

Thanks: 103
Thanked 161 Times in 105 Posts
Question Toggle Button help

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?

Advertisements
__________________

(That's a link. Just saying.)
cr5315 is offline  
Reply With Quote
sponsored links
Old March 18th, 2011, 07:19 PM   #2 (permalink)
ROM Developer
Thread Author (OP)
 
cr5315's Avatar
 
Join Date: Jul 2010
Location: Narnia
Posts: 435
 
Device(s): Current: HTC Evo 4G, Asus Nexus 7 Retired: LG Ally
Carrier: Not Provided

Thanks: 103
Thanked 161 Times in 105 Posts
Default

Anyone?
cr5315 is offline  
Reply With Quote
Old March 20th, 2011, 12:06 PM   #3 (permalink)
Member
 
Join Date: Dec 2010
Location: Aarhus, Denmark
Posts: 136
 
Device(s): SE Xperia X10
Carrier: Not Provided

Thanks: 2
Thanked 22 Times in 22 Posts
Default

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?
JamTheMan is offline  
Reply With Quote
Old March 20th, 2011, 03:25 PM   #4 (permalink)
ROM Developer
Thread Author (OP)
 
cr5315's Avatar
 
Join Date: Jul 2010
Location: Narnia
Posts: 435
 
Device(s): Current: HTC Evo 4G, Asus Nexus 7 Retired: LG Ally
Carrier: Not Provided

Thanks: 103
Thanked 161 Times in 105 Posts
Default

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:
private RSSFeed myRssFeed = null;
I made the private public and the same thing happened as before
cr5315 is offline  
Reply With Quote
Old March 21st, 2011, 12:49 AM   #5 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,920
 
Device(s): GlassXE, MotoX, N5, N4, N7'12, GNex, N1, SGT10.1, Revue, Xoom, Eris, OG Droid
Carrier: TMO

Thanks: 4,603
Thanked 3,578 Times in 1,530 Posts
Default

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.
alostpacket is offline  
Reply With Quote
Reply
Tags
activity, listview, toggle button, togglebutton, xml


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:01 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.