Created
August 3, 2017 03:21
-
-
Save Kahtaf/157b395988a652ac56e47c08f582492b to your computer and use it in GitHub Desktop.
Android Preferences - Reusable widget layout for a preference and it's onClick listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | |
<Preference | |
android:key="preference_key" | |
android:title="@string/pref_title" | |
android:widgetLayout="@layout/preference_secondary_button" /> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parent activity of the preference screen | |
// onClick listener for reusable preference widget. | |
public void onPreferenceSecondaryButtonClicked(View view){ | |
// Gets a reference to the preference that contains this widget | |
ListView preferenceList = (ListView) view.getParent().getParent().getParent(); | |
int preferencePosition = preferenceList.getPositionForView(view); | |
Preference preference = (Preference) preferenceList.getAdapter().getItem(preferencePosition); | |
// Add onclick logic here, using reference to the preference object that contains this widget | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment