i'm just getting started with developing Android Apps. Therefore i have a question, for designing my GUI. Maybe there is a possible build-in widget, but i don't have the right wording to find it (English is not my native language).
I'm looking for something like the Rating Bar, where you can select a value between 1 and 5. Instead of the stars, i'd like to have some bars (the left one is the smallest, the right one the highest). Should look like the "Stairs" within a Signal strength notification. To make it a bit more challenging i'd like to have different colors for every bar (least value is green, highest value red). Therefore i don't think that the Rating Bar with different drawings will fit my needs, because afaik i could just change the star layout, not like the first star should become a 5dp bar, the second a 15dp and so on.
Is there any build in widget i could use?
I attached a picture to show my idea.
I hope you can help me
what you want is easily achievable with a level-list. In your case you need five different drawables, on for each level and then combine them in a drawable xml file, like:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/no_level"
android:maxLevel="0" />
<item
android:drawable="#drawable/first_level"
android:maxLevel="1" />
<item
android:drawable="#drawable/second_level"
android:maxLevel="2" />
<item
android:drawable="#drawable/third_level"
android:maxLevel="3" />
<item
android:drawable="#drawable/forth_level"
android:maxLevel="4" />
</level-list>
and set this drawable as source for an ImageView, for instance. At runtime, retrieve the ImageView and call setImageLevel, with a value from 0 and 4, and the corresponding level will be displayed
Related
I've got two images. I want to do so: when user opens an app with a Light theme - the first image is being used as a background for layouts. When user opens the app with a Dark theme - the second image is being used as a background for layouts.
To solve this problem with text colors, we can just use styles.xml and colors-day/night.xml and one line of code: <item name="android:textColor">#color/textColor</item>
I've tried to the same with images and two styles files: <item name="android:background">#drawable/day</item>
But this feature applies background to each element on a screen, not only to the main layouts.
I know, that I can do it programatically by changing a background with if statements and layout.setBackgroundResource(R.drawable.day/night);
But maybe it can be done with XML as in the case of the text color?
To solve this problem with text colors, we can just use styles.xml and
colors-day/night.xml and one line of code: #color/textColor
You need to do the same thing, but instead of res/values/colors.xml, it should be done on the drawable res/drawable/day
So, you'll have a drawable & drawable-night folders in the res directory each of which should have a unique version of the day image with the same name.
They should look like in Android Studio (Android preview):
And normally use the the attribute name="android:background">#drawable/day</item>
I am looking into code injection for Android apps (written in Java). Let's say I have a set of ImageViews declared similarly as such within separate layout files in .../layout/ :
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#color/colorPrimary"
tools:layout_editor_absoluteX="70dp"
tools:layout_editor_absoluteY="38dp" />
Now let's say I have the following attribute:
android:rotation="180"
My goal is to add this attribute to every single ImageView in every .xml file in the layout directory on run-time, so that I do not have to edit the source code of these .xml files. How would I go about doing this?
You can do this dynamically. What you need to do is to find the image view on activity and call set rotation method and give angle as you are giving in xml file.
And second way of doing the same thing is you can provide customize style for image view in styles folder of the project.And this will be very simple and far more effective way of doing so. To achieve this via style, you can use below code.
<style name="TitleBarImage">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:src">#drawable/lock</item>
<item name="android:rotation">180</item>
</style>
and in every image view, you need to use this below code to get the reflection of style
<ImageView style="#style/TitleBarImage" />
Hope this will help you.
In theory a simple thing. I would like to change the background color of toast (android:minSdkVersion="14" android:targetSdkVersion="18"). What I did? I've found the Theme.Holo.Light definition which I use as parent for my own style:
Theme.Holo.Light definition
Next I've found:
<item name="android:toastFrameBackground">
Nest I wanted to modify it:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBarStyle</item>
<item name="android:toastFrameBackground">#android:color/holo_blue_light</item>
etc.
While for Action Bar it works without problems, for toastFrameBackground eclipse displays always:
error: Error: No resource found that matches the given name: attr 'android:toastFrameBackground'. styles.xml Android AAPT Problem
I've even set it to the original version (just copied from original theme definition):
<item name="toastFrameBackground">#android:drawable/toast_frame</item>
Hoping it should work. No way.
When I click ctr+space Eclipse it seems not to see toastFrameBackground. Why? I've checked some other items from original theme definition and some seems to be visible, while others no.
Any idea? help?
I've spent 3-4 hours and nothing... Probably I will keep the original toast background color as it seems it's not worth to fight but I would prefer to understand deeper the mechanism. Why I don't have an access to the item it seems I should have an access.
That's because toastFrameBackground is not exported and did not make to public.xml, which means this attribute is not available for non-platform/third-party applications.
However, you may want to see https://stackoverflow.com/a/9903465/1893766 for what you are trying to achieve.
First excuse me for my english.
Im designing a theme (With HoloEveryWhere) and im trying to style default spinner.
The problem is that the spinnerStyle is not working. I'm testing with this layout.
<Spinner
android:id="#+id/spCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/etPhone" />
And im using this for my theme.xml..
<item name="android:spinnerStyle">#style/Coloquium.Spinner.Ruby</item>
and in styles.xml
<style name="Coloquium.Spinner.Ruby" parent="#style/Holo.Spinner.Light">
<item name="android:background">#drawable/spinner_holo_white</item>
</style>
The problem is that the spinner style is never applied (Others styles like edittextstyle or buttonstyle works fine), the background never change, But i tried this style as spinnerItemStyle and work fine, but spinnerItemStyle is the inner view, and i want to style the outer, the spinner.
If i set background directly in the layout node, the background change fine, but i need to set spinnerStyle for all spinners in the theme, not in layout.
I test a lot of post looking for a solution without success.
The solution is:
HoloEveryWhere appear use "spinnerStyle" and not "android:spinnerStyle".
But really... I can't find the HoloEveryWhere documentation about this. :S
I am currently making a small application in Android.
My wish is to have two ImageViews that change their animation when i click different buttons on the screen.
First a sample xml: (the drawables are 250x350y .png files)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/main_idle_kf1" android:duration="500" />
<item android:drawable="#drawable/main_idle_kf2" android:duration="500" />
<item android:drawable="#drawable/main_idle_kf3" android:duration="500" />
<item android:drawable="#drawable/main_idle_kf2" android:duration="500" />
</animation-list>
Example of my code
ImageView _imgView1; //declared in oncreate = ((ImageView) findViewById... etc)
AnimationDrawable _aniDrawable1;
OnResume:
_imgView1.setBackgroundResource(R.drawable.anim_main_idle);
_aniDrawable1 = (AnimationDrawable) _imgView1.getBackground();
_aniDrawable1.start();
So far this works, and there are no issues. The little sprite does it's jig.
I am however doing this to two imageViews at a time, and I've been trying to add more/different "frames" in the XML for each. However, i do not have to add many frames, before the VM crashes because it's out of memory.
It seems to me as if it's making a bmp out of each drawable mentioned in the XML, and then binding this in the VM memmory when i call "setBackgroundResource".
My question: Is there any way to make this less memmory intensive? Can i change how .setBackgroundResource saves the data from the XML? 7-8 different pictures like the one in my xml is enough to break a 16MB VM.
I dont want much, i just want a few frames of animation running side by side that change when i click buttons, but that seems prettymuch impossible if AnimationDrawable eats so much out of the VM-memmory.
Hints? ideas? alternatives?
Refer to this article - Loading large images
Particularly look at "Load a Scaled Down Version into Memory" section
Try keeping your image resources in /drawable-nodpi
Android might be assuming resources as /drawable-mdpi as default.