Adding an image to a string with Android SDK - android

I am a super newbie to Android Development and wanted to start slow with an Icon Theme for 3rd party launchers. I was wondering if there is any way to add an image (from inside my "drawable" folder) to a string inside "strings.xml"
This is what I have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme properties -->
<bool name="enableIconPack">true</bool>
<bool name="enableDockPack">false</bool>
<string name="authorName">Clay Cauley</string>
<string name="developerName">Clay Cauley</string>
<string name="authorLink">MY SITE</string>
<string name="theme_title">MY TITLE</string>
<string name="theme_description">This is some text explaining who created the theme and why, etc ... This is also where I would like my logo to appear.</string>
</resources>
My problem lies in the last string, "theme_description" --- If it's possible I wanted to get my logo in there so people see it when on this screen:
Hopefully wanting to get it where the "Hey" currently is.
I've tried 2-3 different approaches but they were all guesses and none worked so I thought I would try here. Any help is greatly appreciated, even if it's telling me it isn't possible :)
This is my "main.xml"
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="65dp"
android:gravity="center"
android:horizontalSpacing="1dp"
android:listSelector="#000000"
android:background="#000000"
android:numColumns="3"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="-1dp" >
</GridView>
Thanks!

What you probably want to do is to add an image to a TextView. There are attributes for that: "android:drawableLeft", "android:drawableRight", "android:drawableStart", etc.
<TextView android:drawableLeft="#drawable/my_drawable" .../>
If you want to define it in a resource file, then do it in styles.xml:
<style name="my_text_view_with_drawable">
<item name="android:drawableLeft">#drawable/my_drawable</item>
</style>
<TextView style="#style/my_text_view_with_drawable" .../>

I think the original poster https://stackoverflow.com/users/1344453/clay-cauley is asking how would you specify an image to be inserted along with the text from a string in string.xml
Since you can specify links and email inside a string, it would be nice to reference a drawable.
Asked differently how would you do something a la html img tag, something like this :)
<img src="#drawable/icon" >
I too am interested in this possibility.
I know how to dedicate the layout with an ImageView and load a bitmap of choice via code.
I think the problem is trying to use TextView (it probably is not designed to display images, how about if the layout has a WebView control? Could a string resource reference a bitmap in the drawables, in local storage?
Thank you.

You do this with a kind of layout, like linearlayout or relativelayout.

Related

Create a different directory other than res/values/styles.xml to define styles

As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.
For example:
res/values/styles_for_main_screen
res/values/styles_for_set_screen
And then in main_screen layout
<TextView
style="#styles_for_main_screen/text_view_custom_style">
</TextView>
This example obviously doesn't work, but it shows what I'd like to achieve.
I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?
Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.
Example how it should be done, thanks #Frank N. Stein for the answer:
This how looks my custom xml res/values/styles_for_main_screen
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_back">
<item name="android:background">#E81C1C</item>
<item name="android:text">whatr</item>
</style>
</resources>
and then to retrieve this style I just write:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="#style/custom_back"
/>
so the convention looks like:
style="#(what I want to retrieve)/(name of style)"
from the android developers site
In XML: #[package:]style/style_name
You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.
Therefore, YES! You can have multiple ones, if so you desire.
Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.
Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.
Then, as Frank said, Yes. You can use whatever file name to write your custom styles.

Hiding views declaratively based on screen size in Android

In android xml:ish
Is there any way to change a visibility attribute based on the layout size/orientation in the xml directly?
I have a button in a fragment that should be visible for small screens sizes only. On larger sizes, let's say layout-large, I want it to be hidden.
Sure, I can write code for this without any problem but for academic reasons I would like to know it it's possible to do something like this.
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="<magic expression here>" />
Thanks
// Johan
This answer is based off the explanation provided here by Flávio Faria.
The visible, gone, etc can be values mapped to their corresponding enum values in a string resource - which means you can create a visibilty.xml with string resources for each layout you want, and Android will automatically resolve the one you want for each screen type.
I'd recommend the following:
/res/values/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Enum values pulled from Android source -->
<string name="visibility_visible">0</string>
<string name="visibility_invisible">1</string>
<string name="visibility_gone">2</string>
<string name="product_info_footer_button_visibility">#string/visibility_visible</string>
</resources>
/res/values-large/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="product_info_footer_button_visibility">#string/visibility_invisible</string>
</resources>
And then you can reference the visibility as follows for your button:
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#string/product_info_footer_button_visibility" />
Warning: This depends on the device having the same enum values for visibility (0/1/2) as defined in the AOSP source. Device manufacturers and custom ROM creators can change these values, in which case this code will likely not work as desired.
The android:visibility attribute is an int (like many attributes) so you can do the following :
Define a resource file named visibility.xml in values-port and values-land resource directories. The content of this file is like this :
values-port/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">0</integer> <!-- 0 is the value for android:visible -->
</resources>
values-land/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">1</integer> <!-- 1 is the value for android:invisible -->
</resources>
and in your layout.xml :
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#integer/buttonvisibility" />
It works : btn_check_availability is visible in portrait and invisible in landscape.
Note : this example use layout orientation as discriminator, but you can of course do it with any resource qualifier (like dimension, density, ...)
There is no magic expressions available in XML. If only.
There are two approaches to this problem:
a/ use the drawable folder system. Drawable folders can be copied and named to be DPI aware following the conventions dictated here: Supporting Multiple Screens.
b/ Do it programmatically. On runtime check for screen DPI and show/hide view accordingly.
Have you looked at using includes and multiple layouts organized into the appropriate size/orientation layout folders? Some layouts could either simply not have the button or have it hidden by default.
Re-using Layouts with include
Providing Alternative Resources

android - how to manage background images without memory leaks?

Good morning,
in the process of writing my first android app, so please be patient.
I want to have background images for the activities I use. My first approach to add it simply to the layout of the activities,
....
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/desc_background"
android:scaleType="centerCrop"
android:src="#drawable/background"
android:tint="#dd000022" />
....
unfortunately this seems to lead to a memory leak when I call certain activities repeatedly. Finally I got hold of and found the reason why here Avoid memory leaks on Android
My next thought would have been to do what Romain in above mentioned article calls a " very fast and also very wrong" solution - saving the bitmap in a static field or in the Application object.
I found various articles on this topic yet none of them seems to be aware of Romains point. Also I am not clear how to implement Romains solution.
Any hint how to handle images in android without running into memory leaks would be highly appreciated.
Thanks a lot
martin
Why not just add a background to the layout you're using.
ex using linearlayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/my_background"
android:keepScreenOn="true"
android:orientation="vertical" >
**REST OF LAYOUT HERE**
</LinearLayout>
or to use one image for all activities create your own style and apply then in you manifest
in your res/values add a styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowBackground">#drawable/my_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
and in manifest for each activity add
android:theme="#style/MyTheme"

Android element ID naming

So I have a string in my strings.xml file declared like so:
<string name="welcome">Please hit the menu to begin</string>
And I have a TextView in my main.xml that uses it like so:
<TextView
android:id="#string/welcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome"
/>
Now, is that the proper way to give a TextView an ID? It seems strange to use a string resource as an ID like that.
Now, is that the proper way to give a TextView an ID?
No. Use android:id="#+id/whatever".
To add an id directly to a textview you must append a + sign
android:id="#+id/welcome"
alternatively you can have an id set up in a resource file
<resources>
<item name="welcome" type="id"/>
</resources>
android:id="#id/welcome"
http://developer.android.com/guide/topics/ui/declaring-layout.html
is the manual page related to this topic

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"

Categories

Resources