how to change the xml background in android dynamically? - android

I am need a logic to change the xml background programatically instead of giving the backgroung color from xml file. here i used setContentView(R.layout.main); to show the xml file in application. But i need to change the background color of main.xml file in java code.

Following is the example that shows how can you make it. Here i have taken LinearLayout as the root node of xml file. In java code i have shown that how to set white background. I think this code will help you.
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
-----
-----
</LinearLayout>
java code
LinearLayout root=(LinearLayout)findViewById(R.id.root);
root.setBackgroundColor(Color.WHITE);
It can also possible to set color using Hex code as shown below,
root.setBackgroundColor(Color.parseColor("#ffffff"));
Even you can set drawable also,
root.setBackgroundResource(R.drawable.bg_image);

Here is how you can do that,
mLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
mLayout.setBackgroundColor(Color.BLUE);
Here mLayout is your parent RelativeLayout.

get the instance of the root layout of your xml and set it's background color from your code as you wish as many times you want but do all this in your UI thread..

GradientDrawable sd = (GradientDrawable)this.getResources().getDrawable(R.drawable.roundbordertry);
sd.setColor(Color.rgb(5, 5,5)); newHeadView.setBackgroundDrawable(sd);
//////////////////////////////////////////////////////////
roundbordertry is the xml file.
newHeadView is a linearlayout.

Related

Change background drawable for all layouts

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.

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.

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.

Add style to ImageButton dynamicaly

I have added a imagebutton dynamically to my Tablerow, now i want to define the style for the image button to a style that i have previously defined. I have tried the setTheme method that doesnot seem to work
Thanks for any help
k after a lot of searching this is what i found and it works perfectly.
1) if you want to create a button/imagebutton/anyView at runtime with a predefined style the you can do so just follow the steps below
2a) Create a new xml file in res/layout and put that one element you want to create for example I wanted to create a minusButton. This is minusb.xml in res/layout
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="#style/MinusButton"/>
2b) Then add the following lines to your java activity code where you would like to create it
ImageButton bt = (ImageButton) getLayoutInflater().inflate(R.layout.minusb, null);
bt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0f));
tr.addView(bt);

Categories

Resources