Change language in Android App - android

I want to change the language in my app programatically.
The first onCreate(Bundle) method works and the images are displayed in chinese.
The second doesnt work. What do I have to insert in the "TODO" comment? I want to change the language AFTER the view was created and want to update it.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TranslationHelper.changeLanguage(this, Locale.CHINESE);
setContentView(R.layout.main_activity);
doBindService();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
TranslationHelper.changeLanguage(this, Locale.CHINESE);
// TODO recreate view to display chinese version
doBindService();
}

The first sample works because it changes locale before setContentView method is called. You need to insert changeLanguage before setContentView.
All views are already inflated after setContentView so changing locale at this point will have no effect. You need to update it manually if you want to change the language after the view was created.

Related

Setting Background Colour to PreferenceActivity

I am having a confusion regarding setting of a background colour in my preference activity. My settingsactivity doen't have any layout.xml attached with it and thus generated everything from the headers.xml and preferences.xml. I used this answer in my onCreate method of my settingsactivity class.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
this.getWindow().getDecorView().setBackgroundColor(0xFF111321);
}
Using this I can see the change in my preferences (sorry for the colour) but my headers headers stay the same. To change the background of my headers I have to write:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
this.getWindow().getDecorView().setBackgroundColor(0xFF111321);
listView = getListView();
listView.setBackgroundColor(0xFF111321);
}
Also if I don't use getWindow() the opposite happens.
Why is this happening?
I hate writing duplicate code for the same functionality, is there any way to do the requirement with a single code.
N.B. getWindow() also works with my other activities.

Android custom reusable controls?

I have a few activities in an app I'm writing that I want to put a form on the page. It's the same form for all the activities that does the same except for different title and text.
Instead of rewriting the controls and the logic for each activities I'm looking for a way to create my own control that I can put in my layout xml files with my own properties so all I'll need to do is write it once and use that control where I need to.
how can I do such a thing?
Thanks
What about writing your control as an android Fragment? In the xml you can declare a FrameLayout and then insert the fragment inside it using the replace() method of FragmentTransaction.
You can use an Activity which extends android.app.Activity and define it the common logic.Then all your other activities will extend it in place of extending android.app.Activity. Then they will inherit it , and you can override what you want to Override as well.
This solution is not bad if you have exactly the same layout and just some differences, you can load the layout.xml only once and then use it in the child class as you like.
SuperClass :
public SuperClass extends Activity{
protected TextView myTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView) findViewById(R.id.my_text_view);
}
}
ChildClassA :
public ChildClassA extends SuperClass {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView.setText("I am in A");
}
}
ChildClassB :
public ChildClassB extends SuperClass {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView.setText("I am in B");
}
}
Other suggestion:
Another idea is to create a custom view. see this tutorial also.
Depends on what differences you have in each Activity and how you access them.
If it's only some strings, that are different, put those strings inside the Intent, which starts the Activities and grab them in onCreate().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lyt_template_form);
((TextView) findViewById(R.id.txt_form_headline)).setText(getIntent().getStringExtra("headline");
((TextView) findViewById(R.id.txt_form_subtitle1)).setText(getIntent().getStringExtra("subtitle1");
//etc...
If there are more differences in your control code and you need to distinguish some kind of "Form Type A", "Form Type B", etc., you can put an indicator inside your Intent and check for that to make different decisions in your code.
So e.g.
if (getArguments().getInt("Type") == 0) {
// do stuff in control like this
} else if (getArguments().getInt("Type") == 1) {
// do stuff in control like this
// etc.
getArgument() is the equivalent of getIntent().getXyzExtra()

findViewById() returns null

There are a few questions already asking about this, but I'm not finding a solution. I have a LiveWallpaper that is using its own subclass of PreferenceFragment to specify preferences. The solution most often cited is to assure that setContentView() is called before findViewByID(). I am not calling setContentView() at all because I do not have a layout specified. This app originally implemented the preferences using the deprecated methods like PreferenceActivity.getPreferenceManager() and without using a layout and worked just fine. I am trying to bring the code up-to-date in using PreferenceFragment.
Am I required to have a layout and if so, what would I have when I don't really want one?
Or is there a another way to get/set the View?
public class SetPreferenceActivity extends Activity {
private CheckBox redCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main); ???
redCheckBox = (CheckBox)findViewById(R.id.redCheckBox); // returns null
getFragmentManager().beginTransaction().replace(android.R.id.content, new LiveWallpaperPreferenceFragment()).commit();
}
}
You can create views dynamically :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Hello");
setContentView(textView);//<----set the view !
}
}

Can we reuse a layout xml on different fragments on Android?

I have a pager with 3 pages where I just set a background image. Instead of creating page1.xml, page2.xml and page3.xml. Can I create just page.xml and set the background by code? Is that a good solution or is bad for android to load images on real time?
sure, you can set the background image in your onCreate() methode:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
findViewById(R.id.my_root).setBackgroundResource(R.drawable.my_image);
or you can use a theme if you like that more
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.my_theme);
setContentView(R.layout.my_layout);
and then set the windowBackground in your custom theme.

Android Development: Text contain

i have a editText, so how do i check if the editText contain like "http://www."?
Is this possible to see if a component contain a specific string?
Thank-you
PS. is it also possible to change android:theme(like no title) at runtime?
IIRC the titlebar needs to be removed in the Activity onCreate before you call setContentView.
I use
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
As for the text search use
String.contains("http://www");
For Example
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(tag, "onCreate");
main = this;
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

Categories

Resources