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
settings.xml
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
Any ideas on how to make the togglebutton work?
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 <p> and </p>" 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?