Android - Wrong layout direction when switching RTL vs LTR many times - android

When I switch my app language between EN and AR (during runtime) the views behave correctly by moving from the LTR to the RTL but when I start stressing the app by switching languages many times the layout direction become missy and I get RTL elements when the app is supposed to show LTR elements. Sometimes the layout direction is not refreshed anymore.
As workaround I have to set the layout direction programmatically for those views. binding.spinner.setLayoutDirection(LocaleUtil.isRtl(this) ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
Do you have any idea please ? I am trying to avoid this kind of workaround :( If it is possible to rely entirely on the Android UI rendering.

I can't imagine a user who will switch language many times and then returning to app on every change :)
But, anyway, if Android is not able to recreate your activities correctly you always can do it manually by setting "android:configChanges" in AndroidManifest.xml for your activities and then listening for onConfigurationChanged(Configuration newConfig) method in such activities:
In AndroidManifest.xml:
<activity android:name=".MyActivity"
android:configChanges="layoutDirection">
In Activity for which you set android:configChanges:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
// change directions for you views to RTL
} else {
// change directions for you views to LTR
}
}

make sure that supportsRtl="true"
<application
android:supportsRtl="true"
android:name=".App"
android:icon="#drawable/news"
android:label="#string/app_name"
android:theme="#style/AppTheme">
</application>

Related

Why does setting LayoutDirection on a LinearLayout not take effect immediately?

I am working on an application, using API 17 (4.2). In the application I am designing a layout class, and I would like to configure it according to the layout direction. However, I haven't been successful in retrieving the applied layout direction within the class:
LinearLayout layout = newLayout(context);
layout.setLayoutDirection(LAYOUT_DIRECTION_RTL);
int ld = layout.getLayoutDirection(); // STILL 0! I was expecting 1
My question is, how do I configure a layouts direction, and retrieve it within the class?
add this to your AndroidManifest.xml:
<application
...
android:supportsRtl="true"
>
As View checks RTL support first, if true, then resolve layout direction.
You can get more details in View.resolveLayoutDirection().

How to fix screen orientation on android for different devices

I want to disable changing orientation in my android app, but I want to allow user use his native orientation: landscape for tablets and portrait for phones.
I try to set "android:screenOrientation" with different values, but I can't find appropriate value.
Is such task needs to write java code (and what code I need to write), or this can be solved using manifest?
UPD As in #Pierrew's hint, I need to create res/layout/main.xml for default layout, res/layout-land/main.xml for landscape layout and res/layout-port/main.xml for portrait layout. So, I need to know, how to defined all layout in res/layout/main.xml and only clarify orientation in res/layout-land/main.xml and res/layout-port/main.xml.
I think I need to declare layout in res/layout/main.xml, and derive all layout properties in res/layout-land/main.xml and res/layout-port/main.xml and override only android:screenOrientation value in derived layout.
How I can do it?
setting android:screenOrientation="landscape" in the layout xml is the way to do it. To support different devices (tablet and phones), you need to use Size-Qualifiers. In your res folder, you will have to create a res/layout-large/main.xml to support tablet. So if you want landscape for tablets. In your res/layout-large/main.xml, you put android:screenOrientation="landscape". In your normal res/layout/main.xml, you can put android:screenOrientation="portrait". Android will automatically detect what device the user is using and load the appropriate xml. For further information, check out the size qualifier section in http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSizeQuali
You can use a little logic using these methods.
Boolean isTablet = (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
Change Screen Orientation programatically.
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityInfo:
http://developer.android.com/reference/android/content/pm/ActivityInfo.html
This should do the trick for you:
import android.provider.Settings;
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled){
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
Add permission to the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Android Screen orientation landscape error?

I'm new to android and making an app in which there is layout design for both portrait and landscape mode. The app is running fine in both screen orientation except for one activity. The activity is working fine in portrait mode when i go from one activity to another, but crashes in landscape mode. I tried to solve this in different ways through Google search but didn't succeed. Please someone help me. Thanks
Use this android:configChanges="orientation" inside your activity tag in AndroidManifest.xml hope this will help
Use this in Manifest.
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="VncCanvasActivity">
This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden"
Make two different resource folders in res folder like below:
1) layout --> put your main.xml
2) layout-land --> put your same main.xml in here too.
Note: in both res folders the name of layouts must be same.
Edit: well android handles orientation change automatically after above mentioned
procedure.. but if you want to handle it manually then here is the code to handle..
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
Do something in Portrait
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Do something in Landscape
}
}
Add this below line in your manifest to the activity you want to handle orientation:
android:configChanges="orientation|screenSize|keyboardHidden"

Button background color is changed by portrait to landscape mode

I am new to Android Application development. In my application if I click on button,it changes it background color from black to green.
If I change the mode from portrait to landscape then it changes the color from green to black with out click on button.
My requirement is,have to stop background color change,by changing the mode from portrait to landscape or landscape to portrait.
Please help me to go forward.
When screen orientation changes, the method onCreate() is called.
My guess is you are calling Button.setBackgroundColor() in this onCreate() method...
Try surrounding this call with if(savedInstanceBundle == null){}, like that it will only set the color to black on the first onCreate() call, and not for any subsequent calls.
Use android:configChanges="orientation" in your activity declaration in manifest. Like this
<activity android:name=".MyActivity"
android:configChanges="orientation"
android:label="#string/app_name">
When you rotate device then onCreate() method calls again to avoid this use below code
android:configChanges="orientation"
in the Manifest.xml for activity
<activity
android:name="com.sample.stackoverflow.MainActivity"
android:configChanges="orientation"
android:label="#string/app_name" />
If you want to load specific layout when orientation change :
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// use your setcontentView here
}

Android Landscape/Portrait changing difficults

Target:
If I'm switching ffrom a Portrait to a Landscape the complete layout should change (same buttons, same textfields, but different positions). Until now the problem is that they lose the values.
I've set
<application ...
...android:configChanges="orientation|keyboardHidden" ...>
2 different Layouts in:
+Layout
+-main.xml
+-second.xml
+Layout-land
+-second.xml
ex: I switch from a portrait to a landscape, I want to save all the portrait values (buttons, textfields) and restore them in the landscape view.
Now I'm missing some input for the method in MyActivity()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// how should i handle this?
}
I've found hints like setContentView(...) and "handle this Problem in the Manifest file". Maybe my approach to this is completely false. I would appreciate any hints!
Just let android handle the orientation changes?
You got the xml-files right: Two different layouts with the same name in the two folders layout/ and layout-land/
Just don't set
<application ...
...android:configChanges="orientation|keyboardHidden" ...>
and Android will handle everything for you :)
Cheers
Ali3n

Categories

Resources