I've searched for a solution to this problem and have not found anything. The problem is that I have created an activity and I want this to change the orientation of the screen when the user displays a dialog. The activity has started with the portrait orientation, which is defined in the AndroidManifest.xml to not be able to change when the user rotate the screen, it only changes the orientation when the user presses a button to show the dialog. But when this happens, the activity changes the orientation, but once changed it is closed. I have checked the logcat, but does not throw any exception.
This way I have defined my activity in the AndroidManifest.xml:
<activity
android:name=".ui.BarCodeActivity"
android:label="#string/title_activity_barcode"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
And this is the code I use to change the screen orientation at that time:
#Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
Log.e(TAG, "onSave startDialog: " + startDialog);
state.putBoolean("startDialog", startDialog);
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle state) {
startDialog = state.getBoolean("startDialog", false);
Log.e(TAG, "onRestore startDialog: " + startDialog);
if (startDialog) {
showDialog();
}
}
private void showDialog() {
Log.e(TAG, "creating dialog");
Dialog barcodeDialog = new Dialog(BarCodeActivity.this);
barcodeDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialogInterface) {
dialogInterface.dismiss();
startDialog = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
barcodeDialog.setContentView(R.layout.code_dialog);
barcodeDialog.setTitle("Scan barcode");
ImageView barcode = (ImageView) barcodeDialog.findViewById(R.id.dialog_barcode);
barcode.setImageDrawable(barcodeDrawable);
TextView codeView = (TextView) barcodeDialog.findViewById(R.id.dialog_code);
codeView.setText(code);
/*barcodeDialog.show();*/
}
When the activity will start again and then closes, I have seen that in the logcat output strange, if not you will have something to see, I've been researching but I have not had luck with the solutions. This is the output:
08-01 19:21:21.600 31521-31521/com.my.app W/ResourceType﹕ Failure getting entry for 0x7f090058 (t=8 e=88) in package 0 (error -75)
08-01 19:21:21.600 31521-31521/com.my.app W/ResourceType﹕ Failure getting entry for 0x7f090058 (t=8 e=88) in package 0 (error -75)
08-01 19:21:21.610 31521-31521/com.my.app D/not﹕ got -1
08-01 19:21:21.610 31521-31521/com.my.app D/not﹕ got -1
08-01 19:21:21.620 31521-31521/com.my.app D/not﹕ got -1
08-01 19:21:21.630 31521-31521/com.my.app D/not﹕ got -1
What can be the error?
If it helps I have updated the Android Studio to 1.3 version.
Try changing
<activity
android:name=".ui.BarCodeActivity"
android:label="#string/title_activity_barcode"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
to
<activity
android:name=".ui.BarCodeActivity"
android:label="#string/title_activity_barcode"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
and test with this code in your activities.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
I know this is old but when you use configChanges you must define both orientation and screensize as shown below if you support all the versions of android else take a look at this documentation.
Therefore, change android:configChanges="keyboardHidden|orientation"
to
android:configChanges="orientation|screenSize|keyboardHidden"
and it should work because we use that in our app.
Hope, this helps someone.
Related
I finished my app with multi languages , but when the device orientation is changed to landscape then app's language is changed .
can I use (onSaveInstanceState) method to keep app language without changing when rotating screen?
what is the code I have to insert ??
Try this in your manifest
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
And then
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// you can do something here
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// orientation_landscape
Toast.makeText(this, "orientation_landscape", Toast.LENGTH_SHORT).show();
} else {
// orientation_portrait
Toast.makeText(this, "orientation_portrait", Toast.LENGTH_SHORT).show();
}
}
Is there any way to disable Reverse Landscape and Reverse portrait orientations in an android activity. I used the below code.but reverse landscape is coming on that.
rotation = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation();
System.out.println("Rotation Value : " +rotation);
if(rotation==0){
System.out.println("portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
if(rotation==1){
System.out.println("landscape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}
if(rotation==2 )
{
System.out.println("reverse portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
if(rotation==3)
{
System.out.println("reverse landscape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
Add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself, in this case by doing nothing.
<activity
android:name="MainActivity"
android:screenOrientation="portrait" //This line only if you want to lock your screen Orientation
android:configChanges="keyboardHidden|orientation|screenSize">
Check http://developer.android.com/reference/android/R.attr.html#configChanges for more details.
Then override onConfigurationChanged :
http://developer.android.com/guide/topics/resources/runtime-changes.html
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
my layout is very heavy, so when you turn the screen takes a long time. How can I save my view "onSaveInstanceState" ??
My code:
if (tab.getPosition() == 0) {
/*
* ----------------- TAB DATA -----------------
*/
try {
System.gc();
// initialize the class that needs to load the Activity
myLayout = new TableMainLayout(this);
// I tried to give some time to load, but that's not good!
synchronized (this) {
wait(5000);
}
setContentView(myLayout);
} catch (Exception e) {
Log.i("link", "Errore actionBarTabs update Series: " + e);
}
}
Thanks
I understood when you change the orientation app does not want to read the data back, well put this code in manifest file in your activity that you do not want to read again.
android:configChanges="orientation"
This code allows you to freeze your activity.
For example:
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="#string/app_name"
android:theme="#style/AppBaseTheme" >
</activity>
I've got a problem. My AdMob has been set up for some time now without any problem, but I noticed something wrong. Ad gets successfully loaded (i see message from ddms), but it won't show. It will get shown after periodical 60 seconds refresh or when I open up login to google plus. The problem happens only with Google Play Services AdMob and not with AdMobSDK jar. I'd switch to AdMob jar, however I'm using Google Play Game Services for leaderboards and achievements.
I suspect the problem is with view not being shown, or inproper settings.
So again, ad will show itself after 60 seconds (along with ad refresh) of waiting, or when I fire up the log in screen for google play services.
I'm adding my code, also I should mention that I've switched to new AdMob website and I repeat, that problem is not happening if I use AdMob jar file (the ad is then shown in 2-3 seconds like normal). I've cut the google play game services code (they don't affect this issue because i've tried in my other app without them, and the problem is still there).
MainActivity code:
public class MainActivity extends AndroidApplication {
public static enum AdsStatus {
SHOW_ADS, HIDE_ADS;
}
protected RelativeLayout layout;
protected static AdView adMobView;
public static class InnerHandler extends Handler {
WeakReference<MainActivity> mActivity;
InnerHandler(MainActivityactivity) {
mActivity = new WeakReference<MainActivity>(activity);
}
#Override
public void handleMessage(final Message msg) {
if(msg.obj instanceof AdsStatus) {
switch((AdsStatus)msg.obj) {
case SHOW_ADS:
mActivity.get().showAds();
break;
case HIDE_ADS:
mActivity.get().hideAds();
break;
}
}
}
}
protected Handler handler = new InnerHandler(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the layout
layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new MainApplicationListener(), true);
layout.addView(gameView);
//Ad Mob
final DisplayMetrics displayMetrics = MainActivity.this
.getApplicationContext().getResources()
.getDisplayMetrics();
if (displayMetrics.widthPixels >= 800 && displayMetrics.heightPixels >= 480) {
if(adMobView != null) {
adMobView.destroy();
}
adMobView = new AdView(AirDance.this);
adMobView.setAdUnitId(<MY_ID_IS_HERE>);
adMobView.setAdSize(AdSize.SMART_BANNER);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(<MY_DEVICE_IS_HERE>)
.build();
adMobView.loadAd(adRequest);
layout.addView(adMobView, adParams);
}
// Hook it all up
setContentView(layout);
}
#Override
protected void onResume() {
super.onResume();
AppRater.applicationLaunched(this, analytics);
if(adMobView != null) {
adMobView.resume();
}
}
#Override
protected void onPause() {
super.onPause();
if(adMobView != null) {
adMobView.pause();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(adMobView != null) {
adMobView.destroy();
adMobView = null;
}
}
public void showAds() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(adMobView != null) {
adMobView.setEnabled(true);
adMobView.setVisibility(View.VISIBLE);
}
}
});
}
public void hideAds() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(adMobView != null) {
adMobView.setEnabled(false);
adMobView.setVisibility(View.GONE);
}
}
});
}
}
And here is AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..."
android:installLocation="auto"
android:versionCode="16"
android:versionName="1.2.6" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.screen.landscape"/>
<uses-feature android:name="android.hardware.touchscreen.multitouch" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="MainActivity">
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="#string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="...MainActivity"
android:label="MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- adMob -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
EDIT :
okay, so ad gets visible after I lock and unlock screen with app open, also you can click on invisible ad
EDIT :
Okay I think I solved it. I just manually reload whole layout onAdLoad event. Anyway, this is just functional solution, it does not explain why it happens with Google Play
Services AdMob.
adMobView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
layout.requestLayout();
}
});
}
});
As nobody has replied with explanation, I'm going to consider this one solved. I just manually reload whole layout onAdLoad event. Anyway, this is just functional solution, it does not explain why it happens with Google Play Services AdMob.
adMobView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
layout.requestLayout();
}
});
}
});
Also, as mentioned by user3263204, you can try this
adMobView.setBackgroundColor(Color.BLACK);
to solve your problem.
The above solution works. Even a simpler way to keep the banner shown is to set its background.
adMobView.setBackgroundColor(Color.BLACK);
As user3263204 says, you can use setBackgroundColor to make the adview appear.
You can make the background transparent, if (as in my case) you have the adview over something else:
adMobView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
You can also requestWindowFeature(Window.FEATURE_NO_TITLE); and set to full screen and the ads shows.
banner.setBackgroundColor(Color.TRANSPARENT);
This seems to fix the issue for me. Also be sure to run the banner creation code from within the UI thread.
activity.runOnUiThread(new Runnable()
{
#Override
public void run()
{
// all your banner creation code is here
banner.setBackgroundColor(Color.TRANSPARENT);
}
});
I need to detect hidden key board when hidden keyboard is pressed
My source code
<activity
android:name="com.teamios.info.activity.MainScreenActivity"
android:screenOrientation="landscape"
android:theme="#style/Theme.MyScreenTranNorman"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateUnchanged|adjustPan" />
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
}
I tested in samsung galaxy nexus phone android os 4.2.1, but Toast didn't show when keyboard hidden
Please help me.
<activity
android:name="com.teamios.info.activity.MainScreenActivity"
android:theme="#style/Theme.MyScreenTranNorman"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateUnchanged|adjustPan" />
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
And add
<activity android:name=".MyActivity" android:screenOrientation="landscape " > </activity>
in menifest class.
Did you add android:configChanges="keyboardHidden" in your manifest file ?