Question

I have a preference screen with a custom layout, in which I have included a button with an onClickListener. The idea being that if I press the button, the preference item to which it is attached is to be deleted. How can I determine which preference item the clicked button is attached to?

Custom Layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingEnd="?android:attr/scrollbarSize" >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginEnd="6dip"
        android:layout_marginStart="15dip"
        android:layout_marginTop="6dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:text="Preference"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:text="Summary"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />

        <Button
            android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/delete_item"
            android:focusable="false"
            android:onClick="onRemoveButtonClick" />
    </RelativeLayout>

</LinearLayout>

Code

public class MainActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferencescreen);
        addPreferencesFromResource(R.xml.prefs);
    }

    public void addPref() {
        PreferenceManager preferenceManager = getPreferenceManager();
        PreferenceScreen preferenceScreen = (PreferenceScreen) preferenceManager
                .findPreference("mainPrefScreen");

        ListPreference prefList = new ListPreference(this);
        prefList.setTitle("New Item");
        prefList.setEntries(R.array.Names);
        prefList.setEntryValues(R.array.Values);
        prefList.setLayoutResource(R.layout.preference);
        prefList.setKey("New Item Key");


        preferenceScreen.addPreference(prefList);
    }

    public void onRemoveButtonClick(View v) {
        // ON Delete button Click here
        // What TO Do
    }

    public void addApp(View v) {
        addPref();
    }
}
Was it helpful?

Solution

Ended up with the following which seems to do the trick

public void onRemoveButtonClick(View v) {
        ListView listview = getListView();
        ListAdapter listAdapter = listview.getAdapter();
        int position = listview.getPositionForView(v);

        MultiSelectListPreference mslp = (MultiSelectListPreference) listAdapter.getItem(position);

        if (mslp != null) {
            preferenceScreen.removePreference(mslp);
        }       
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top