Can we reuse a layout xml on different fragments on Android? - 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.

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 !
}
}

Change language in Android App

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.

How to setTheme to an activity at runtime? It doesn't work call setTheme before onCreate and setContentView

I want setTheme to an activity at runtime, I have search some solutions by google.
someone said call setTheme before onCreate and setContentView can works, the code section like
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
super.onCreate(savedInstanceState);
...
setContentView(...)
}
but it not works, I want to know, is there another solution can setTheme to activity?
Just try this - set your theme after super.onCreate(savedInstanceState); and before setContentView(...)
Like below code -
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent_NoTitleBar); // Set here
setContentView(...)
}
Actually this only worked for me if I set it before calling super.onCreate(savedInstanceState);
public void onCreate(Bundle savedInstanceState)
{
final int themeRes = getIntent().getIntExtra(EXTRA_THEME_ID, 0);
if (themeRes != 0) setTheme(themeRes);
super.onCreate(savedInstanceState);
//ect...
}
setContentView(...);
setTheme(R.style.MyTheme);
setContentView(...);
It must work well..
More on Themes, Read this
http://entertheinfinity.blogspot.in/2016/06/designing-android-interface-themes.html
To set the theme at runtime and fix the "black background" problem:
the theme should be set before onCreate().
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
super.onCreate(savedInstanceState);
...
setContentView(...)
}
the theme of the transparent activity in the android manifest should be set to any theme that has a transparent background (e.g. a dialog theme).
this tells the Android OS continue to draw the activities behind the transparent activity so that you don't end up with a black background.
i'm using an AppCompatActivity; i need to use an AppCompat theme:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
...
<application
...>
...
<activity
android:name=".TranslucentActivity"
android:theme="#style/Theme.AppCompat.DayNight.Dialog"
.../>
...
</application>
</manifest>

Categories

Resources