android lock SYSTEM orientation to landscape (not just my application) - android

i want to give the user the option to set the phone's orientation to auto, portrait or landscape.which means for the ENTIRE PHONE's orientation not just my application. every other app must have the landscape orientation too. it was pretty simple to do the first two like this
portrait:
Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
auto:
Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
but i can't find a way to set it to landscape. i want to set all apps to landscape not just mine. i see here that it can be done but it doesn't give much information. How to make Android system force LANDSCAPE for all apk?. anyone willing to explain or provide code snippet?

Code below can force your screen to landscape mode. It is similar to set it to other mode.
public class MyService extends Service {
private boolean mViewAdded = false;
private WindowManager.LayoutParams mLayoutParams;
private View mOverlayView;
private WindowManager mWindowManager;
// Call this some where in your code
private void setLandscapeOrientation() {
mWindowManager = ((WindowManager)getSystemService("window"));
mOverlayView = new View(this);
mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
mLayoutParams.width = 0;
mLayoutParams.height = 0;
mLayoutParams.flags = mLayoutParams.flags |
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
mLayoutParams.flags = mLayoutParams.flags &
~WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON &
~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
mLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
if (mViewAdded) {
mWindowManager.updateViewLayout(mOverlayView, mLayoutParams);
} else {
mWindowManager.addView(mOverlayView, mLayoutParams);
mViewAdded = true;
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (mViewAdded) {
mWindowManager.removeView(mOverlayView);
mViewAdded = false;
}
}
}

In your AndroidManifest.xml, for each activity put
android:screenOrientation="landscape"
It forces the activity to landscape

Related

Brightness goes normal after changing activity

I was trying to switch between 'full brightness' and 'phones normal brightness' by using a switch button in my main activity.
I successfully handled the switching of brightness by using this code:
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
getWindow().setAttributes(params);
}
}
});
if (switchButton.isChecked()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
getWindow().setAttributes(params);
}
The problem is,
after switching to 'full Brightness', when I change the activity, the brightness goes normal.
Now, How can I keep track of the 'brightness setting' from 'main activity' and apply it to the other activities of the app?
N.B. I don't want to change system brightness. Brightness will only change while using the app.
Thanks In Advance.
ok.
Finally solved my problem.
I have created a class having the logic of brightness controlling using SharedPreferences to Handle the state.
code:
if (br.getString("set", "").equals("1")) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
getWindow().setAttributes(params);
}
Then extend the class with the activities and initialize SharedPreferences in onCreate method.
br = getSharedPreferences("br", Activity.MODE_PRIVATE);
Thank you #0X0nosugar for the idea.
You need to hold a SCREEN_BRIGHT_WAKE_LOCK if you want it to have an effect beyond your activity. The method you are using above applies to the foreground window, and when you activity goes away that no longer applies.
Note Android generally frowns on use of wake locks because it's easy to screw up and not release them, wasting the devices battery. They recommend the window param version for the vary reason that it automatically releases when the user leaves the activity.

Why is hardware acceleration not working on my View?

I'm using Facebook's Rebound library to replicate the bouncy animations seen in their chat heads implementation. The problem is, most of the time the animation stutters. A few pictures will explain this better. Here's the buttery-smooth chat heads animation:
And here's my attempt (notice how the animation for the white View skips nearly all frames):
Once in a while it works smoothly:
Below is the code I'm using currently (the entire project is up on Github if you want to set it up quickly). I'm guessing this has something to do with hardware acceleration not being enabled correctly on my View. There are 2 Springs in my SpringSystem, one for the "bubble" (the Android icon) and another for the content (the white View that is displayed on tapping the bubble). Any help on how to solve this issue would be greatly appreciated. Thanks.
AndroidManifest.xml:
<application android:hardwareAccelerated="true" ...>
...
</application>
AppService.java:
// the following code is in AppService#onCreate()
// AppService extends android.app.Service
// full code at https://github.com/vickychijwani/BubbleNote
mContent.setLayerType(View.LAYER_TYPE_HARDWARE, null);
final Spring bubbleSpring = system.createSpring();
bubbleSpring.setCurrentValue(1.0);
bubbleSpring.addListener(new SpringListener() {
#Override
public void onSpringUpdate(Spring spring) {
float value = (float) spring.getCurrentValue();
params.x = (int) (mPos[0] * value);
params.y = (int) (mPos[1] * value);
mWindowManager.updateViewLayout(mBubble, params);
// fire the second animation when this one is about to end
if (spring.isOvershooting() && contentSpring.isAtRest()) {
contentSpring.setEndValue(1.0);
}
}
// ...
});
final Spring contentSpring = system.createSpring();
contentSpring.setCurrentValue(0.0);
contentSpring.addListener(new SpringListener() {
#Override
public void onSpringUpdate(Spring spring) {
// always prints false?!
Log.d(TAG, "hardware acc = " + mContent.isHardwareAccelerated());
float value = (float) spring.getCurrentValue();
// clamping is required to prevent flicker
float clampedValue = Math.min(Math.max(value, 0.0f), 1.0f);
mContent.setScaleX(value);
mContent.setScaleY(value);
mContent.setAlpha(clampedValue);
}
// ...
});
I've figured it out by going through the framework source code.
TL;DR: add WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED to the layout flags when you manually attach a View to a Window / WindowManager; setting android:hardwareAccelerated=true in the manifest won't work.
I'm manually attaching my View to the WindowManager (because I need to create my UI in a Service to emulate chat heads) like so:
// code at https://github.com/vickychijwani/BubbleNote/blob/eb708e3910a7279c5490f614a7150009b59bad0b/app/src/main/java/io/github/vickychijwani/bubblenote/BubbleNoteService.java#L54
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mBubble = (LinearLayout) inflater.inflate(R.layout.bubble, null, false);
// ...
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
// ...
mWindowManager.addView(mBubble, params);
Let's go digging...
Welcome to the Android framework
I started debugging at View#draw(...), then went up the call stack to ViewRootImpl#draw(boolean). Here I came across this piece of code:
if (!dirty.isEmpty() || mIsAnimating) {
if (attachInfo.mHardwareRenderer != null && attachInfo.mHardwareRenderer.isEnabled()) {
// Draw with hardware renderer.
mIsAnimating = false;
mHardwareYOffset = yoff;
mResizeAlpha = resizeAlpha;
mCurrentDirty.set(dirty);
dirty.setEmpty();
attachInfo.mHardwareRenderer.draw(mView, attachInfo, this,
animating ? null : mCurrentDirty);
} else {
// If we get here with a disabled & requested hardware renderer, something went
// wrong (an invalidate posted right before we destroyed the hardware surface
// for instance) so we should just bail out. Locking the surface with software
// rendering at this point would lock it forever and prevent hardware renderer
// from doing its job when it comes back.
// Before we request a new frame we must however attempt to reinitiliaze the
// hardware renderer if it's in requested state. This would happen after an
// eglTerminate() for instance.
if (attachInfo.mHardwareRenderer != null &&
!attachInfo.mHardwareRenderer.isEnabled() &&
attachInfo.mHardwareRenderer.isRequested()) {
try {
attachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
mHolder.getSurface());
} catch (OutOfResourcesException e) {
handleOutOfResourcesException(e);
return;
}
mFullRedrawNeeded = true;
scheduleTraversals();
return;
}
if (!drawSoftware(surface, attachInfo, yoff, scalingRequired, dirty)) {
return;
}
}
}
In my case ViewRootImpl#drawSoftware() was being called, which uses the software renderer. Hmm... that means the HardwareRenderer is null. So I went searching for the point of construction of the HardwareRenderer, which is in ViewRootImpl#enableHardwareAcceleration(WindowManager.LayoutParams):
// Try to enable hardware acceleration if requested
final boolean hardwareAccelerated =
(attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
if (hardwareAccelerated) {
// ...
mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent);
// ...
}
Aha! There's our culprit!
Back to the problem at hand
In this case Android does not automatically set FLAG_HARDWARE_ACCELERATED for this Window, even though I've set android:hardwareAccerelated=true in the manifest. So the fix is simply:
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mBubble = (LinearLayout) inflater.inflate(R.layout.bubble, null, false);
// ...
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
// NOTE
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
// ...
mWindowManager.addView(mBubble, params);
Although the animation is still not as smooth as Facebook's. I wonder why... (before anyone asks: no, there are no copious logs during the animation; and yes, I've tried with a release build)

Prevent status bar for appearing android (modified)

I am implementing a kiosk mode application and i have successfully made the application full-screen without status bar appearance post 4.3 but unable to hide status bar in 4.3 and 4.4 as status-bar appears when we swipe down at the top of the screen.
I have tried to make it full screen by
speciflying the full screen theme in manifest
setting window Flags ie setFlags
setSystemUiVisibility
Possible duplicate but no concrete solution found
Permanently hide Android Status Bar
Finally the thing i want is, how to hide status bar permanently in an activity?? in android 4.3,4.4,5,6versions
We could not prevent the status appearing in full screen mode in kitkat devices, so made a hack which still suits the requirement ie block the status bar from expanding.
For that to work, the app was not made full screen. We put a overlay over status bar and consumed all input events. It prevented the status from expanding.
note:
customViewGroup is custom class which extends any
layout(frame,relative layout etc) and consumes touch event.
to consume touch event override the onInterceptTouchEvent method of
the view group and return true
Updated
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
customViewGroup implementation
Code :
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
In Android M you have to get an extra permission for making overlays. android.permission.SYSTEM_ALERT_WINDOW is not enough! So I used the code from the answer of Abhimaan within disableStatusBar() and had to make an intent to open the right settings dialog. I also added removing view in onDestroy() in order to enable status bar when the app exits. I also reduced the overlay height to 40 as it seems to be enough. Code works with 5.1 and 6.0 here.
public static final int OVERLAY_PERMISSION_REQ_CODE = 4545;
protected CustomViewGroup blockingView = null;
#Override
protected void onDestroy() {
super.onDestroy();
if (blockingView!=null) {
WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
manager.removeView(blockingView);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "Please give my app this permission!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
} else {
disableStatusBar();
}
}
else {
disableStatusBar();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "User can access system settings without this permission!", Toast.LENGTH_SHORT).show();
}
else
{ disableStatusBar();
}
}
}
protected void disableStatusBar() {
WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to receive touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (40 * getResources().getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
blockingView = new CustomViewGroup(this);
manager.addView(blockingView, localLayoutParams);
}
For a project I worked on I had found a solution for this but it took a long time. Various threads on Stackoverflow and elsewhere helped me to come up with it. It was a work around on Android M but it worked perfectly. As someone asked for it so I thought I should post it here if it can benefit anyone.
Now that its been a while, I don't remember all the details, but the CustomViewGroup is the class which overrides the main ViewGroup, and detects that a user has swiped from top to show the status bar. But we didn't want to show it, so the user's intercept was detected and any further action was ignored, i.e. Android OS won't get a signal to open the hidden status bar.
And then the methods to show and hide the status bar are also included which you can copy/paste as is in your code where you want to show/hide the status bar.
/**
* This class creates the overlay on the status bar which stops it from expanding.
*/
public static class CustomViewGroup extends ViewGroup {
public CustomViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "********** Status bar swipe intercepted");
return true;
}
}
public static void allowStatusBarExpansion(Context context) {
CustomViewGroup view = new CustomViewGroup(context);
WindowManager manager = ((WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
manager.removeView(view);
}
// Stop expansion of the status bar on swipe down.
public static void preventStatusBarExpansion(Context context) {
WindowManager manager = ((WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
Activity activity = (Activity) context;
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to receive touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
//http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resId > 0) {
result = activity.getResources().getDimensionPixelSize(resId);
}
localLayoutParams.height = result;
localLayoutParams.format = PixelFormat.TRANSPARENT;
CustomViewGroup view = new CustomViewGroup(context);
manager.addView(view, localLayoutParams);
}

turn off the display even if the app is in background

I use the following code to create on the flight a window used as preview when a picture is taken:
void CreatePreviewDialog()
{
dummy_frame_layout = new DummyFrameLayout(context);
wm_params = new WindowManager.LayoutParams(
240, 320, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
wm_params.gravity = Gravity.CENTER;
wm_params.setTitle("Preview");
window_manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
window_manager.addView(dummy_frame_layout, wm_params);
}
My app runs in background and it is waked-up by an alarm. It turns-on the display as you can see. As soon as the picture is taken, the window is destroyed using the following method:
void DestroyPreviewDialog()
{
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).removeView(dummy_frame_layout);
dummy_frame_layout = null;
}
The problem is that the screen remains on. I would like to turn-off the screen when the preview window is closed. How does DestroyPreviewDialog() should be modified in such a way to turn-off the display? (of course the display should be turned off only if it was found turned off when CreatePreviewDialog() was called, but this is simple. For now I need a way to turn off the display)
EDIT
I've modified DestroyPreviewWindow() as follows:
private void DestroyPreviewDialog()
{
wm_params.screenBrightness = 0.0f;
window_manager.updateViewLayout(dummy_frame_layout, wm_params);
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).removeView(dummy_frame_layout);
dummy_frame_layout = null;
}
but the results does not change. Screen remains on and bright!

Android: Configuration Change Not Updating

I am using the XML tag
android:configChanges="orientation|keyboardHidden|keyboard"
and the following code to detect device orientation changes and change layouts:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
disp = getWindowManager().getDefaultDisplay();
swidth = disp.getWidth();
sheight = disp.getHeight();
parent.removeAllViews();
parent = new RelativeLayout(this);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
layoutPortrait();
else
layoutLandscape();
}
This works fine going form portrait to landscape. But, for whatever reason, going from landscape to portrait (starting in landscape or switching to it then back) doesn't change the screen back to portrait.
Through the use of Log messages I've determined that after being in Landscape mode, the Display and Configuration classes DO NOT UPDATE. They remain holding the same orientation/length/width values as when the device was in landscape.
Does anyone have any idea why this is?
Additional Code (requested by contributor)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disp = getWindowManager().getDefaultDisplay();
swidth = disp.getWidth();
sheight = disp.getHeight();
int ornt;
if(swidth == sheight)
ornt = Configuration.ORIENTATION_SQUARE;
else if(swidth < sheight)
ornt = Configuration.ORIENTATION_PORTRAIT;
else if(swidth > sheight)
ornt = Configuration.ORIENTATION_LANDSCAPE;
else
ornt = Configuration.ORIENTATION_UNDEFINED;
parent = new RelativeLayout(this);
labelOne = new TextView(this);
labelOne.setText("Temperature");
labelOne.setId((int)(Math.random()*Integer.MAX_VALUE));
temp = new EditText(this);
temp.setSingleLine(true);
temp.setBackgroundColor(Color.WHITE);
temp.setTextColor(Color.BLACK);
temp.setId((int)(Math.random()*Integer.MAX_VALUE));
labelTwo = new TextView(this);
labelTwo.setText("Humidity(%)");
labelTwo.setId((int)(Math.random()*Integer.MAX_VALUE));
humidity = new EditText(this);
humidity.setSingleLine(true);
humidity.setBackgroundColor(Color.WHITE);
humidity.setTextColor(Color.BLACK);
humidity.setId((int)(Math.random()*Integer.MAX_VALUE));
output = new TextView(this);
output.setBackgroundColor(Color.WHITE);
output.setTextColor(Color.BLACK);
output.setId((int)(Math.random()*Integer.MAX_VALUE));
submit = new Button(this);
submit.setText("Calculate");
submit.setId((int)(Math.random()*Integer.MAX_VALUE));
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Double result = Calculate.calculate(getInputs());
if(result !=null) {
String answer = String.format("%,.1f",result);
if(f.isChecked()) {
output.setText(answer + "°F");
} else {
output.setText(answer + "°C");
}
}
}
});
f = new RadioButton(this);
f.setText("Fahrenheit");
c = new RadioButton(this);
c.setText("Celsius");
rg = new RadioGroup(this);
rg.setOnCheckedChangeListener(new ListenForMode());
rg.addView(f);
rg.addView(c);
rg.setId((int)(Math.random()*Integer.MAX_VALUE));
f.setChecked(true);
if(ornt == Configuration.ORIENTATION_PORTRAIT || ornt == Configuration.ORIENTATION_SQUARE)
layoutPortrait();
else
layoutLandscape();
}
Final Update
Issue is with the emulator not responding correctly to the orientation changes. I changed the onConfigurationChanged(...) orientation checking to use the condition width < height for portrait and all else landscape. This works perfectly on my Android device.
Thanks to all contributors to this question!
You really shouldn't be overriding the configChanges in your manifest. Android can handle redrawing the Views for you, and it actually handles it really well, especially if you have filtered layouts in your resource folder.
If you are overriding configChanges because of issues with AsyncTask losing its Context (as I once did), I would recommend using an IntentService to handle your asynchronous work as Services aren't tied to any one Activity.
If it happens on emulator, then it's OK - emulator behaves a little bit weird in sense of configuration changes. I have been trying to solve similar problem, unless I noticed that the app worked fine on a real device.
It seems that the emulator ignores the android:configChanges="orientation" attribute, but in a bit strange way: it does force the configuration change but doesn't call onConfigurationChanged(..) method every time it should be called.

Categories

Resources