3

my application has two language.right to left and left to right. i change locale inside of application(not from phone setting). and reload data on resume() function to see changes on UI. i have two folders for values.one values-en, one values-fa. to change direction of textview to RTL and LTR,put two styles.xml in these folders. styles.xml (values-en) :

<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Direction">
    <item name="android:gravity">left</item>
    <item name="android:layout_gravity">left</item>
</style>
</resources>

styles.xml (values-fa) :

<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Direction">
    <item name="android:gravity">right</item>
    <item name="android:layout_gravity">right</item>
</style>
</resources>

and to use this style,for example i do it:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/menutext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/Direction"
android:padding="0dp"/>
</RelativeLayout>

if i run application,everything is ok,and direction of textview is related to locale.problem is here that when i change language from app's setting from option menu,data load correct,but direction not.and i'm should go to another activity to see changes of direction.

to solve this problem,can i remove styles.xml because of i'm using Html.fromHtml() and my data have

<p dir="LTR">

and

<p dir="RTL">

tags?

another probable solution(thanks to TOMCA for comment) : android dynamically change style at runtime

but problem still alive.anybody have a idea?

Community
  • 1
  • 1
CooL i3oY
  • 790
  • 1
  • 9
  • 26
  • 1
    http://stackoverflow.com/questions/3241729/android-dynamically-change-style-at-runtime – TOMKA Oct 04 '12 at 16:30
  • @TOMKA tnx for good link,but problem is alive.solution on that link not working for resume to activity.(i don't leaves activity and go back to it that call onCreate ) – CooL i3oY Oct 06 '12 at 07:33
  • 1
    I think it's because styles aro loaded only when the content view is set. Maybe just try to recall `setContentView()` after changing language? It's pretty ugly because you have to reset all your `findViewById()` but I think it'll work. – Kyriog Oct 06 '12 at 08:01
  • @Kyriog yes,i did it,and it's worked.but thanks again for your attention and comment.and now i'm going to post answer for others :) – CooL i3oY Oct 06 '12 at 09:09

1 Answers1

2

do what's explain here: android dynamically change style at runtime

and for resume problem: just try to recall setContentView() in onResume like this:

@Override
 protected void onResume() {
     if(selectedLang.equalsIgnoreCase("fa"))
    context.setTheme(R.style.CustomTheme_DirectionRight);
     else
    context.setTheme(R.style.CustomTheme_DirectionLeft);
     super.onResume();
     setContentView(R.layout.content);
     findAgainViews();//because recalling setContentView reset all findViewById()
 }
Community
  • 1
  • 1
CooL i3oY
  • 790
  • 1
  • 9
  • 26