Android TextView - android

I have an example in android that I am trying to run.
There were 2 ways to do it..
// Get a drawable
ColorDrawble redDrawable = (ColorDrawable).getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackground(redDrawable);
When I put this in the Eclipse IDE I get an error ColorDrawble cannot be resolved to a type
I have the textview in the main XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:id="#+id/texter"
android:layout_height="wrap_content"
android:text="this string"/>
</LinearLayout>
and the resources in the strings xml file
<resources>
<string name="hello">Hello World, ResourceTesterActivity!</string>
<string name="app_name">ResourceTester</string>
<drawable name="red_rectangle" >#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
<drawable name="green_rectangle" >#f0f0</drawable>
</resources>
**

you can simply use a drawable like this :
// Get a drawable
Drawable redDrawable = YourActivity.this.getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackgroundDrawable(redDrawable);//i've changed setBackground with setBackgroundDrawable.
or you can directly use :
textView.setBackgroundResources(R.drawable.red_rectangle);
NOTE : clean and rebuild your project , and run it to test
and for the drawable, you dont need to declare it in strings.xml , just add your drawable on the folder drawables , and it will work

It should be:
ColorDrawble redDrawable = (ColorDrawable)
getResources().getDrawable(R.drawable.red_rectangle);
You don't need the dot (perhaps it's a typo).
Then press Ctrl+Shift+O (Organize Imports) to import ColorDrawble class.

Related

Unable to change background color in android xml file

Whenever am trying to change the background color by using
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/SomeColor">
Am getting the error:
Failed to parse file E:\ADT\sdk\platforms\android-18\data\res\color\SomeColor.xml
I have cleared the project and even restarted it several times but I have been unsuccessful in getting rid of the error.
Try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/SomeColor">
EDIT
And make sure that you set your color properly. You should set in strings.xml.
<color name="SomeColor">#ffff0000</color>
Or you can set
android:background="#dcdcdc" // give the appropriate color code here.
Hope this helps .. :)
now u fetch xml file itself dont do this dude just try to fetch color name itself in that xml like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="graycolor">#DCDCDC</color>
<color name="graycolor_press">#8B8B8B</color>
</resources>
Then fetch like this
<item android:drawable="#color/graycolor"/>

No resource found when using '#string/value' in layout.xml

just started programming android and I am trying to make a simple xml file but it seems it got some problem with the android:text=#string/..
I have got this error:
error: Error: No resource found that matches the given name (at 'text'
with value '#string/ false_button').
For each of the strings..
The code is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding= "24dp"
android:text="#string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
</LinearLayout>
You need to have the resources that you want to reference in your layout in strings.xml under res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True Button</string>
<string name="false_button">False Button</string>
</resources>
#string/true_button refers to a resource in strings.xml
You can also have hardcoded string but not recommended
android:text="True Button"/>
add following to your string.xml file under values folder
<string name="true_button">true</string>
<string name="false_button">false</string>
Make sure that you are created string value with the name of false_button in the string.xml
<string name="false_button">false</string>
or just give name within the quotes "" in the android:text attribute
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false_button"/>
Click on res folder then click on values and then open strings.xml and add following content.
<string name="true_button">whatever text you want to write to your button</string>
<string name="false_button">whatever text you want to write to your button</string>
and save your file. Then clean your project by clicking Project -> clean and then run it again.
Hope this will help you.
make sure that in res/values/
you have all the resources present there as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True</string>
<string name="false_button">False</string>
</resources>
otherwisely you can hardcode values at yours layout where you specified button as
android:text="True "/> or android:text="False"/>
In your android project, look for the res directory. Open it and inside it look for values directory. In values, open strings.xml and check if it has a flase_button value stored in it . If not you can add a new value using name as false_button and value as what you want to display on the button.
When you open the strings.xml in IDE like eclipse, you can switch views of the .xml file by selecting proper tab given at the bottom of your eclipse screen. Select strings.xml view and
modify the current strings.xml to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app</string>
<string name="false_button">FALSE</string>
</resources>
Hope this was helpful.
You just need to add this in your Strings.xml class
just add <string name="false_button">False Button</string> line in your strings.xml
so that it will look like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="false_button">False Button</string>
</resources>
All of these responses that refer to adding the string to the string.xml file make sense and work, but fail to address the original problem: this book ("Big Nerd Ranch: Android Programming") goes through these steps one at a time, mentions referencing the string in the layout XML file, but fails to mention adding to the strings.xml file. Fail.

The colors.xml file does not work in eclipse

iam trying to run the colors.xml file under res/values for the hello world app.
i've put the following code in the .xml:
<resources>
<color name="background_color">#00F</color>
<color name="app_text_color">#FF00FF</color>
</resources>
however the string displayed does not have any of the background color or the app text color when i run the app.
Do I have to write the code programmatically also.
Yes you need to apply the colors to your views explicitly or create a custom theme if you want to change these settings for your whole app.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/app_text_color"
android:text="hallo" />

setting textColor in TextView in layout/main.xml main layout file not referencing colors.xml file. (It wants a #RRGGBB instead of #color/text_color)

I'm trying to set some general colors for a program I'm writing. I created a colors.xml file and am trying to directly reference the colors from the layout.xml file. I believe I'm am doing this correctly however it's giving me the following error:
Color value '#colors/text_color' must start with #
Here is my res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#888888</color>
<color name="text_color">#00FFFF</color>
</resources>
Here is my res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:text="#string/hello"
android:layout_height="wrap_content"
android:id="#+id/TextView01"
android:textColor="#colors/text_color"/>
</LinearLayout>
I looked at some references on the android developers site: More Resource Types : Color and found this code:
Example:XML file saved at res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
This application code retrieves the color resource:
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
This layout XML applies the color to an attribute:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/translucent_red"
android:text="Hello"/>
I think my two xml files follow this example pretty close - however the only difference is that I haven't used any application code to retrieve the color resource. I don't believe this is necessary (but it is a difference.) I thought I'd see if anyone else had similar problems or a solution? or is this a bug?
I did update all my android sdk (and Eclipse plugin) files last week so I believe them to be the latest.
A variation using just standard color code:
android:textColor="#ff0000"
After experimenting on that case:
android:textColor="#colors/text_color" is wrong since #color is not filename dependant. You can name your resource file foobar.xml, it doesn't matter but if you have defined some colors in it you can access them using #color/some_color.
Update:
file location:
res/values/colors.xml
The filename is arbitrary. The element's name will be used as the resource ID. (Source)
You have a typo in your xml; it should be:
android:textColor="#color/text_color"
that's "#color" without the 's'.
You should write textcolor in xml as
android:textColor="#color/text_color"
or
android:textColor="#FFFFFF"

MonoDroid - setting background color from colors.xml throws InvalidOperationException

I have a simple layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_red"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
My colors.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
When I compile in VS2010 I get an error "System.InvalidOperationException: Command failed...." I have tried to place the colors.xml in res/values and res/color but I still
get the error. If I reference the color directly like:
android:background="#80ff0000"
everything works fine.
Does anyone know if the colors.xml is supported by MonoDroid and if so why I'm getting this error?
Thanks for the help!
Good you found this out. Another issue point to take into account is that you always define the color id's with lowercase letters. I used capital casing and although my c# code finds the resources with case sensitive searching, android expects lowercase id's when referring from drawable or layout xml files (using #color/the_id_here).
Figured it out, the Build Action for colors.xml was set to Content instead of AndroidResource !

Categories

Resources