Referring to resource for Shape Drawable stroke width - android

Eclipse is able to render my layout when it includes:
...
<shape>
...
<stroke>
...
android:width="#strings/someWidth"
where someWidth is defined in strings.xml as
<string name="someWidth">5dp</string>
but it crashes on a real device, and only works if I use a direct string, like:
android:width="5dp"
How can I use a resource for the stroke width?

Try to use the dimension resources.
Create a dimens.xml file inside your values folder with the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="some_width">5dp</dimen>
</resources>
You can refer to this using #dimen/some_width.

Related

How to create a gradient color background for splash screen in Flutter?

I'm working on the splash screen for my Flutter app, and in drawable folder I have to create a file called colors.xml in order to change background color for my splash screen. I'm finding it hard to make it a gradient color. My intention is to create a gradient background color that would start at the top left and end at bottom right, using two different colors. Does anyone have an example of how to do that in Flutter? Thanks!
P.S. An is there a difference in the process for android and ios?
1 In \android\app\src\main\res\drawable\launch_background.xml
change this :
<item android:drawable="#android:color/white" />
to :
<item android:drawable="#drawable/gradient_background" />
2 Create this file \android\app\src\main\res\values\colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gradientstart">#3587d0</color>
<color name="gradientend">#36f1d3</color>
</resources>
3 Finally, create this file \android\app\src\main\res\drawable\gradient_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/gradientstart"
android:endColor="#color/gradientend"
android:angle="90"/>
</shape>
For Android first define two color in your colors.xml:
<color name="gradientstart">#888888</color>
<color name="gradientend">#111111</color>
then in \android\app\src\main\res\drawable\launch_background.xml just replace this:
<item android:drawable="#color/background" />
to this:
<gradient android:startColor="#color/gradientstart" android:endColor="#color/gradientend" android:angle="315"/>
and for ios according to this question
If your gradient is a simple vertical or horizontal gradient, and
you’re really concerned about asset size, you can define a very narrow
image and then add an image view with “scale to fill” content mode.
But these images are so small anyway, the amount of space saved will
be negligible, so I’m not sure it’s worth worrying about.
I found use of flutter_native_splash much more easy and direct. Here's a link on the steps to creating one. First design your desired gradient as an image and instead of adding color, add a background_image on the pubspec.yaml file.
Something like:
flutter_native_splash:
image: ""
background_image: "assets/my_splash_background.png"

Error by including drawable xml-file

Hello community,
I want to iclude different drawable xml-files into my activity_main.xml
However,when I want to includ following code :
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<size
android:layout_height="110dp" />
</shape>
</item>
</selector>
the name is button_height.xml into my main-file:
<Button
android:id="#+id/cmda1"
android:layout_width="110dp"
android:layout_height="#drawable/button_height" />
It does not work and Eclipse says only:
You must supply a layout_height attribute.
and
error in an XML file: aborting build.
I searched an answer in the internet but dont found one.
But I think its an easy mistake lots of people are doing.
so, I hope for usefull answers
and sorry for my awful english.
Your whole idea of drawable, selector and dimension is messed up. Can't put height in selector and also selectors (which are drawable) as height.
Do this.
If you want to use xml to get height
Add a file in res folder with name res/values/dimens.xml and add this line in that
<resources>
<integer name="btn_cmda_height" >110dp</integer>
</resources>
and change the button cmda1 height property as
android:layout_height="#dimen/btn_cmda_height"
Make it dynamic for various screen sizes by creating these files which will hold different values. Although this is the old way.
Read more about new way to do this here
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
If you don't want to use xml to get height
just do this android:layout_height="110dp" in the button cmda1 height property
NOTE:
Your selector code is wrong and not required at all. Selectors are used to define the background state of a view

Easy way to change ProgressBar standard color scheme in Mono for Android?

I am looking for a way to change the tint and background colors of a standard android ProgressBar in Xamarin Studio. Found some tips for how this is done in java like this:
myprogress.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.progress));
But I couldn't figure out how to use this in Xamarin. I have also seen the pretty large xml files, making gradient backgrounds. But shouldn't there be an easy way to just change the colors without any gradient?
you should probably go with this.
in res/values add new style by adding
<style name="Theme.MyAppTheme.Process" parent="android:Theme.Light">
<item name ="android:background">#2D2D2D</item>
<item name ="background">#2D2D2D</item>
</style>
Now use this style in your ProgressBar.
myprogress.setProgressStyle(R.style.Process);
you can change color code in backgroud of style..
To convert the Java to C#, it would look like this:
myprogress.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.progress));
All you need to do is put the drawable (png, xml, etc) you want to use as the background in your Resources/drawable folder. If you just want a gradient you should use xml instead of a bitmap. Create a new xml file in your Resources/drawable folder, here is an example of a blue gradient:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#065b92"
android:endColor="#1183ce"
android:type="linear"
/>
</shape>

dimension resource cannot be found in Xamarin for Android

In a drawable XML file, I want to use a dimension resource "background_radius".
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="radial"
android:startColor="#FFFFFF"
android:endColor="#00000"
android:gradientRadius="#dimen/background_radius"
/>
</shape>
The dimension resource is defined in Values\Dimens.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="background_radius">400dp</dimen>
</resources>
I got the following error when I build it.
No resource found that matches the given name (at 'gradientRadius' with value '#dimen/background_radius'). ...\Resources\drawable\background.xml
But when I design it in VS, I can see the correct background radius. It seems VS just cannot build it.
What's wrong?
Thanks.
+++++++++++++++++++++++++
For some unknown reason, the project did not pick up the new dimens xml file. I re-created the files and rebuild it. Now the dimen can be found.
In addition, the radial gradient does not like the dimen value with dp. so I update the dimen resource to:
<item name="background_radius" format="float" type="dimen">400</item>
Give your dimension a dimension unit (dp, sp, etc.)
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp.
So, change it to:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<dimen name="background_radius">400dp</dimen>
</resources>
for example.
I came across a similar thing; created a Colors.xml file and everything was as it should be but the resource could not be found. After a while I forgot about it and found that it had finally been picked up. Today made a Dimens.xml and the exact same thing happened.
I had to restart Visual Studio in order for it to be picked up.

Can I define multiple ColorStateLists in a single XML file?

In my app I have a lot of controls that should change their text color when changing drawable state. Android provides a way to do it - color state lists. For each color state list I have to create a separate XML file, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#398ede" android:state_pressed="true"/>
<item android:color="#808080"/>
</selector>
Is there any way to avoid creating a separate file for each color state list and define them all in a single file?
You must have found the answer by now, but for the sake of those who come to this link again, here's how we do it.
If you have a resource file defining multiple colors like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="xBlack">#FF000000</color>
<color name="xYellow">#FFFFFF00</color>
</resources>
You need to put this file under values (and not in colors)
To access the values you need to use:
dummy_button_id.setBackgroundColor(getResources().getColor(R.color.xBlack));

Categories

Resources