I programmatically create a list (no a ListView, just adding them to the parent) of such elements:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_weight="1">
<TextView android:id="#+id/filiale_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/lagerstand_text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="10sp" android:textColor="#color/red"/>
</LinearLayout>
Also, I have defined some colors in values/colors.xml. As you see, the TextView with id "lagerstand_text" has set it's color to red by default. That works.
When creating the elements in Java, I do
lagerstandText.setText("bla");
and for some elements also I do
lagerstandText.setTextColor(R.color.red);
and other colors. While the elements on which I don't call setTextColor() are red, all others are grey, no matter which color I chose (even if it's the same red again).
Why is that?
The documentation is not very verbose about this, but you cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.
Use the following to set color of your text programmatically:
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
Starting with the support library 23 you have to use the following code, because getColor is deprecated:
textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
So, there are many ways to achieve this task.
1.
int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
textview.setTextColor(color);
2.
textView.setTextColor(getResources().getColor(R.color.some_color));
3.
textView.setTextColor(0xffbdbdbd);
4.
textView.setTextColor(Color.parseColor("#bdbdbd"));
5.
textView.setTextColor(Color.argb(a_int, r_int, g_int, b_int));
1.standard color u prefer please go with below .
textview.setTextColor(Color.select_color)
2.here want to use custwom color add it in color.xml file
textview.setTextColor(getResources().getColor(R.color.textbody));
or
textView.setTextColor(Color.parseColor("#000000"));
or
subText.setTextColor(Color.rgb(255,192,0));
For future reference, you can use the follow:
String color = getString(Integer.parseInt(String.valueOf(R.color.my_color)));
my_textView.setTextColor(Color.parseColor(color));
This way you can make use of your Color Resources.
textView.setTextColor(Color.RED);
The integer id for a particular color(defined in xml layout) defined in R class cannot be passed as a parameter to setTextColor() method of View class.
You must obtain the parameter of the setTextColor() by the following line of code :
int para=getResources().getColor(R.color.your_color,null);
view.setTextColor(para,null);
The method getColor(int id) has been depreciated...instead use getColor(int id,Resources.Theme theme) as in the line of code above.
The `second parameter( theme )` can be null
Related
I have a horizontal line created with a view like this:
<View
android:layout_width="fill_parent"
android:id="#+id/led_connection"
android:layout_height="5dip"
android:background="#fff"
android:layout_marginBottom="15dp" />
I want to know how can I change the color programatically. Because I am trying to use setBackground and setBackgroundDrawable but SDK sais me that it cannot applied to a View.
I'm getting the view with this:
View led_connection = (View)v.findViewById(R.id.led_connection);
Try:
led_connection.setBackgroundColor(0xFF00FF00);
See docs for details
If you have html colors, can try this to solve your problem.
led_connection.setBackgroundColor(Color.parseColor("#679456"));
led_connection.setBackgroundColor(Color.parseColor("html_code_colors"));
As Marcin Orlowski sais, the solution is use setBackgroundColor with an hex color with the 2 first digits that are alpha level.
I'm using for GREEN:
led_connection.setBackgroundColor(0xFF008000); // GREEN
and for RED:
led_connection.setBackgroundColor(0xFFFF0000); // RED
Thank you
I need an EditText to appear as a TextView for a form that will change from being write to read only. I have found a useful snippet of code to do this in a layout:
<EditText android:id="#+id/title"
android:layout_width="fill_parent"
style="?android:attr/textViewStyle"
android:background="#null"
android:textColor="#null" />
I need to do this dynamically (not in the layout). I know that the background can be set to #null by using setBackgroundResource(null). Because setTextColor(int color) takes an int, I assume that a specific color must be selected. What is the correct color to choose that would be the equivalent to #null? A color from the default theme? Or is there a better way to do this?
You can create color in your string.xml like
<color name="transparent">#00000000</color>
Then you can assign it to like
txtTitle.setTextColor(getResources().getColor(R.color.transparent));
#00000000 is equal to null.
You can call these methods on any View to disable editing or clicking. It will effectively make the view read only. Set them to true to re-enable them.
view.setFocusable(false);
view.setClickable(false);
view.setLongClickable(false);
I want to customize Buttons for my application. The application has a color picker where the user will select color and I have to set that particular start/end color to the buttons. These colro values will be stored in an object "Utility".
Basically from start only, I want to use "Utility" object to set colors for background, text color, font, etc. And again when the color is changed by the user I got to change it to the buttons and refresh them. And also to save colors in a file, so next time user starts app, it comes up with the last color selected.
I couldn't find <selector> to be the best option, as I wont be able to change the color in xml. What can be the best option for such requirement ?
UPDATIONS :
#jitendra, from your answer I got somethign helpful. I use GradientDrawable to set colors of my buttons. In my onCreate() of the Activity, I call a method RefreshComponents() that sets the background of root, text color/size of buttons and gradient colors of the buttons. It works properly, but the only problem I see is the on applying GradientDrawable to the button the gap between 2 buttons is lost.
This is the image WITHOUT applying GradientDrawable :
On applying GradientDrawable the output is :
You see the size of button is increased a bit from all the sides. If I apply to next button also, they both touch eachother. My xml for the above is :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainroot" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:paddingTop="35dip" android:paddingBottom="35dip"
android:paddingLeft="35dip" android:paddingRight="35dip"android:gravity="center" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainrow1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_marginBottom="15dip" android:gravity="center_horizontal" >
<Button android:text="Accounting" android:id="#+id/accBtn" android:layout_width="80dip" style="#style/TileButtonStyle" />
<Button android:text="Data" android:id="#+id/dataBtn" android:layout_width="80dip" android:layout_height="fill_parent"></Button>
<Button android:text="Information" android:id="#+id/infoBtn" android:layout_width="80dip" android:layout_height="fill_parent" android:ellipsize="end"></Button>
</LinearLayout>
..... Other lineasr layout with same parameters as above child
And the GradientDrawable that I create is :
public static GradientDrawable getButtonDrawable(Button btn) {
int colors[] = {getStartColor(), getEndColor()};
GradientDrawable grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
grad.setCornerRadius(5f);
return grad;
}
And finally in my onCreate(), I add :
GradientDrawable btnGradient = Utility.getButtonDrawable(btn1);
btn1.setBackgroundDrawable(btnGradient);
What is going wrong here ? Is the margin around the button becoming 0 ? Do I have to set bounds for the grad, or again set LayoutParams for the button ?
Any help is appreciative to help me achieve my goal.
Thanks
You can Create StateListDrawable Object dynamically in java file and set as background and sources of applcation components.
Android has Themes and Styles, but they are a development-time feature and can't be manipulated at runtime. However, different predefined themes can be applied at runtime.
So you can have a set of predefined themes, with fixed view properties (colors, fonts, etc..) and give user an option to choose a theme at runtime.
But, if you need to change every particular view property, then you will need to roll your own "theme" system. Which means you will need to have properties stored somewhere and applied each time a view is built.
Tvd! I incline to agree with Peter Knego and Jitender Sharma. Furthermore, I think/believe you can setOnClickListener on those buttons of yours and perform your color changing stuff with the code that rests inside the setOnClickListenermethod assigned to every button. In addition to this, you'll have to configure your color.xml file and custom themes. There's a lot of work to be done. I'm not really sure which is the best way though. I strongly suggest you to go through the android learning stuff I'd provided to you in my previous answer. Only they can give you a detailed insight and a solid idea to go ahead. All the best!
Oh I got the solution :
I added layout_marginRight attribute to buttons and that did the work.
Though I am still concerned, without GradientDrawable the buttons had margin betweenthem then after applying GradientDrawable why is the default margin lost ? Why is the need of additional layout_marginRight to be added ?
If anyone yet has answer for this, please let me know.
Thanks
I would like to set a red colour text in my app, but I don't know how.
Please provide references. Thanks.
If you want it to set in XML layout then use:
<TextView
...
...
android:textColor="#FF0000" >
</TextView>
If you want to set programmatically then use:
textview.setTextColor(Color.RED);
//textview must be defined in your class
Read the docs for TextView, specifically for textColor.
Use the following in your xml where you want to change the color to red and by using the other hexacode of color you can change to any other color. This is an example to set the color to red:
android:textColor="#FF0000"
Use this in your layout:
<TextView>
....
textColor="red"
....
</TextView>
Create a custom style resource which uses an Android Theme as a parent then override the text colors as defined in the Android theme.xml. Reference this new style in your AndroidManifest.xml's application tag.
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.