Skip to content

Instantly share code, notes, and snippets.

@cyrilmottier
Created April 12, 2012 13:49
Show Gist options
  • Save cyrilmottier/2367432 to your computer and use it in GitHub Desktop.
Save cyrilmottier/2367432 to your computer and use it in GitHub Desktop.
View#onConfigurationChanged(Configuration) on API < 8
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment