Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to attach OnClickListener to a button in a custom preference layout?
    primarykey
    data
    text
    <p>I have a preference screen that is populated with items from a database. I have this working by creating my own <code>PreferenceActivity</code>. In the activity I create <code>DialogPreference</code> items and add them to my <code>PreferenceCategory</code> To style to preference item on the screen I use a custom layout and apply it using <code>setLayoutResource(R.layout.custom_pref_row)</code></p> <p>This basically adds an <code>ImageButton</code> to the view aligned to the right of the layout. This all works fine and my preference screen shows the custom view with the button. My question is how do I attach a click listener to the button in the custom view? I was not able to find a way to get at <code>View</code> for the row from the <code>PreferenceActivity</code>. If my items were not created dynamically I might be able to do this all from XML and then reference the id or the button, but I can do that because I am creating the list dynamically.</p> <p>Any suggestions on how to get a handle on the <code>ImageButton</code> for each item? In the end I want to configure the button to launch a delete confirmation dialog.</p> <p>R.layout.custom_pref_row:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize"&gt; &lt;RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_marginBottom="6dip" android:layout_weight="1"&gt; &lt;TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" android:ellipsize="marquee" android:fadingEdge="horizontal" /&gt; &lt;TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title" android:textAppearance="?android:attr/textAppearanceSmall" android:maxLines="2" /&gt; &lt;ImageButton android:id="@+id/pref_delete_station" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_trash_can" android:layout_alignParentRight="true" android:background="@null"&gt;&lt;/ImageButton&gt; &lt;/RelativeLayout&gt; &lt;!-- Preference should place its actual preference widget here. --&gt; &lt;LinearLayout android:id="@+android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" android:orientation="vertical" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Related part of my PreferenceActivity:</p> <pre><code>DialogPreference diaPref; for (Station mStation : sList) { diaPref = new StationEditor(this.getPreferenceScreen().getContext(), null, this, mStation); diaPref.setLayoutResource(R.layout.custom_pref_row); diaPref.setTitle(mStation.getName()); diaPref.setKey(STATION_PREFIX + mStation.getId()); // add new preference stationTypesCategory.addPreference(diaPref); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload