How to connect OnClickListener to a button in user preferences?

I have a preferences screen that is populated with items from the database. It works for me, creating my own PreferenceActivity. In my work, I create elements DialogPreferenceand add them to my PreferenceCategoryTo style on the preference item on the screen. I use a custom layout and apply it withsetLayoutResource(R.layout.custom_pref_row)

This basically adds ImageButtonto the view aligned to the right of the layout. All this works great, and the settings screen shows a custom view with a button. My question is how to attach a click button to a button in a user view? I could not find a way to get Viewfor the string from PreferenceActivity. If my objects were not created dynamically, I could do all of this from XML, and then reference the identifier or button, but I can do this because I am dynamically creating the list.

Any suggestions on how to get a handle ImageButtonfor each item? In the end, I want to configure a button to launch the delete confirmation dialog box.

R.layout.custom_pref_row:

<?xml version="1.0" encoding="utf-8"?>
<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">

<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">

    <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" />

    <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" />

    <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"></ImageButton>

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" />


</LinearLayout>

Related part of my PreferenceActivity:

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);
}
+3
source
2

, . , Preference , .

setLayoutResouce() , , . , , .

, onCreateView . , .

alraedy Preference (DialogPreference), , , ...

@Override
protected View onCreateView (ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View customRow = inflater.inflate(R.layout.preferences_station_list_row, null);
    ((ImageButton) customRow.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {      
        @Override
        public void onClick(View v) {
           Log.i("c","clicked");
        }
    });

    customRow.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(null);
        }
    });
    customRow.setClickable(true);
    return customRow;
}

, , , , . ShowDialog(). , , , "" . , , , ?

0

DialogPreference onBindDialogView(View view). :

@Override
protected void onBindDialogView(View view) {

    ((ImageButton) view.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {      
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    super.onBindDialogView(view);
}

DialogPreference /, , .

DialogPreference.

, !

+1

All Articles