styles.xml with android version qualifier cause app crash - android

My app use a different style for minimum version of Android:
My app use a minimum version: API 9 Android 2.3 (Gingerbread)
I use Version qualifier for assign a different style to every Android version:
My styles file from values/style.xml is:
<style name="AppTheme" parent="android:Theme">
</style>
My styles file from values-v11/styles.xml (if device is Android 3.0 or later):
<style name="AppTheme" parent="android:Theme.Holo">
</style>
My styles file from values-v14/styles.xml (if device is Android 4.0 or later):
<style name="AppTheme" parent="android:Theme.DeviceDefault">
</style>
The following .xml is only a fragment of AndroidManifest.xml
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
</application>
My issue is: When the app runs in my phone, the app crashes unexpectedly, I know that my issue is in the use of version qualifiers.

You use Android 2.3 for more compatibility.
On Android Studio, the activities for minimum versions (like Android 2.3 Gingerbread or earlier) are created automatically with the following code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The ActionBarActivity isn't compatible with styles different to AppCompat
Try to convert from ActionBarActivity to Activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Related

Splashscreen API not showing icon

We are currently implementing a splashscreen via the new Splashscreen API. We are following the migration guide. The background color is set correctly (by windowSplashScreenBackground), but the icon is not visible, neither on an Emulator, nor on a physical device.
build.gradle
android {
compileSdkVersion 31
...
}
dependencies {
implementation "androidx.core:core-splashscreen:1.0.0-alpha01"
...
}
values-v31/themes.xml
<style name="AppTheme" parent="Theme.SplashScreen">
<item name="postSplashScreenTheme">#style/AppThemeCompat</item>
<item name="windowSplashScreenBackground">#android:color/black</item>
<item name="windowSplashScreenAnimatedIcon">#mipmap/ic_launcher</item>
<item name="windowSplashScreenAnimationDuration">200</item>
</style>
AndroidManifest.xml
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
...
</application>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen()
...
}
I am answering this question directly as we found the solution to our problem within one or two hours that could be saved from other developers:
Icons set by the Splashscreen API do not work when starting an app from Android Studio. If the app gets closed and then restarted, the icon shows correctly.
This bug might exist due to the early alpha status of this library and will allegedly be fixed in a future version of the Splashscreen API.

Android 12 splash screen before api 21

I'm trying to upgrade my application to target android 31 which introduces splash screen API so I followed the migration process mentioned here, but after migration the application doesn't run because splash screen API only supports android version 21 and above so what is the process to support older versions than 21?
Androidx SplashScreen class works as following:
On API 31+ (Android 12+) this class calls the platform methods.
Prior API 31, the platform behavior is replicated with the exception of the Animated Vector Drawable support on the launch screen.
the problem is Animated Vector Drawable class was introduced with Android 21 so the current Androidx SplashScreen class backward compatible with Android +21 so i figured out some solution.
I created two different splash screen activities one to handle the old versions and the second which use Androidx SplashScreen API. and I made the system lunches splash screen based on the current android version, so I did the following
1- to allow application compiling and build, I had to add this line to my manifest file
<uses-sdk tools:overrideLibrary="androidx.core.splashscreen"/>
2- create the new Splash screen activity which uses Androidx SplashScreen API by following the migration steps from documentation.
3- create bools.xml under values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="new_splash_enabled">false</bool>
<bool name="old_splash_enabled">false</bool>
</resources>
4- override bools.xml in values-vX (X is the minSdkVersion) in my case was 16 so i created values-v16 folder in the same level of values folder and create bools.xml under it like below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="new_splash_enabled">false</bool>
<bool name="old_splash_enabled">true</bool>
</resources>
5- override bools.xml in values-vX (X is the min version you want to apply the new SplashScreen so it is any number between 21 and 31)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="new_splash_enabled">true</bool>
<bool name="old_splash_enabled">false</bool>
</resources>
6- in your manifest, i made the system decides which splash activity to launch based on values in bools.xml file
<activity
android:name=".NewSplash"
android:theme="#style/Theme.App.Starting"
android:enabled="#bool/new_splash_enabled"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OldSplash"
android:enabled="#bool/old_splash_enabled">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you're using the Androidx Compat Library make sure you're using the attributes without the android tag.
For example:
This will work fine
<item name="windowSplashScreenBackground">?colorPrimary</item>
This will give you a "require API 31" error
<item name="android:windowSplashScreenBackground">?colorPrimary</item>
Lazy approach: in short programmatically set theme -> done.
Advantage: Only one activity. Basically a one liner
Disadvantage: SDK <=21 No splash, but everyone else
Long answer, I don't like the two Activities logic. So:
Follow step 1 to 2 of #Ramy-Ibrahim's answer.
set in application block of your manifest:
android:theme="#style/Theme.App.Starting"
My launcher activity has no theme set anymore instead in onCreate():
//v6.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setTheme(R.style.Theme_App_Starting);
SplashScreen.installSplashScreen(this);
}else{
setTheme(R.style.AppTheme);
}
//mind everything BEFORE super.onCreate()
super.onCreate(savedInstanceState);
Tested in Android 11(SDK30) and Android 4.4 (SDk19) and Android 12.2 (SDK32)
What worked for me , and inspired by #Ramy Ibrahim answer
in your manifest file before <application> tag add
<uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />
in your theme/style
<style name="ThemeStarting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#color/_background_color</item>
<item name="windowSplashScreenAnimatedIcon">#drawable/icon</item>
<item name="windowSplashScreenAnimationDuration">1000</item>
<item name="postSplashScreenTheme">#style/YOUR_THEME</item>
</style>
NewSplashActivity
public class NewSplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
finish();
}
else {
setTheme(R.style.OldSplashTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.new_spl);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
finish();
}
}, 1999);
}
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}

IllegalStateException: Only fullscreen opaque activities can request orientation [duplicate]

I am facing the problem while retrieving the contacts from the contact book in Android 8.0 Oreo java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
I am trying to get the contact in my activity from the phone contact book and it works perfect for Lollipop, Marshmallow, Nougat, etc but it will gives me the error for Oreo like this please help me. My code is here below.
Demo Code :-
private void loadContacts() {
contactAsync = new ContactLoaderAsync();
contactAsync.execute();
}
private class ContactLoaderAsync extends AsyncTask<Void, Void, Void> {
private Cursor numCursor;
#Override
protected void onPreExecute() {
super.onPreExecute();
Uri numContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] numProjection = new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
if (android.os.Build.VERSION.SDK_INT < 11) {
numCursor = InviteByContactActivity.this.managedQuery(numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
} else {
CursorLoader cursorLoader = new CursorLoader(InviteByContactActivity.this, numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
numCursor = cursorLoader.loadInBackground();
}
}
#Override
protected Void doInBackground(Void... params) {
if (numCursor.moveToFirst()) {
try {
final int contactIdIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
final int displayNameIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
final int numberIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
final int typeIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
String displayName, number, type;
do {
displayName = numCursor.getString(displayNameIndex);
number = numCursor.getString(numberIndex);
type = getContactTypeString(numCursor.getString(typeIndex), true);
final ContactModel contact = new ContactModel(displayName, type, number);
phoneNumber = number.replaceAll(" ", "").replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("-", "");
if (phoneNumber != null || displayName != null) {
contacts.add(phoneNumber);
contactsName.add(displayName);
contactsChecked.add(false);
filterdNames.add(phoneNumber);
filterdContactNames.add(displayName);
filterdCheckedNames.add(false);
}
} while (numCursor.moveToNext());
} finally {
numCursor.close();
}
}
Collections.sort(contacts, new Comparator<String>() {
#Override
public int compare(String lhs, String rhs) {
return lhs.compareToIgnoreCase(rhs);
}
});
InviteByContactActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mContactAdapter.notifyDataSetChanged();
}
});
return null;
}
}
private String getContactTypeString(String typeNum, boolean isPhone) {
String type = PHONE_TYPES.get(typeNum);
if (type == null)
return "other";
return type;
}
static HashMap<String, String> PHONE_TYPES = new HashMap<String, String>();
static {
PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_HOME + "", "home");
PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + "", "mobile");
PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_WORK + "", "work");
}
}
Error Log:-
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example, PID: 6573
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Activity.InviteByContactActivity}: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
In android Oreo (API 26) you can not change orientation for Activity that have below line(s) in style
<item name="android:windowIsTranslucent">true</item>
or
<item name="android:windowIsFloating">true</item>
You have several way to solving this :
1) You can simply remove above line(s) (or turn it to false) and your app works fine.
2) Or you can first remove below line from manifest for that activity
android:screenOrientation="portrait"
Then you must add this line to your activity (in onCreate())
'>=' change to '!=' thanks to Entreco comment
//android O fix bug orientation
if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
3) You can create new styles.xml in values-v26 folder and add this to your style.xml. (Thanks to AbdelHady comment)
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowIsFloating">false</item>
In Android O and later this error happens when you set
android:screenOrientation="portrait"
in Manifest.
Remove that line and use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in your activity.
This will fix your issue.
Google throws this exception on Activity's onCreate method after v27, their meaning is : if an Activity is translucent or floating, its orientation should be relied on parent(background) Activity, can't make decision on itself.
Even if you remove android:screenOrientation="portrait" from the floating or translucent Activity but fix orientation on its parent(background) Activity, it is still fixed by the parent, I have tested already.
One special situation : if you make translucent on a launcher Activity, it has't parent(background), so always rotate with device. Want to fix it, you have to take another way to replace <item name="android:windowIsTranslucent">true</item> style.
The problem seems to be happening when your target sdk is 28. So after trying out many options finally this worked.
<activity
android:name=".activities.FilterActivity"
android:theme="#style/Transparent"
android:windowSoftInputMode="stateHidden|adjustResize" />
style:-
<style name="Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Note:parent="Theme.AppCompat.Light.NoActionBar" is needed for api 28. Previously had something else at api 26. Was working great but started to give problem at 28.
Hope it helps someone out here.
EDIT: For some only by setting <item name="android:windowIsTranslucent">false</item> and <item name="android:windowIsFloating">false</item>
worked.May be depends upon the way you implement the solution works.In my case it worked by setting them to true.
If you use a fullscreen transparent activity, there is no need to specify the orientation lock on the activity. It will take the configuration settings of the parent activity. So if the parent activity has in the manifest:
android:screenOrientation="portrait"
your translucent activity will have the same orientation lock: portrait.
I used android:screenOrientation="behind" instead of android:screenOrientation="portrait". Basically, you created a dialog (in an activity) and dialog can't request orientation by itself it needs parent activity to do this (because a parent is visible in the background and has own layout).
"behind" The same orientation as the activity that's immediately beneath it in the activity stack.
The only solution that really works :
Change:
<item name="android:windowIsTranslucent">true</item>
to:
<item name="android:windowIsTranslucent">false</item>
in styles.xml
But this might induce a problem with your splashscreen (white screen at startup)... In this case, add the following line to your styles.xml:
<item name="android:windowDisablePreview">true</item>
just below the windowIsTranslucent line.
Last chance if the previous tips do not work : target SDK 26 instead o 27.
If you have to use setRequestedOrientation(), there is no way but sacrifice the windowIsTranslucent attribute on Android 8.0
values\styles.xml for api level 25- (<8.0)
<style name="Base.Theme.DesignDemo" parent="Base.Theme.AppCompat.Light">
...
<item name="android:windowIsTranslucent">true</item>
...
</style>
values-v26\styles.xml for api level 26 (=8.0)
<style name="Base.Theme.DesignDemo" parent="Base.Theme.AppCompat.Light">
...
<!-- android 8.0(api26),Only fullscreen opaque activities can request orientation -->
<item name="android:windowIsTranslucent">false</item>
...
</style>
values-v27\styles.xml for api level 27+ (>8.0)
<style name="Base.Theme.DesignDemo" parent="Base.Theme.AppCompat.Light">
...
<item name="android:windowIsTranslucent">true</item>
...
</style>
Many people have given a fix, so I'll talk about the source of the problem.
According to the exception log:
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
at android.app.Activity.onCreate(Activity.java:1081)
at android.support.v4.app.SupportActivity.onCreate(SupportActivity.java:66)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:297)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
at com.nut.blehunter.ui.DialogContainerActivity.onCreate(DialogContainerActivity.java:43)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
at android.app.ActivityThread.-wrap12(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
at android.os.Handler.dispatchMessage(Handler.java:108) 
at android.os.Looper.loop(Looper.java:166)
The code that triggered the exception in Activity.java
//Need to pay attention mActivityInfo.isFixedOrientation() and ActivityInfo.isTranslucentOrFloating(ta)
if (getApplicationInfo().targetSdkVersion >= O_MR1 && mActivityInfo.isFixedOrientation()) {
final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
ta.recycle();
//Exception occurred
if (isTranslucentOrFloating) {
throw new IllegalStateException(
"Only fullscreen opaque activities can request orientation");
}
}
mActivityInfo.isFixedOrientation():
/**
* Returns true if the activity's orientation is fixed.
* #hide
*/
public boolean isFixedOrientation() {
return isFixedOrientationLandscape() || isFixedOrientationPortrait()
|| screenOrientation == SCREEN_ORIENTATION_LOCKED;
}
/**
* Returns true if the activity's orientation is fixed to portrait.
* #hide
*/
boolean isFixedOrientationPortrait() {
return isFixedOrientationPortrait(screenOrientation);
}
/**
* Returns true if the activity's orientation is fixed to portrait.
* #hide
*/
public static boolean isFixedOrientationPortrait(#ScreenOrientation int orientation) {
return orientation == SCREEN_ORIENTATION_PORTRAIT
|| orientation == SCREEN_ORIENTATION_SENSOR_PORTRAIT
|| orientation == SCREEN_ORIENTATION_REVERSE_PORTRAIT
|| orientation == SCREEN_ORIENTATION_USER_PORTRAIT;
}
/**
* Determines whether the {#link Activity} is considered translucent or floating.
* #hide
*/
public static boolean isTranslucentOrFloating(TypedArray attributes) {
final boolean isTranslucent = attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsTranslucent, false);
final boolean isSwipeToDismiss = !attributes.hasValue(com.android.internal.R.styleable.Window_windowIsTranslucent)
&& attributes.getBoolean(com.android.internal.R.styleable.Window_windowSwipeToDismiss, false);
final boolean isFloating = attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false);
return isFloating || isTranslucent || isSwipeToDismiss;
}
According to the above code analysis, when TargetSdkVersion>=27, when using SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, and other related attributes, the use of windowIsTranslucent, windowIsFloating, and windowSwipeToDismiss topic attributes will trigger an exception.
After the problem is found, you can change the TargetSdkVersion or remove the related attributes of the theme according to your needs.
I can't agree to most rated answer, because
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
causes an error
java.lang.IllegalStateException: Only fullscreen opaque activities can
request orientation
but this makes it works for me
<style name="TranslucentTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
and use it for your Activity, when you extends from
InterstitialActivity extends AppCompatActivity
in AndroidManifest.xml
<activity
android:name=".InterstitialActivity"
...
android:screenOrientation="portrait"
android:theme="#style/TranslucentTheme" />
Just remove this line android:screenOrientation="portrait" of activity in Manifiest.xml
That activity will get orientation from it's previous activity so no need to apply orientation which has <item name="android:windowIsTranslucent">true</item>.
Just Set Orientation of activity in Manifiest.xml
android:screenOrientation="unspecified"
OR for restricted to Portrait Orientation
You can also use in Activity, In onCreate method call before super.onCreate(...) e.g.
#Override
protected void onCreate(Bundle savedInstanceState) {
setOrientation(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml_layout);
//...
//...
}
// Method
public static void setOrientation(Activity context) {
if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
else
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
it seems when target sdk is pie (api level 28.0) and windowIsTranslucent is true
<item name="android:windowIsTranslucent">true</item>
and you try to access orientation. problem comes with android oreo 8.0 (api level 26) there are two ways to solve this
remove the orientation
or set windowIsTranslucent to false
if you are setting orientation in manifest like this
android:screenOrientation="portrait"
or
in activity class like this
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
remove form both places.
and observed when u set windowIsTranslucent to true, it takes orientation from parent activity.
in the manifest file set second activity parentActivityName as first activity and remove the screenOrientation parameter to the second activity. it means your first activity is the parent and decide to an orientation of your second activity.
<activity
android:name=".view.FirstActiviy"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" />
<activity
android:name=".view.SecondActivity"
android:parentActivityName=".view.FirstActiviy"
android:theme="#style/AppTheme.Transparent" />
I had the same problem, and my solution was to eliminate the line
android:screenOrientation="portrait"
and then add this in the activity:
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
I recently faced the issue and here's the solution.
No need to change the screen orientation parameter which you set at android manifest file.
Just add two folders in
res>values
as res>values-v26
and res>values-v27
Then copy your styles.xml and themes.xml file there.
and change the following parameters from TRUE to FALSE.
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsTranslucent">false</item>
It will work.
A common bug of Android 8.0
some of the answers wasnt clear for me and didnt work,
so this was causing the error:
<activity
android:name=".ForgotPass_ChangePass"
android:screenOrientation="portrait" <--- // this caused the error
android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"/>
android studio was suggeting to set screenOrientation to fullSensor
android:screenOrientation="fullSensor"
yes this will fix the error but i wan to keep my layout in the portrait mode, and fullSensor will act based on the sensor
"fullSensor" The orientation is determined by the device orientation
sensor for any of the 4 orientations. This is similar to "sensor"
except this allows any of the 4 possible screen orientations,
regardless of what the device will normally do (for example, some
devices won't normally use reverse portrait or reverse landscape, but
this enables those). Added in API level 9.
source: android:screenOrientation
so the solution that worked for me i used "nosensor" :
<activity
android:name=".ForgotPass_ChangePass"
android:screenOrientation="nosensor"
android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"/>
"nosensor" The orientation is determined without reference to a
physical orientation sensor. The sensor is ignored, so the display
will not rotate based on how the user moves the device.
see android documentation here
After doing some research, it seems that this problem may be due to a google bug. For me, I was able to leave this line in my Activities onCreate method:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
AND I changed my targetSdkVersion to 26. Having that line in my onCreate still resulted in a crash while my targetSdkVersion was still set at 27. Since no one else's solution has worked for me thus far, I found that this works as a temporary fix for now.
only 8.0.0 throw the exception, above 8.0 has remove the exception
It is a conflict (bug) between Themes inside style.xml file in android versions 7 (Api levels 24,25) & 8 (api levels 26,27),
when you have
android:screenOrientation="portrait"
:inside specific activity (that crashes) in AndroidManifest.xml
&
<item name="android:windowIsTranslucent">true</item>
in the theme that applied to that activity inside style.xml
It can be solve by these ways according to your need :
1- Remove on of the above mentioned properties that make conflict
2- Change Activity orientation to one of these values as you need : unspecified or behind and so on that can be found here : Google reference for android:screenOrientation
`
3- Set the orientation programmatically in run time
Use
android:screenOrientation="behind"
And Theme
<style name="translucent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#beaaaaaa</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
<item name="android:typeface">normal</item>
<item name="android:windowSoftInputMode">adjustPan</item>
<item name="windowActionBar">false</item>
</style>
Probably you showing Activity looking like Dialog(non-fullscreen), so remove screenOrientation from Manifest or from code. This will fix the issue.
I faced this problem only in SDK 26 (8.0.0) if using windowIsTranslucent 'true' and forcefully setting orientation:
Here's the solution:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// fixing portrait mode problem for SDK 26 if using windowIsTranslucent = true
if (Build.VERSION.SDK_INT == 26) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
<style name="SplashActivty" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
</style>
<!-- Splash screen -->
<activity
android:name="edu.aku.family_hifazat.activities.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="unspecified"
android:theme="#style/SplashActivty">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I was getting this error when I try to capture image or take image from gallery what works for me is to remove both
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
and
android:screenOrientation="portrait"
now my activity is using this theme:
<style name="Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
The only thing that worked for me was changing
android:screenOrientation="portrait"
to
android:screenOrientation="unspecified"
in the manifest for all translucent activities.
That way it is compatible with all API versions since the translucent activity seems to inherit its orientation from the parent activity as of targetApi: 28.
The style can be left as it is including <item name="android:windowIsTranslucent">true</item>.
I resolved this issue by removing android:screenOrientation="portrait" and added below code into my onCreate
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
while my theme properties are
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowDisablePreview">true</item>
I do not know if this is a bug from Google or an intended behavior but I (at least momentarily) solved it by changing compileSdkVersion and targetSdkVersion back to 26 in Gradle...
If you haven't resolved your problem just register the ad activity in the manifest like this:
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
tools:replace="android:theme"
/>
You also need to update to the latest version of the sdk.
this happened after 27,use targetSdkVersion 26 replace, wait for google fixed it
If the activity created by yourself, you can try this in the Activity:
#Override
public void setRequestedOrientation(int requestedOrientation) {
try {
super.setRequestedOrientation(requestedOrientation);
} catch (IllegalStateException e) {
// Only fullscreen activities can request orientation
e.printStackTrace();
}
}
This should be the easiest solution.

Cannot see Options Menu on Android Device

I've got an application written with minSdkVersion="8" and targetSdkVersion="19", because is so simple and I need it to be used on low level android devices.
The problem is when I try to use it in an Android 4+ device. I cannot see the Options menu.
When I use the app in the Emulator, I use the "Menu" button, and I can see it. But not in my tablet or mobile phone (both with 4+ version).
Okay, let's see the code:
In the manifest.xml file I have this (I think the problem is the theme...).
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<activity
android:name="com.clv.app2.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme">
And I have a simple OnCreateOptionsMenu like this in MainActivity... (I have defined only two options).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
Can anyone can help me to see where is the problem with this?
Thank you in advance.
EDIT:
option_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/scan"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/connect"/>
<item
android:id="#+id/discoverable"
android:icon="#android:drawable/ic_menu_mylocation"
android:title="#string/discoverable"/>
EDIT:
Okay. I have been reading other entries in forum, and I see that there are devices that have not a menu key (to show options menu), and from 3+ version it is not required to have it.
When I execute my application in an Android device less than 3 version, I have that physical button, and it works. In an Android device more than 3 version (I have one with 4+ version), that button does not exists, and there are no way to make the options menu visible.
My question is... Do I have to put a button in screen on 3+ devices with no "physical menu button", to use it when I want to see the options menu???
I have found the problem that make the application not work on 4+ devices.
In the onCreate method I have in my MainActivity, I have this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "En onCreate...");
// Preparo texto cabecera de pantalla
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
If I take all that code out, leaving only setContentView it works.
It think the issue is in your activity schema.
First change schema in AndroidManifest to yours:
<activity
android:name="com.clv.app2.MainActivity"
android:label="#string/app_name"
android:theme="#style/MyTheme">
Create styles_mytheme.xml in values folder with lines:
<resources>
<style name="Theme.MyTheme" parent="#android:style/Theme"/>
</resources>
Or just add next line to any styles files:
<style name="Theme.MyTheme" parent="#android:style/Theme"/>
Create folder values-v11 and create file there styles_mytheme.xml:
<resources>
<style name="Theme.MyTheme" parent="#android:style/Theme.Holo"/>
</resources>
So basically your Activity will use default theme for old devices and Holo for devices from v11.
I think its not a problem of theme, but while creating your project just make changes like:
Minimum Required SDK: API8: Android 2.2(Froyo)
Target SDK: API17:Android 4.2(Jelly Bean)
Compile With: API8: Android 2.2(Froyo)
Theme: None
Hope it will work.

ActionBarSherlock top-left icon doesn't navigate up

I am using ActionBarSherlock to implement ActionBar for both Pre/post versions of Android HoneyComb.
My problem is that when I tap top-left icon on Android version 4.0.4, it did not respond.
Here is what I have done till now:
1) In all style folders "values/styles.xml" , "values-v11/styles.xml" & "values-v14/styles.xml"; I have done the following
<style name="ActivityTheme" parent="#style/AppTheme">
<item name="actionBarStyle">#style/ActivityActionBarStyle</item>
<item name="android:actionBarStyle">#style/ActivityActionBarStyle</item>
</style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
<item name="displayOptions">showHome|showTitle|homeAsUp</item>
<item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>
In any application activity (except Home activity since it should not have Up arrow), I have done the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inner);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
.....rest of my code ...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
/*app icon in action bar clicked; go home*/
intent = new Intent(this, MainActivity.class);
/* With this flag, if the activity you're starting already exists in the current task,
* then all activities on top of it are destroyed and it is brought to the front.
* you should usually not create a new instance of the home activity. Otherwise,
* you might end up with a long stack of activities in the current task with multiple
* instances of the home activity.*/
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
In the Manifest file, I make sure that I have applied the respective style to all activities (except main activity since it should not have Up arrow)
<activity
android:name="com.andrid.example.TestActivity"
android:label="#string/app_name"
android:theme="#style/ActivityTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.andrid.example.MainActivity" />
</activity>
So now when I test the application on Pre-HoneyComb version, the Up arrow is never shown which is correct since ABS can't respond at all if icon was tapped to navigate up.
But when I tried the application on Post-HoneyComb version like 4.1 on Emulator, the Up arrow is shown and when I tapped it, it works as expected and navigates up normally.
My problem is that when I tried the application on Android 4.0.4 emulator , the Up arrow is shown as expected but when I tapped it, it do nothing
I have found the fix:
Simply I should avoid set the Icon Home as Up using xml attribute: homeAsUp but instead I should have used setDisplayHomeAsUpEnabled(true) explicitly as follows:
In all style folders "values/styles.xml" , "values-v11/styles.xml" & "values-v14/styles.xml"; I have done the following
<style name="ActivityTheme" parent="#style/AppTheme">
<item name="actionBarStyle">#style/ActivityActionBarStyle</item>
<item name="android:actionBarStyle">#style/ActivityActionBarStyle</item>
</style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
<item name="displayOptions">showHome|showTitle</item>
<item name="android:displayOptions">showHome|showTitle</item>
</style>
in Each Activity (except main activity):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Categories

Resources