I would like to now if the is any possible way to trigger an event when the layout is being redrawn because of the event of switching directly from landscape right orientation to landscape left orientation.
I tried the OrientationEventListener. The problem is that it triggers to fast. I am doing UI changes and they happen before the landscape is being redrawn, and it looks bad.
I could just add some delay on the changes but depending on the phone that change could last more or less.
For the onConfigurationChanged the timing is perfect, but of course it only works for portrait to landscape or landscape to portrait.
My application must not pause in this change, so using onPause, onStop, etc is not an option.
So I am looking for an event similar to onConfigurationChanged that also covers landscape right to landscape left changes.
I tried the OrientationEventListener. The problem is that it triggers
to fast. I am doing UI changes and they happen before the landscape is
being redrawn, and it looks bad.
You can use handler.post(runnable), that way your UI changes shouldn`t happen before the redrawn, but will be queued on the main thread and executed after it finishes drawing.
handler.post(new Runnable() {
@Override
public void run() {
//do the UI changes you want
}
});
If this still doesn`t help out you can try a difficult and ugly way to make use of orientation listener and the onLayout() method :)
You can do the following:
int result = this.getResources().getConfiguration().orientation;
if (result == 1){
a//set content view to portrait
}
else{
//set content view to landscape}
I think you almost get it. I do not know if you do similar thing, but this one let you doing UI change as soon as the orientation change. I did this example for only changing from ROTATION_270
to ROTATION_90
, but you can add to the code so that I can cover all cases from PORTRAIT
to REVERSE_PORTRAIT
etc...
My example is simply changing a textview from "Landscape" to "reverse landscape" when orientation changes.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Code
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.widget.TextView;
public class LandScapeToLandScapeReverse extends Activity
{
private int mCurrentOrientation;
private Display mDisplay;
private OrientationEventListener mOrientationEventListener;
private TextView mTextView;
private static final String TAG = "LandScape To LandScape Reverse";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textview);
mDisplay = getWindowManager().getDefaultDisplay();
mCurrentOrientation = mDisplay.getRotation();
mTextView.setText("CurrentOrientation: " + mCurrentOrientation);
mOrientationEventListener = new OrientationEventListener(this)
{
@Override
public void onOrientationChanged(int orientation)
{
// On my phone it seem to change at 233 maybe should start lower
// to take into account of phones model variations.
if (mCurrentOrientation == Surface.ROTATION_270 && orientation > 230)
{
mTextView.setText("CurrentOrientation: " + orientation);
Log.d(TAG, "orientation = " + orientation
+ " getRotation: " + mDisplay.getRotation());
if (mDisplay.getRotation() != mCurrentOrientation)
{
// I think one should disable listener here and enable
// again after doing all the UI changes.
mOrientationEventListener.disable();
mTextView.setText("Landscape reverse");
}
}
}
};
mOrientationEventListener.enable();
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.d(TAG, "onDestroy");
mOrientationEventListener.disable();
}
}
I got some idea maybe there is a function that is called before the orientation change that we can override. I will play around and if I find one I will update the answer.
You can use DisplayListener which gets triggered in this case, as described in this answer: https://stackoverflow.com/a/23612548/2942294