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"
Related
I want to style all my ImageButtons in a theme. After searching for quite some time I found the solution to my problem. But I don't know why it works like it does.
My main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher_foreground" />
</LinearLayout>
This is my original theme that didn't work. It styles my TextView but ignores the ImageButton. The result is shown in the screenshot below.
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:imageButtonStyle">#style/redBackground</item>
<item name="android:textViewStyle">#style/redBackground</item>
</style>
<style name="redBackground">
<item name="android:background">#FF0000</item>
</style>
</resources>
And here's the theme that works:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="imageButtonStyle">#style/redBackground</item>
<item name="android:textViewStyle">#style/redBackground</item>
</style>
<style name="redBackground">
<item name="android:background">#FF0000</item>
</style>
</resources>
The only difference is the missing 'android:' prefix in front of the 'imageButtonStyle' attribute.
So my questions are:
What is the difference between imageButtonStyle and android:imageButtonStyle?
Why does android:textViewStyle work but not android:imageButtonStyle? They are both defined the plattforms 'attrs.xml'.
Why is there no textViewStyle (without android prefix)? Removing the prefix yields an error.
Where are the attributes defined that have no prefix? Apparently not in the plattforms 'attrs.xml'.
Where can I find proper documentation for the whole style stuff? Of course I halve already read the respective Google docs (https://developer.android.com/guide/topics/ui/look-and-feel/themes.html). But still i have basic questions like this one.
Interestingly, it seems like the 'android:imageButtonStyle' version has worked some years ago: How to apply an style to all ImageButtons in Android?. I haven't tested that myself, though.
And here's the post that proposed removing the android prefix. Including unanswered comments that ask why it works: buttonStyle not working for 22.1.1
android tag that you use is used for attribute coming from Android SDK.
app tag is used if you are using the support library.app is just a namespace for any custom parameters for a custom View.
This can be anything but if you see the root element there's probably a line xmlns:app="http://schemas.android.com/apk/res-auto" that assigns the namespace
You may also see other namespaces if you are using custom views (of your own or form a library).
In case that anyone else stumbles across the question: I've found the answer in this Droidcon talk: https://youtu.be/Jr8hJdVGHAk?t=21m12s
The topic is handled in a minute starting at 21:12.
As I understand it, specifying no namespace results in the global namespace being searched which seems to include the support libraries attributes. And indeed both, the SDK's R.attr as well as the support library's R.attr define the imageButtonStyle attribute (with slightly different descriptions). However, the support library does not define a textViewStyle attribute. So that explains why you can't omit it's android: prefix.
To answer my last question concerning the documentation: Despite the Google guide and the R.attr classes' documentation, the video mentioned above and this Google I/O talk are quite informative: https://www.youtube.com/watch?v=TIHXGwRTMWI
So the only question that is left open is why the SDK's imageButtonStyle does not work.
I'm trying to show a SurfaceView inside an Activity that has a theme that extends Theme.Dialog (you can think as I'm trying to show it inside a Dialog class), but I'm having some layout glitch.
I couldn't make a picture because the glitch vanish when taking a screenshot :/ but anyway what you see is that the "hole" of SurfaceView is moved from its original position and it create some weird overlay effect...
The problem seems to be related with the flag windowIsFloating, if I set it to false the glitch goes away...
Any idea for a possible workaround to keep using windowIsFloating flag?
Simple layout to reproduce the issue:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<FrameLayout android:id="#+id/container"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:padding="30dp"
android:background="#ff0">
<SurfaceView android:id="#+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
and modify your activity's theme like this to reproduce the issue:
<style name="MyTheme" parent="#android:style/Theme">
<item name="android:windowIsFloating">true</item>
</style>
It seems like there is no way to solve the glitch, simply windowIsFloating flag is very buggy with SurfaceView...
So, following also the answer here How to create a transparent activity WITHOUT windowIsFloating, I ended up by creating a theme that extends Dialog's theme but set windowIsFloating flag to false and windowIsTranslucent to true.
That way you need manually to create a layout that behave like a Dialog, but you don't have any glitch and your background can be fully transparent :)
Symple example of a theme that will do the trick:
<style name="MyTheme" parent="#android:style/Theme">
<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">true</item>
</style>
Then you need to apply margins to your layout, otherwise it will be fullscreen as a normal Activity.
I share this because it may be useful to others. I've run into a transparency issue with a SurfaceView inside a dialog and the following answer helped me resolve it (and I did not have to use windowIsFloating=false as suggested here although it also does the trick):
https://stackoverflow.com/a/7061396/875442
As it turned out my problem was both a Z-ordering and a transparency issue.
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.
I have a ListView, and it works great on a phone. Now I am making a tablet UI, with the ListView on the left and details on the right.
When I touch an item, it flashes blue as long as it is pressed. I want to keep that blue color until another item is selected, just like the Gmail app on the Nexus 7.
What is the cleanest way to achieve that? I'd rather avoid setting backgrounds manually, I assume there is a way to mark an element as the "active" one and theme it accordingly.
What is the cleanest way to achieve that?
What you are looking for is known as the "activated" state. To make this work:
Step #1: In res/values-v11/, have a style resource that implements activated. For example, for a new project that has the AppTheme declaration defined there, go with something like:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"></style>
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
</resources>
Step #2: Define the same style in res/values/ for any older devices, just as a stub style resource, so references to it continue to work:
<resources>
<style name="AppTheme" parent="android:Theme.Light"/>
<style name="activated" parent="AppTheme"/>
</resources>
Step #3: In your layout XML resource for the row in the ListView, add style="#style/activated" to the list of attributes of the root element
Step #4: Set the ListView to be a single-choice list, such as the following line in a ListFragment:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
You can see this in action in this sample project, this sample project, and this sample project. For more background on those first two samples, see this SO question: Complete Working Sample of the Gmail Three-Fragment Animation Scenario?
Using
android.R.layout.simple_list_item_activated_1
instead of
R.layout.simple_list_item_checkable_1.
Just for somebody checking someday.
after days of search and pulling my hair i just found out that activatedBackgroundIndicator is also available in ActionBarSherlock styling system. Most of the devs which develop backwards compatible apps, use ActionBarSherlock,so using ActionBarSherlock is a good option for most cases. So instead of using android:background="?android:attr/activatedBackgroundIndicator" which will give errors in android versions prior to 11, just use: android:background="?activatedBackgroundIndicator"
here is the example row layout xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//note the activatedBackgroundIndicator
android:background="?activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingTop="2dip" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textSize="15sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="5dip"
android:textSize="20dip" />
</LinearLayout>
I would like my application to be available under different brandings - this means that it must be possible to easily change background bitmaps in few places, but also change text resources.
My plan is to use themes and styles, I know they can be used for switching bitmaps but will they allow me to change also texts in TextViews for example?
Is it also possible to specify in style or theme a text identifier and later dynamically read it in code?
[EDIT]
After some investigation, of course texts can be substituted with styles. Another problem that comes with application branding is the need to change package name - otherwise Google Play wont accept our branded application.
[EDIT]
After more investigation, below I include small sample on how to add two switchable themes that will allow to substitude drawable in activity layout, and text resource in textview. Whats left is to call setTheme(R.style.Theme1); or setTheme(R.style.Theme2); in onCreate before setContentView.
<-- attrs.xml />
<resources>
<attr name="ProductName" format="reference"/>
<attr name="ProductBackground" format="integer"/>
</resources>
<-- styles.xml />
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme1" parent="android:Theme.Light">
<item name="ProductName">#string/s_product_1_name</item>
<item name="ProductBackground">#drawable/back_1_vga</item>
</style>
<style name="Theme2" parent="android:Theme.Light" >
<item name="ProductName">#string/s_product_2_name</item>
<item name="ProductBackground">#drawable/back_2_vga</item>
</style>
</resources>
<-- my activity layout />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?ProductBackground"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".MainActivity"
android:background="#ff0000"
android:text="?ProductName"
/>
</RelativeLayout>
There are two aproaches to make different brandings of you app:
1) Split your app into multiple library projects, common code is in lets say common_lib library project, and all your brandings are in separate projects using common_lib but changing some of the resources, like strings, some drawables, also package name. This is the aproach we have choosen in our app.
2) Use gradle product flavors: http://developer.android.com/sdk/installing/studio-build.html, I think this should now be the prefered solution.