Change background drawable for all layouts - android

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.

Related

Fill color of vector drawables that are icons in dialogs or toolbars

What if I would want to use vector drawables as icons in dialogs or logos in toolbars? Then I would use them like this:
alertDialogBuilder.setIcon(R.drawable.my_vector_drawable);
toolbar.setLogo(R.drawable.my_vector_drawable);
toolbar.setNavigationIcon(R.drawable.my_vector_drawable);
...
Am I right?
But here comes up a question: how can I change their fill color without modifying vector xml files?
For instatnce I can change fill color of any vector that is in view by using 'tint' tag in xml or 'setColorFilter()' method in code.
If you want change fill color not change xml file you must create Drawable instances of this file. This instance get you method to change your file.
for example:
Drawable myIcon = ContextCompat.getDrawable(this,R.drawable.my_vector_drawable);
myIcon.setColorFilter(ContextCompat.getColor(this, R.color.yourcolor));
alertDialogBuilder.setIcon(myIcon);
toolbar.setLogo(myIcon);
toolbar.setNavigationIcon(myIcon);

Android : Setting textview background in 'style' in XML

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"

Is there any way to change the background color activity through java file in android?

I try to change the background color activity through java file but it does not work
so,
Is there are any way to change the background color through java file (Not throw XML file) ?
You can change the background of Activity from code using a Drawable
Set a Drawable from Resource like this
getWindow().setBackgroundDrawableResource(R.drawable.your_bg);
Or set a Color like this
getWindow().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
Yes, in code you can do this:
LinearLayout ll = (LinearLayout)findViewById(R.layout.blahblah);
ll.setBackgroundColor(R.color.FireBrick);
The java code above is equivalent to placing this xml attribute inside your layout:
android:background="#color/FireBrick"
To make it easier on you, I recommend you placing this color.xml file inside your res/values/ folder. The FireBrick color is defined inside that particular file.

android change view's background color when state changes

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.

Setting background colour of Android layout element

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.
I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?
Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
You can use simple color resources, specified usually inside res/values/colors.xml.
<color name="red">#ffff0000</color>
and use this via android:background="#color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="#drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml
<RelativeLayout ...
...
android:id="#+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="#color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="#android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
4 possible ways, use one you need.
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. Data Binding
<LinearLayout
android:background="#{#color/white}"
OR more useful statement-
<LinearLayout
android:background="#{model.colorResId}"
3. XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="#color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:
I got this by adding the following to my layout:
android:background="#FFFFFF"
Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.
Kotlin
linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
or
<color name="newColor">#f44336</color>
-
linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.
In foo.xml set an id for the entire layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/foo" .../>
And in activity_bar.java set the color in the onCreate():
public class activity_bar extends AppCompatActivty {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
//Set an id to the layout
RelativeLayout currentLayout =
(RelativeLayout) findViewById(R.id.foo);
currentLayout.setBackgroundColor(Color.RED);
...
}
...
}
I hope this helps.

Categories

Resources