Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. <android.preference.ListPreference
  2. android:layout="@layout/your_custom_layout"
  3. ....
  4. />
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:minHeight="?android:attr/listPreferredItemHeight"
  10. android:gravity="center_vertical"
  11. android:paddingRight="?android:attr/scrollbarSize">
  12.  
  13. <RelativeLayout
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:layout_margin="@dimen/preferences_layout_margin">
  18.  
  19. <ImageView android:id="@+id/yourIconId"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_centerVertical="true" /> <!-- here i've added an ImageView for icon -->
  23.  
  24. <TextView android:id="@android:id/title"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:singleLine="true"
  28. android:textAppearance="?android:attr/textAppearanceMedium"
  29. android:ellipsize="marquee"
  30. android:fadingEdge="horizontal"
  31. android:layout_toRightOf="@+id/iconImageView"
  32. android:layout_marginLeft="@dimen/preferences_icon_margin" />
  33.  
  34. <TextView android:id="@android:id/summary"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@android:id/title"
  38. android:layout_alignLeft="@android:id/title"
  39. android:textAppearance="?android:attr/textAppearanceSmall"
  40. android:maxLines="2" />
  41.  
  42. </RelativeLayout>
  43. <!-- Preference should place its actual preference widget here. -->
  44. <LinearLayout android:id="@android:id/widget_frame"
  45. android:layout_width="wrap_content"
  46. android:layout_height="fill_parent"
  47. android:gravity="center_vertical"
  48. android:orientation="vertical" />
  49.  
  50. </LinearLayout>
  51.  
  52. // @Based on the nice version of LucaZanini. Thank you!
  53. public class IconPickerPreference extends ListPreference {
  54. private int currentIndex = 0;
  55.  
  56. private class CustomListPreferenceAdapter extends ArrayAdapter<IconItem> {
  57. private Context context;
  58. private List<IconItem> icons;
  59. private int resource;
  60.  
  61. public CustomListPreferenceAdapter(Context context, int resource, List<IconItem> objects) {
  62. super(context, resource, objects);
  63. this.context = context;
  64. this.resource = resource;
  65. this.icons = objects;
  66. }
  67.  
  68. @Override
  69. public View getView(final int position, View convertView, ViewGroup parent) {
  70.  
  71. ViewHolder holder;
  72. if (convertView == null) {
  73. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  74. convertView = inflater.inflate(resource, parent, false);
  75.  
  76. holder = new ViewHolder();
  77. holder.iconName = (TextView) convertView.findViewById(R.id.iconName);
  78. holder.iconImage = (ImageView) convertView.findViewById(R.id.iconImage);
  79. holder.radioButton = (RadioButton) convertView.findViewById(R.id.iconRadio);
  80. convertView.setTag(holder);
  81. } else {
  82. holder = (ViewHolder) convertView.getTag();
  83. }
  84.  
  85. holder.iconName.setText(icons.get(position).name);
  86.  
  87. int identifier = context.getResources().getIdentifier( icons.get(position).file, "drawable", context.getPackageName());
  88. holder.iconImage.setImageResource(identifier);
  89. holder.radioButton.setChecked(icons.get(position).isChecked);
  90. convertView.setOnClickListener(new OnClickListener() {
  91. @Override
  92. public void onClick(View v) {
  93. ViewHolder holder = (ViewHolder) v.getTag();
  94. for (int i = 0; i < icons.size(); i++) {
  95. if (i == position) {
  96. icons.get(i).isChecked = true;
  97. } else {
  98. icons.get(i).isChecked = false;
  99. }
  100. }
  101. getDialog().dismiss();
  102. }
  103. });
  104. return convertView;
  105. }
  106. }
  107.  
  108. private class IconItem {
  109. private String file;
  110. private boolean isChecked;
  111. private String name;
  112.  
  113. public IconItem(CharSequence name, CharSequence file, boolean isChecked) {
  114. this(name.toString(), file.toString(), isChecked);
  115. }
  116.  
  117. public IconItem(String name, String file, boolean isChecked) {
  118. this.name = name;
  119. this.file = file;
  120. this.isChecked = isChecked;
  121. }
  122.  
  123. }
  124.  
  125. private class ViewHolder {
  126. protected ImageView iconImage;
  127. protected TextView iconName;
  128. protected RadioButton radioButton;
  129. }
  130.  
  131. private Context context;
  132. private ImageView icon;
  133.  
  134. private CharSequence[] iconFile;
  135. private CharSequence[] iconName;
  136. private List<IconItem> icons;
  137. private SharedPreferences preferences;
  138. private Resources resources;
  139. private String selectedIconFile, defaultIconFile;
  140. private TextView summary;
  141.  
  142. public IconPickerPreference(Context context, AttributeSet attrs) {
  143. super(context, attrs);
  144. this.context = context;
  145. resources = context.getResources();
  146. preferences = PreferenceManager.getDefaultSharedPreferences(context);
  147.  
  148. TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.attrs_icon, 0, 0);
  149. try {
  150. defaultIconFile = a.getString(R.styleable.attrs_icon_iconFile);
  151. } finally {
  152. a.recycle();
  153. }
  154. }
  155.  
  156. @Override
  157. protected void onBindView(View view) {
  158. super.onBindView(view);
  159.  
  160. CharSequence[] entries = getEntries();
  161. CharSequence[] values = getEntryValues();
  162. selectedIconFile = values[ currentIndex].toString();
  163. icon = (ImageView) view.findViewById(R.id.iconSelected);
  164. updateIcon();
  165. summary = (TextView) view.findViewById( R.id.preference_summary);
  166. summary.setText( entries[ currentIndex]);
  167. }
  168.  
  169. @Override
  170. protected void onDialogClosed(boolean positiveResult) {
  171. super.onDialogClosed(positiveResult);
  172. if (icons != null) {
  173. for (int i = 0; i < iconName.length; i++) {
  174. IconItem item = icons.get(i);
  175. if (item.isChecked) {
  176. persistString( "" + i);
  177. currentIndex = i;
  178. selectedIconFile = item.file;
  179. updateIcon();
  180. summary.setText(item.name);
  181. break;
  182. }
  183. }
  184. }
  185. }
  186.  
  187. @Override
  188. protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
  189. String number = "0";
  190. if (restorePersistedValue) {
  191. // Restore existing state
  192. number = this.getPersistedString( "0");
  193. } else {
  194. persistString( number);
  195. }
  196. try {
  197. currentIndex = Integer.parseInt(number);
  198. } catch( Exception e) {
  199. ; // skip any error, it will be corrected to 0
  200. }
  201. }
  202.  
  203. @Override
  204. protected void onPrepareDialogBuilder(Builder builder) {
  205. builder.setNegativeButton("Cancel", null);
  206. builder.setPositiveButton(null, null);
  207. iconName = getEntries();
  208. iconFile = getEntryValues();
  209.  
  210. if (iconName == null || iconFile == null || iconName.length != iconFile.length) {
  211. throw new IllegalStateException(
  212. "IconPickerPreference requires an entries array and an entryValues array which are both the same length");
  213. }
  214.  
  215. icons = new ArrayList<IconItem>();
  216. for (int i = 0; i < iconName.length; i++) {
  217. IconItem item = new IconItem(iconName[i], iconFile[i], ( i == currentIndex));
  218. icons.add(item);
  219. }
  220. CustomListPreferenceAdapter customListPreferenceAdapter = new CustomListPreferenceAdapter(
  221. context, R.layout.preference_list_icon_picker, icons);
  222. builder.setAdapter(customListPreferenceAdapter, null);
  223. }
  224. private void updateIcon() {
  225. int identifier = resources.getIdentifier( selectedIconFile, "drawable", context.getPackageName());
  226. icon.setImageResource(identifier);
  227. icon.setTag(selectedIconFile);
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement