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.
Related
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()
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 !
}
}
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.
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.
I am using a custom title bar for my all activities but i can use this in PreferenceActivity.
All i can do in PreferenceActivity is this:
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences3);
My title bar in preference activity stay always grey without any text but other activities works very well this code. What i can do to solve my problem??
put getWindow().... after super and addPreferencesFromResource... so order should be:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences3);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
p.s. credits to original author #jeffrey-blattman following my previous answer here..