I put an image for all activity in my application (in style.xml ...). how I can change the background for all my activity dynamically. is it possible to change the properties of a theme dynamically?
thank you for your help
You can change window's background in your activity using
getWindow().setBackgroundDrawable(...);
getWindow().setBackgroundDrawableResource(...);
See Window docs.
Hi You can set the Background on each Activity XML Layout..
android:background="#drawable/bg"
Related
Is there a way to change the background image of all screen layouts?
Yes I understand you can do it for each one individually by using
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.score_view);
linearLayout.setBackground(getResources().getDrawable(R.drawable.blue));
But is there a way to change the drawable for all .xml layout files like maybe changing the drawable name? Or creating a custom class?
You can use <item name="android:windowBackground">#[Your background]</item> in your application theme to change complete backgroud of you application.
Currently, am setting the background of a TextView using code like this:
textView.setBackgroundResource(R.drawable.rect_with_border_grey);
I then came to know about dimens.xml usage and how you can set through that file. Is it possible to set the background through ? i.e. I want to do the above code line through XML. Any help please?
I wouldn't do this using layout xml file as #Opiatefuchs pointed out (in the comment below). B'cos, this textview's background will change depending on the user setting in the App dynamically.
Make a style like
<style name="MyTextStyle">
Do everything you want to do with your TextView here.
</style>
and then assign that style to your textview in xml like this
style="#style/MyTextStyle"
It will work.
dimen.xml is only use for giving dimension only. for setting background you need to do it with either layout xml file or from java.
Its up-to the requirement of setting background.
I finally found that you cannot set the background of a TextView via element (i.e. XML) in Android. You need to use layout XML. But, in my case, I cannot use layout since the background will change dynamically based on user choice in the App.
create dimens.xml file like this:
<resources>
<dimen name="paddingTop">10dp</dimen>
<dimen name="paddingRight">20dp</dimen>
...
</resources>
then use it in your layout xml files like this:
android:layout_marginTop="#dimen/paddingTop"
android:layout_marginTop="#dimen/paddingRight"
how to use color state list for background?
I know android:background="#drawable/drawable_selector", but android:background="#color/color_selector" will cause exceptions.
but android:background="#FFFFFF" works again, can anyone explains why?
now i want to change a layout's background color(not a drawable) when it's pressed,
how to do it?
dynamically you can change like this. Use this if it is useful for you -
textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));
put the color in res/values/colors.xml,
like #FFFFFF,
and then create a drawable xml in drawable directory,
,that is ok.
I'm developing a library where I have to set the window background programmatically and I can't use custom style XMLs. The idea is that the user who implements the library can set any theme he want - I just need to make the background transparent. So all the styles for the widgets will stay the same.
I tried some window flags like WindowManager.LayoutParams.FLAG_DIM_BEHIND and WindowManager.LayoutParams.FLAG_BLUR_BEHIND, but none of them was working.
Every solution that I've found is based on the style xml file.
Is there a way to set windowIsTranslucent directly from the code?
Thanks in advance, Roman
No, this is not possible.
It is only possible to set windowIsTranslucent in your theme.
Suggestion; Create your own theme that others can override.
android:windowIsTranslucent, android:windowIsFloating, and android:windowNoDisplay are all read exactly once, before your activity has even started. So by the time you're running your own code, it's too late.
http://osxr.org:8080/android/source/frameworks/base/services/java/com/android/server/am/ActivityRecord.java#0399
I used this
#Override
protected void onCreate(Bundle savedInstanceState) {
processSetTheme(this);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawableResource(R.color.realTranslucent);
if (Build.VERSION.SDK_INT>=19){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
super.onCreate(savedInstanceState);
<color name="realTranslucent">#00000000</color>
Here is trick:
First you use Translucent activity.
then you can set background of activity's layout file top layout.
I need to use perticular image as the theme for my most of the activities in my application...
please help me how to do this..
Basic idea is to define and apply the custom style of your Activities in Res files. Tutorial here.
if your using XML display there is a field background
#drawable/custom_dialog_background
or
android:background in the XML anchors
setBackgroundResource(int) in your code
http://developer.android.com/reference/android/view/View.html#attr_android:background