Hy,
I am using the next background colour #android:color/transparent for a ImageButton in the layout.xml. Could or should I externalize it in the colors.xml resources file?
Thanks
You could but it depends on your needs.
If you use that color in many different parts of your application it's best to place it in an external xml file. Sometimes you need to change the style of the whole application and it's easier to change the definition of that color instead of changing the color in many different places. If you're only using it in one single place then it is OK to leave it out of the external file.
EDIT:
<color name="my_background_color">#android:color/transparent</color>
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.
I have an XML file with 5 textviews, all with the same textSize. Currently, I define the size like this:
android:textSize="30dp"
However, I want a quick way to apply something like:
android:textSize="text_size"
Where textSize would be a variable I define somewhere, and can use wherever I want in my XML file. I tried defining these variables in the strings.xml file in the Android project, and calling them like this:
android:textSize="#string/text_size"
Where it would be defined like this in strings.xml
<string name="text_size">30dp</string>
It worked fine in Ecplise itself, and showed just fine in the Graphic Layout. But when I tried running the app on my phone, it crashed.
So, is there a way to define variables in my XML layout files? Thanks.
Your idea of putting it in strings.xml is close, but those values need to be set in a dimensions XML file. The documentation is here.
Using your example, you might make a file called res/values/my_dimens.xml. Then put in there
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_text_size">30dp</dimen>
</resources>
Then to use it in your layout, just set
android:textSize="#dimen/my_text_size"
Often i like default android style defined for some widget.
Often i like to just slightly modify this default style, not create my own style from a scratch.
Now, i use android.R.layout.simple_spinner_dropdown_item as dropDownViewResource on my spinner adapter - like this:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I can open this layout xml file and copy its content to modify it. But the most important part of that xml file is attribute style, it looks like this: style="?android:attr/spinnerDropDownItemStyle"
Now, my question is: how do i find/view/copy this style spinnerDropDownItemStyle, so i can for example just change background color.
Thanx for your help
here you can find style spinnerDropDownItemStyle:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
But, if you looking for change background of Spinner, you'll need to search for 9 patch images:
https://developer.android.com/studio/write/draw9patch.html
And here a good example:
http://www.gersic.com/blog.php?id=57
Ty
My suggestion to you is . you can make a seprate custom layout and put it on your spinner setDropDownResource()
adapter.setDropDownViewResource(android.R.layout.CUSTOM_LAYOUT);
Now you can easily make a style for your layout
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.