Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cyrilmottier.android.androidtips;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.res.Configuration;
- import android.os.Build;
- import android.util.AttributeSet;
- import android.view.View;
- /**
- * @author Cyril Mottier
- */
- public class MyView extends View {
- private static final boolean IS_PRE_FROYO = Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO;
- public MyView(Context context) {
- super(context);
- }
- public MyView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- protected void onAttachedToWindow() {
- super.onAttachedToWindow();
- if (IS_PRE_FROYO) {
- getContext().registerReceiver(mConfigurationChangedReceiver, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
- }
- }
- @Override
- protected void onDetachedFromWindow() {
- if (IS_PRE_FROYO) {
- getContext().unregisterReceiver(mConfigurationChangedReceiver);
- }
- super.onDetachedFromWindow();
- }
- private BroadcastReceiver mConfigurationChangedReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- onConfigurationChangedSupport(context.getResources().getConfiguration());
- }
- };
- @Override
- protected void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- onConfigurationChangedSupport(newConfig);
- };
- private void onConfigurationChangedSupport(Configuration newConfig) {
- // This is where you put your code
- }
- }
Add Comment
Please, Sign In to add comment