Guest User

Untitled

a guest
Sep 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package com.cyrilmottier.android.androidtips;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.content.res.Configuration;
  8. import android.os.Build;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11.  
  12. /**
  13. * @author Cyril Mottier
  14. */
  15. public class MyView extends View {
  16.  
  17. private static final boolean IS_PRE_FROYO = Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO;
  18.  
  19. public MyView(Context context) {
  20. super(context);
  21. }
  22.  
  23. public MyView(Context context, AttributeSet attrs) {
  24. super(context, attrs);
  25. }
  26.  
  27. public MyView(Context context, AttributeSet attrs, int defStyle) {
  28. super(context, attrs, defStyle);
  29. }
  30.  
  31. @Override
  32. protected void onAttachedToWindow() {
  33. super.onAttachedToWindow();
  34. if (IS_PRE_FROYO) {
  35. getContext().registerReceiver(mConfigurationChangedReceiver, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
  36. }
  37. }
  38.  
  39. @Override
  40. protected void onDetachedFromWindow() {
  41. if (IS_PRE_FROYO) {
  42. getContext().unregisterReceiver(mConfigurationChangedReceiver);
  43. }
  44. super.onDetachedFromWindow();
  45. }
  46.  
  47. private BroadcastReceiver mConfigurationChangedReceiver = new BroadcastReceiver() {
  48. @Override
  49. public void onReceive(Context context, Intent intent) {
  50. onConfigurationChangedSupport(context.getResources().getConfiguration());
  51. }
  52. };
  53.  
  54. @Override
  55. protected void onConfigurationChanged(Configuration newConfig) {
  56. super.onConfigurationChanged(newConfig);
  57. onConfigurationChangedSupport(newConfig);
  58. };
  59.  
  60. private void onConfigurationChangedSupport(Configuration newConfig) {
  61. // This is where you put your code
  62. }
  63.  
  64. }
Add Comment
Please, Sign In to add comment