/TextEnlarger/src/com/ideal/textenlarger/ExtendedCheckBoxListView.java

http://eyes-free.googlecode.com/ · Java · 107 lines · 58 code · 18 blank · 31 comment · 0 complexity · 2b3f8bd478646cbbbd8786c07f3d02a3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.textenlarger;
  17. import android.content.Context;
  18. import android.view.View;
  19. import android.widget.CheckBox;
  20. import android.widget.LinearLayout;
  21. import android.widget.TextView;
  22. /**
  23. * Class needed by ApplicationsListActivity.
  24. *
  25. * This was taken from a checkbox list tutorial at anddev.org:
  26. * http://www.anddev.org/extended_checkbox_list__extension_of_checkbox_text_list_tu-t5734.html
  27. */
  28. public class ExtendedCheckBoxListView extends LinearLayout {
  29. private TextView mText;
  30. private CheckBox mCheckBox;
  31. private ExtendedCheckBox mCheckBoxText;
  32. public ExtendedCheckBoxListView(final Context context, ExtendedCheckBox aCheckBoxifiedText) {
  33. super(context);
  34. // Set orientation to be horizontal
  35. this.setOrientation(HORIZONTAL);
  36. mCheckBoxText = aCheckBoxifiedText;
  37. mCheckBox = new CheckBox(context);
  38. mCheckBox.setPadding(0, 0, 20, 0);
  39. // Set the initial state of the checkbox.
  40. mCheckBox.setChecked(aCheckBoxifiedText.getChecked());
  41. // Set the right listener for the checkbox, used to update
  42. // our data holder to change it's state after a click too
  43. mCheckBox.setOnClickListener( new OnClickListener()
  44. {
  45. /**
  46. * When clicked change the state of the 'mCheckBoxText' too!
  47. */
  48. public void onClick(View v) {
  49. mCheckBoxText.setChecked(getCheckBoxState());
  50. ((ApplicationsListActivity) context).setCheckedState(mText.getText().toString(), getCheckBoxState());
  51. }
  52. });
  53. // Add the checkbox
  54. addView(mCheckBox, new LinearLayout.LayoutParams(
  55. android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
  56. mText = new TextView(context);
  57. mText.setText(aCheckBoxifiedText.getText());;
  58. addView(mText, new LinearLayout.LayoutParams(
  59. android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
  60. // Remove some controls in order to prevent a strange flickering when clicking on the TextView!
  61. mText.setClickable(false);
  62. mText.setFocusable(false);
  63. mText.setFocusableInTouchMode(false);
  64. setOnClickListener( new OnClickListener()
  65. {
  66. // Check or unchecked the current checkbox!
  67. public void onClick(View v) {
  68. toggleCheckBoxState();
  69. ((ApplicationsListActivity) context).setCheckedState(mText.getText().toString(), getCheckBoxState());
  70. }
  71. });
  72. }
  73. public void setText(String words) {
  74. mText.setText(words);
  75. }
  76. public void toggleCheckBoxState()
  77. {
  78. setCheckBoxState(!getCheckBoxState());
  79. }
  80. public void setCheckBoxState(boolean bool)
  81. {
  82. mCheckBox.setChecked(bool);
  83. mCheckBoxText.setChecked(bool);
  84. }
  85. public boolean getCheckBoxState()
  86. {
  87. return mCheckBox.isChecked();
  88. }
  89. }