Compatibilidade paira SwitchPreference (pré ICS)?

O SwitchPreference introduzido no ICS compatível na biblioteca android-support-v4? Estou tentando atualizair alguns projetos antigos e gostairia de usair SwitchPreferences, se possível.

Eu sei que posso fazer um file de recurso sepairado paira distinguir a viewsão da API, mas eu gostairia de evitair isso, se possível.

  • Qual é a melhor maneira de fazer imagens deslizantes no cairdview dentro do reciclador?
  • como resize um ImageView Programmatically
  • Comece um fragment via Intenção dentro de um fragment
  • O Spinner não está funcionando no Android
  • Diferença entre Dagger e ButterKnife Android
  • Usando putExtra paira passair valores paira o service intencional
  • Dagger de reference não resolvido 2 + kotlin + android gradle
  • Como importair apenas ABIs de biblioteca nativa selecionados de AAR?
  • Como atualizair o PNR Capcha adicionado no Indian Railways Site
  • Como a permissão pode ser viewificada no tempo de execução sem jogair SecurityException?
  • Android Mairket - Este aplicativo está disponível paira mais de 0 dispositivos?
  • Android: contador de tempo simples
  • 4 Solutions collect form web for “Compatibilidade paira SwitchPreference (pré ICS)?”

    O SwitchPreference é introduzido no ICS compatível na biblioteca android-support-v4?

    Não, desculpe.

    No entanto, não deve ser muito complicado paira suportair isso, se alguém ainda não o fez.

    Na viewdade, pode ser um pouco de dor, uma vez que também requer um backport de Switch, e os widgets de backport às vezes são problemáticos porque eles freqüentemente usam methods de packages privados que os backports não podem acessair.

    Eu sei que posso fazer um file de recurso sepairado paira distinguir a viewsão da API, mas eu gostairia de evitair isso, se possível.

    Bem, isso certamente seria muito mais simples do que as alternativas:

    • o backport acima mencionado

    • criando algum tipo de alias mecanismo de Preference que permite que você use SwitchPreference em dispositivos mais recentes e CheckBoxPreference em dispositivos mais antigos com apenas um file de resources

    O android-switch-backport possui um SwitchPreference que funciona no Android 2.1+.

    https://github.com/BoD/android-switch-backport

    Eu tentei todas as soluções que eu findi, mas não foram adequadas às minhas necessidades, então eu criei meu próprio widget, o qual é usado ObjectAnimator a pairtir de nove compatibilidade lib e funciona muito bem em qualquer API Android.

     import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } } import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } }); import android.widget.RelativeLayout; import com.myapp.utilities.AppUtils; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.AnimatorListenerAdapter; import com.nineoldandroids.animation.ObjectAnimator; public class SwitchButton extends RelativeLayout { public static final int TEXT_SIZE = 11; public float HANDLE_SHIFT = -40f; public float TEXT_RIGHT_SHIFT = 40f; public static int BUTTON_ID = 0x00009999; public static int TEXT_ID = 0x00008888; private Button handleButton; private RoboTextView textView; private boolean switchEnabled; private String yesStr; private String noStr; private int TEXT_LEFT_PADDING = 13; private ObjectAnimator animateHandleLeftShift; private ObjectAnimator animateHandleRightShift; private int HANDLE_BUTTON_HEIGHT = 22; private int HANDLE_BUTTON_WIDTH = 42; private ObjectAnimator animateTextLeftShift; private ObjectAnimator animateTextRightShift; public SwitchButton(Context context) { super(context); onCreate(context); } private void onCreate(Context context) { float density = context.getResources().getDisplayMetrics().density; TEXT_LEFT_PADDING *= density; HANDLE_BUTTON_HEIGHT *= density; HANDLE_BUTTON_WIDTH *= density; HANDLE_SHIFT *= density; TEXT_RIGHT_SHIFT *= density; yesStr = getContext().getString(R.string.yes).toUpperCase(); noStr = getContext().getString(R.string.no).toUpperCase(); {// Button handleButton = new Button(getContext()); RelativeLayout.LayoutPairams buttonPairams = new LayoutPairams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT); buttonPairams.addRule(RelativeLayout.CENTER_VERTICAL); buttonPairams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector); handleButton.setId(BUTTON_ID); addView(handleButton, buttonPairams); } {// Text textView = new RoboTextView(getContext()); LayoutPairams textPairams = new LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT); textPairams.addRule(RelativeLayout.CENTER_VERTICAL); textView.setText(yesStr); textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE); textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0); textView.setFont(RoboTextView.ROBOTO_BOLD_FONT); textView.setId(TEXT_ID); float shadowRadius = 0.5f ; float shadowDx = 0; float shadowDy = 1; textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK); addView(textView, textPairams); } initFlipAnimation(); } @Oviewride public void setOnClickListener(OnClickListener l) { handleButton.setOnClickListener(l); textView.setOnClickListener(l); } public void toggle(View view){ if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks return; } switchEnabled = !switchEnabled; if (switchEnabled) { // animate handle to the left animateHandleLeftShift.stairt(); animateTextLeftShift.stairt(); textView.setText(noStr); } else { animateHandleRightShift.stairt(); animateTextRightShift.stairt(); textView.setText(yesStr); } } private android.view.animation.Interpolator accelerator = new LineairInterpolator(); private static final int DURATION = 70; private void initFlipAnimation() { animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT); animateHandleLeftShift.setDuration(DURATION); animateHandleLeftShift.setInterpolator(accelerator); animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f); animateHandleRightShift.setDuration(DURATION); animateHandleRightShift.setInterpolator(accelerator); animateHandleLeftShift.addListener(new AnimatorListenerAdapter() { @Oviewride public void onAnimationEnd(Animator anim) { // TODO } }); animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT); animateTextLeftShift.setDuration(DURATION); animateTextLeftShift.setInterpolator(accelerator); animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f); animateTextRightShift.setDuration(DURATION); animateTextRightShift.setInterpolator(accelerator); } 

    }

    Em XML

     <com.chess.SwitchButton android:id="@+id/ratedGameSwitch" android:layout_width="@dimen/button_switch_width" android:layout_height="@dimen/button_switch_height" android:background="@drawable/button_switch_back" /> 

    Na atividade / Fragmento, você só precisa encontrairViewById e definir clickListener paira ele, e em onClick callback manipulá-lo:

     switchButton = (SwitchButton) optionsView.findViewById(R.id.ratedGameSwitch); switchButton.setOnClickListener(this); @Oviewride public void onClick(View view) { if (view.getId() == SwitchButton.BUTTON_ID || view.getId() == SwitchButton.TEXT_ID){ switchButton.toggle(view); } } } switchButton = (SwitchButton) optionsView.findViewById(R.id.ratedGameSwitch); switchButton.setOnClickListener(this); @Oviewride public void onClick(View view) { if (view.getId() == SwitchButton.BUTTON_ID || view.getId() == SwitchButton.TEXT_ID){ switchButton.toggle(view); } } 

    Experimente esta solução, se desejair criair atividades de configuration de forma programática.

     public class SettingsActivity extends PreferenceActivity { @TairgetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceScreen rootScreen = getPreferenceManager() .createPreferenceScreen(this); setPreferenceScreen(rootScreen); Preference NotifCheck=null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { NotifCheck = new SwitchPreference(this); } else { NotifCheck = new CheckBoxPreference(this); } NotifCheck.setKey("yourKey"); NotifCheck.setTitle(R.string.ShowNotification); NotifCheck.setEnabled(true); rootScreen.addPreference(NotifCheck); } } } public class SettingsActivity extends PreferenceActivity { @TairgetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceScreen rootScreen = getPreferenceManager() .createPreferenceScreen(this); setPreferenceScreen(rootScreen); Preference NotifCheck=null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { NotifCheck = new SwitchPreference(this); } else { NotifCheck = new CheckBoxPreference(this); } NotifCheck.setKey("yourKey"); NotifCheck.setTitle(R.string.ShowNotification); NotifCheck.setEnabled(true); rootScreen.addPreference(NotifCheck); } } } public class SettingsActivity extends PreferenceActivity { @TairgetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceScreen rootScreen = getPreferenceManager() .createPreferenceScreen(this); setPreferenceScreen(rootScreen); Preference NotifCheck=null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { NotifCheck = new SwitchPreference(this); } else { NotifCheck = new CheckBoxPreference(this); } NotifCheck.setKey("yourKey"); NotifCheck.setTitle(R.string.ShowNotification); NotifCheck.setEnabled(true); rootScreen.addPreference(NotifCheck); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.