Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public class SettingWithSwitch extends RelativeLayout {
  2. TextView settingTitle;
  3. TextView settingState;
  4. SwitchCompat toggleSwitch;
  5.  
  6. public SettingWithSwitch(Context context) {
  7. super(context);
  8. init(context, null, 0, 0);
  9. }
  10.  
  11. public SettingWithSwitch(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. init(context, attrs, 0, 0);
  14. }
  15.  
  16. public SettingWithSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
  17. super(context, attrs, defStyleAttr);
  18. init(context, attrs, defStyleAttr, 0);
  19. }
  20.  
  21. @TargetApi(21)
  22. public SettingWithSwitch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  23. super(context, attrs, defStyleAttr, defStyleRes);
  24. init(context, attrs, defStyleAttr, defStyleRes);
  25. }
  26.  
  27. private void init(Context context, AttributeSet attributeSet, int defStyleAttr, int defStyleRes){
  28. LayoutInflater.from(context).inflate(R.layout.settings_item_with_switch, this, true);
  29. TypedArray values = context.getTheme().obtainStyledAttributes(attributeSet
  30. , R.styleable.SettingWithSwitch
  31. , defStyleAttr
  32. , defStyleRes);
  33. settingTitle = (TextView) findViewById(R.id.setting_item_title);
  34. settingState = (TextView) findViewById(R.id.setting_item_detail);
  35. toggleSwitch = (SwitchCompat) findViewById(R.id.setting_item_switch);
  36. String title;
  37. String detail;
  38. boolean value;
  39. boolean switchVisible;
  40. Integer background;
  41. try {
  42. title = values.getString(R.styleable.SettingWithSwitch_settingTitle);
  43. detail = values.getString(R.styleable.SettingWithSwitch_settingDetail);
  44. value = values.getBoolean(R.styleable.SettingWithSwitch_value, true);
  45. switchVisible = values.getBoolean(R.styleable.SettingWithSwitch_switchVisible, true);
  46. background = values.getResourceId(R.styleable.SettingWithSwitch_bg, -1);
  47. } finally {
  48. values.recycle();
  49. }
  50. settingTitle.setText(title);
  51. toggleSwitch.setChecked(value);
  52. if(switchVisible){
  53. toggleSwitch.setVisibility(VISIBLE);
  54. }
  55. if(detail == null){
  56. settingState.setVisibility(GONE);
  57. } else {
  58. settingState.setText(detail);
  59. }
  60. setBackgroundResource(background);
  61. }
  62.  
  63. public boolean isChecked(){
  64. return this.toggleSwitch.isChecked();
  65. }
  66.  
  67. public void setChecked(boolean value){
  68. this.toggleSwitch.setChecked(value);
  69. }
  70.  
  71. public void setTitle(String title){
  72. this.settingTitle.setText(title);
  73. }
  74.  
  75. public void setTitle(int resId){
  76. this.settingTitle.setText(resId);
  77. }
  78.  
  79. public void setDetail(String detail){
  80. this.settingState.setText(detail);
  81. }
  82.  
  83. public void setDetail(int resId){
  84. this.settingState.setText(resId);
  85. }
  86.  
  87. public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener){
  88. this.toggleSwitch.setOnCheckedChangeListener(onCheckedChangeListener);
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement