How to make transparent glass color background - android

I'm trying to make a Layout button like the one in this photo with a transparent glassy color.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/glassColor"/>
<stroke android:width="1dp"
android:color="#color/colorWhite"/>
</shape>
I have the drawable, but I can't figure out how to create a hex color like the one in the picture

I am not sure this is the answer you are looking for but if you create a new color value #color/ you can access the built in color pane to set the color transparency.
I am sure you know this by now and surely my answer is not what you have expected.
<color name="colortransparent">#4F784A06</color>

Related

How to pass color to custom xml item in Android?

I have a custom shape for button's dashed border. With hardcoded color everything works as expected, but I need to pass color from outside. How can I do it?
Here is my xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid/>
<corners android:radius="16dip" />
<stroke
android:width="1dp"
android:color="#color/blue"
android:dashWidth="3dp"
android:dashGap="3dp"
/>
</shape>
</item>
</selector>
and this is usage
android:background="#drawable/dashed_border_button"
I need to change border color from hardcoded to dynamic
I achieved the same by using another Drawable resource file wit the attribute Stroke - Color = "your color"
and then setting the background Drawable to new Drawable file
yourview.setBackgroundResource(R.drawable.another);
This is because the method :
DrawableCompat.setTint(as.getBackground(),Color.BLUE);
Set even the solid fill color to the blue (here in this case) , which you don't want.
Hope it helps!!`
You need to get background() from your view and make
DrawableCompat.setTint(color, DrawableCompat.wrap(view.bacground())

Changing color of a portion in picture

I am doing such type of project, in my project, i want to change the color of only a portion of my picture, for example when I click on a button I want the blue become green, i want to know if there is any way to do that or it's impossible
To achieve the above feature, you could define a shape and place it above the button and then change the color onClick. A simple example is:
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp">
</corners>
<solid
android:color="#4848ea">
</solid>
</shape>
Now change the color of the shape using the following code.
GradientDrawable Shape = (GradientDrawable)shape.getBackground();
Shape.setColor(Color.GREEN);
Place the above code within the button onClick.

How to change the background color of EditText without removing the standard bottom border

After the call of EditText.setBackgroundColor(Color.RED) I see only a red rectangle with text inside it, but I want to change only the text background and to keep the standard bottom border which is grey when the field has no focus and blue if it has focus. Can I do it programmatically?
I want to change the text background to red if the input is invalid and to transparent if it is valid.
I think the best way is to work with drawable/shape.xml as background and upon logic code situation call EditText.setBackground(some_other_shape.xml). Here is an example for shape file xml demonstrating how to use:
Border color ("stroke"): In this example some custom color from colors.xml
Fill color ("solid"): In this example Android default transparent color
Even image icon inside
<!--Example for custom shape file xml design-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#mipmap/icon_image" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/orange_primary" />
</shape>
</item>
</layer-list>
So just prepare several shapes for each situation you need. Another approach is to work with layout params etc. but I think this one is faster and gives more control for custom design in easy to understand code.

How to access drawable resources (color) in java

I want to access color resource defined as drawable resource and desire to toggle the background color in JAVA, basically background of a button was changed using below mentioned drawable XML. I tried accessing button and modify color attribut but this changed the shape of button to normal square shape. I want to keep shape as defined in drawable XML and change background color manually.
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#EAEAEA" />
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="1dip"
android:topLeftRadius="1dip"
android:topRightRadius="8dip" />
</shape>
</item>
<item><shape android:shape="rectangle">
<solid android:color="#EAEA00" />
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="1dip"
android:topLeftRadius="1dip"
android:topRightRadius="8dip" />
</shape>
</item>
You have 2 possibilities:
myButton.setBackgroundColor(Color.CHOOSE_ONE);
myButton.setBackgroundResource(R.color.youCustomColor);
If you want to set the color from an hexadecimal value just use the static method of the Color class:
myButton.setBackgroundColor(Color.parseColor("#RRGGBB"));
//http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29
You can use following code to change the color of button -
button.setBackgroundColor(Color.rgb(red, green, blue));
and get rgb values from below link -
http://www.ceveni.com/2009/08/set-rgb-color-codes-for-android-and-rgb.html
If you are using a Resource Color You should probably resolve it with getResources().getColor(R.color.example_color)
For this option, your code would be the following:
myButton.setBackgroundResource(getResources().getColor(R.color.example_color));

Complex custom button states in Android

in my current project I'm building an App with API 10 which should look like an ICS App.
Since my GUI is not very complex I can build this all manually, but I have some problems with button states.
I googled a lot and found nothing that would work for me so I would appreciate any help ;)
I designed an action bar with google ICS stock icons. When for example someone touches the search-loupe-icon, which has an 8dp padding around it, it's background color should change to a defined highlight-color-value.
So now you would suggest probably a selector drawable. But my case is: I have an icon-drawable which has an background-color, which I want to change. I dont want to change the icon, so since I cant change the backgroundcolor from a selector-xml this doesn't work.
(I thought I can setup multiple drawable-xml with my icon static bg colors so that I could refer with one selector to those different colored icons, but since a shape-drawable-xml cannot have a source and an bitmap-drawable-xml cannot have a background color, this is no sollution.)
If I try to change the background color in my onclick-listener (also tried onTouch) with:
ImageButton searchIcon = (ImageButton) findViewById(R.id.upper_actionbar_icon_search);
searchIcon.setBackgroundColor(R.color.android_ics_blue_light);
The Icon gets a dark grey bg color. This is also true for any other color altough nothing lies on top of the ImageButton. I also tried an ImageView.
So could someone explain to my please how the hell I can define a highlighted bg color-image without any hacks (default-gone image that lies above the icon and is set visible onclick).
Thanks so much!!
Okay the solution is simple.
Write yourself a drawable selector which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#color/android_ics_blue_light"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/android_ics_blue_light"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/light_grey"/>
</shape>
</item>
Within your ImageButton code define your icon as src and your new drawable as background, like this:
<ImageButton
<!-- ... -->
android:src="#drawable/icon_search"
android:background="#drawable/actionbar_ontouch"
/>
Works like a charm!

Categories

Resources